How to change the background of a android Dialog Box - java

How can I change the background color programatically of a dialog box?
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
builder.setMessage(R.string.tnc)
//.setTitle(R.string.tnc_title)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Log.d(TAG, "OK Button Clicked");
dialog.dismiss();
finish();
}//end onClick
});
AlertDialog dialog = builder.create();
dialog.show();

This is the best way to do it !
Firstly make a dialog box programmatically .
public void onClick(View v) {
dialog1 = new Dialog(Page.this,R.style.myBackgroundStyle);
Window window = dialog1.getWindow();
window.setGravity(Gravity.CENTER);
window.setLayout(WindowManager.LayoutParams.FILL_PARENT,
WindowManager.LayoutParams.FILL_PARENT);
window.setBackgroundDrawable(new ColorDrawable());
dialog1.setTitle(null);
dialog1.requestWindowFeature(Window.FEATURE_NO_TITLE);
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
dialog1.setContentView(R.layout.custom_dialog);
dialog1.getWindow().setBackgroundDrawable(
new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog1.setCanceledOnTouchOutside(true);
description.setText(DESCRIPTION);
heading_name.setText(PRODUCT_NAME);
Button abc = (Button)dialog1.findViewById(R.id.abc);
abc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
Button xyz = (Button) dialog1.findViewById(R.id.xyz);
retailer_btn.setOnClickListener(new View.OnClickListener() {
dialog1.dismiss();
}
});
dialog1.show();
}
});
and now create a new xml file and set this code in your xml (Dialog Layout)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/description_layout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="45dp"
android:layout_gravity="top|right"
android:gravity="right|top"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/desription_layout2"
android:layout_width="160dp"
android:layout_height="90dp"
android:layout_marginRight="2dp"
android:padding="2dp"
android:background="#drawable/dropdown_white_background"
android:orientation="vertical" >
<Button
android:id="#+id/abc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="2"
android:background="#drawable/curve_1"
android:text="abc"
android:textSize="18dp"
android:textColor="#000000"/>
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_marginBottom="1dp"
android:layout_marginTop="1dp"
android:background="#ffffff" />
<Button
android:id="#+id/xyz"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="2"
android:background="#drawable/curve_1"
android:text="xyz"
android:textSize="18dp"
android:textColor="#000000"/>
</LinearLayout>
</LinearLayout>
and if you want to do additional stuff with your dialog such as rounding the corners of the dialog of the button then create a new xml in the drawable and the add this as the background accordingly.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<corners android:radius="3dip" />
<stroke android:width="1dip" android:color="#ffffff" />
<gradient android:angle="-90" android:endColor="#ccffff" android:startColor="#ccffff" />
</shape>
</item>
<item android:state_focused="true"><shape android:shape="rectangle">
<corners android:radius="3dip" />
<stroke android:width="1dip" android:color="#ffffff" />
<solid android:color="#66ffff" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<corners android:radius="3dip" />
<stroke android:width="1dip" android:color="#66ffff" />
<gradient android:angle="-90" android:endColor="#66ffff" android:startColor="#66ffff" />
</shape>
</item>
</selector>
Hope this helps :)

In that case you require to make a custom dialog box.
Refer this tutorial here which illustrates that well.
http://www.codeproject.com/Tips/659766/Android-Custom-DialogBox
Hop it helps.!!

Create a custom layout and set it as the layout for your dialog box.
You can use this code:
final Dialog dialog = new Dialog(getActivity());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom_dialog_box);

Related

AppCompatActivity has changed my AlertDialog design

I have change my activity to appCompat Activity.
Also i changed AlertDialog to Android.Support.V7.App.AlertDialog.
But i have lost my previous alertdialog design.
This is how it was look like.
And this is how it looks now
My theme till now was
parent="#android:style/Theme.Holo.Light.DarkActionBar">
But i was enforced to changed it cause appCompat doesn't support Holo theme.
So i change it to
parent="Theme.AppCompat.Light.DarkActionBar">
How can i make alert dialog look like previous one?
Try this:
AlertDialog dialog= new AlertDialog.Builder(new ContextThemeWrapper(context, android.R.style.Theme_Holo_Dialog));
if you want to make buttons could help user for easier press, you can build a dialog with custom buttons and style the Button(e.g.style the Button on drawable folder).You can refer to the following code:
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
AlertDialog alert = dialog.Create();
alert.SetTitle("Login Information");
//alert.SetMessage("Complex Alert");
//alert.SetIcon(Resource.Drawable.alert);
LayoutInflater inflater = (LayoutInflater)this.GetSystemService(Context.LayoutInflaterService);
View view = inflater.Inflate(Resource.Layout.input_layout, null);
alert.SetView(view);
EditText editText_name = view.FindViewById<EditText>(Resource.Id.et_name);
EditText editText_pwd = view.FindViewById<EditText>(Resource.Id.et_pwd);
Button button1 = view.FindViewById<Button>(Resource.Id.button1);
Button button2 = view.FindViewById<Button>(Resource.Id.button2);
button1.Click += delegate {
Toast.MakeText(this,"press button1!",ToastLength.Short).Show();
};
button2.Click += delegate {
Toast.MakeText(this, "press button2!", ToastLength.Short).Show();
};
//alert.SetButton("OK", (c, ev) =>
//{
// // Ok button click task
// string name = editText_name.Text;
// string password = editText_pwd.Text;
// Toast.MakeText(this, "name = " + name + " password= " + password, ToastLength.Long).Show();
//});
//alert.SetButton2("CANCEL", (c, ev) => { });
alert.Show();
The input_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/et_name"
android:hint="please input name"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/et_pwd"
android:password="true"
android:hint="please input password"
/>
<LinearLayout
android:padding="20dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button1"
android:text="button1"
android:textColor="#android:color/white"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/defaultbutton"
/>
<Button
android:id="#+id/button2"
android:layout_marginLeft="20dp"
android:text="button2"
android:textColor="#android:color/white"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/defaultbutton"
/>
</LinearLayout>
define a xml(e.g. defaultbutton.xml) in folder drawable
<?xml version="1.0" encoding="utf-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#1E90FF" />
<!--<stroke
android:width="2dp"
android:color="#ffffff" />-->
<corners
android:bottomLeftRadius="20dp"
android:bottomRightRadius="20dp"
android:topLeftRadius="20dp"
android:topRightRadius="20dp" />
</shape>
Note:
1.define a xml( defaultbutton.xml) in folder drawable
2.use like this:
android:background="#drawable/defaultbutton"
The result is:

How to change the background color of a button that wasn't clicked?

My goal here is every time a button is clicked the Button background colors go back to their default color and the Button that is clicked changes color. I'm hoping to do this in JAVA, I think a for loop is the way to go but I'm not sure how to edit Button that wasn't clicked.
Here is my XML
<Button
android:id="#+id/but1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:background="#drawable/background_selector"
android:onClick="buttonOn"
android:text="Button 1" />
<Button
android:id="#+id/but2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:background="#drawable/background_selector"
android:onClick="buttonOn"
android:text="Button 2" />
<Button
android:id="#+id/but3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:background="#drawable/background_selector"
android:onClick="buttonOn"
android:text="Button 3" />
Here is my selector
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/background_blue"
android:state_pressed="true"/>
<item android:drawable="#drawable/background_blue"
android:state_focused="true"/>
<item android:drawable="#drawable/background_white"/>
</selector>
Here is background_white
<solid android:color="#android:color/white" />
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
</shape>
Here is background_blue
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#android:color/holo_blue_bright" />
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
</shape>
Here is my java code
public class MainActivity extends AppCompatActivity {
Button but1;
Button but2;
Button but3;
Button but4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
but1 = (Button) findViewById(R.id.but1);
but2 = (Button) findViewById(R.id.but2);
but3 = (Button) findViewById(R.id.but3);
but4 = (Button) findViewById(R.id.but4);
}
public void buttonOn(View v) {
Fragment view;
switch(v.getId()) {
case R.id.but1:
Log.i("Button 1", "pressed");
break;
case R.id.but2:
Log.i("Button 2", "pressed");
break;
case R.id.but3:
Log.i("Button 3", "pressed");
break;
case R.id.but4:
Log.i("Button 4", "pressed");
break;
}
}
Add background as a drawable selector to button.Also add android:focusableInTouchMode="true" to make button take touch when pressed.
<Button
android:id="#+id/but1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:background="#drawable/background_selector"
android:onClick="buttonOn"
android:text="Button 1" />
Then add two drawables for button selected and unselected inside background_selector
background_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/background_blue"
android:state_pressed="true"/>
<item android:drawable="#drawable/background_blue"
android:state_focused="true"/>
<item android:drawable="#drawable/background_white"/>
</selector>
And then add the background_white and background_blue to drawable folder
background_white.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#android:color/white" />
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
</shape>
background_blue.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#android:color/holo_blue_bright" />
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
</shape>
public void changeButtonColors(ViewGroup layout, int clickedButton_id) {
for (int i = 0; i < layout.getChildCount(); i++) {
View v = layout.getChildAt(i);
if (v instanceof Button) {
if (v.getId() == clickedButton_id){
((Button)v).setBackgroundColor(Color.RED);
}else{
((Button)v).setBackgroundColor(Color.BLUE);
}
}
}
}

Changing color of the border of the background of a list item

I was going through this tutorial and had the idea of making it so when you press a button, one of the cards borders changes color.
I looked at this solution, but nothing happens.
Here is what I have done:
I have an xml in drawable for the shape (feed_item.xml):
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Bottom 2dp Shadow -->
<item>
<shape android:shape="rectangle">
<solid android:color="#d8d8d8" />
<corners android:radius="7dp" />
</shape>
</item>
<!-- White Top color -->
<item android:bottom="3px" android:id="#+id/feedsquare">
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- view background color -->
<solid
android:color="#color/white" >
</solid>
<!-- view border color and width (I WANT TO CHANGE THIS) -->
<stroke
android:width="1dp"
android:color="#color/white" >
</stroke>
<!-- If you want to add some padding -->
<padding
android:left="4dp"
android:top="4dp"
android:right="4dp"
android:bottom="4dp" >
</padding>
</shape>
</item>
</layer-list>
Then I have a layout that is basically how each card/list item looks (list_row.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:background="#drawable/feed_item"
android:id="#+id/card"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:padding="2dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:id="#+id/title"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_width="wrap_content"
android:text="MMMMMMMMMM"
android:textSize="#dimen/name3"
android:textColor="#color/black" />
<!--android:scaleType="fitXY"-->
<ImageView android:id="#+id/list_image"
android:layout_height="180dp"
android:layout_margin="2dp"
android:layout_weight="0.04"
android:layout_width="match_parent"
android:scaleType="centerInside"
android:adjustViewBounds="true"
android:src="#mipmap/navigation_refresh"/>
<TextView android:id="#+id/description"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_width="wrap_content"
android:text="MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM"
android:textSize="#dimen/description3"
android:textColor="#color/black" />
</LinearLayout>
Now, I have a fragment with a listview. The fragment does exactly what the tutorial does (show 8 cards). I've added a button to the fragment that I want to change a specific cards border color (in the fragments onactivitycreated):
newpost = (Button) getActivity().findViewById(R.id.reply);
newpost.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LayerDrawable layers = (LayerDrawable) getActivity().getResources().getDrawable(R.drawable.feed_item);
GradientDrawable shape = (GradientDrawable) (layers.findDrawableByLayerId(R.id.feedsquare));
shape.setStroke(1 ,R.color.green);
}
});
Nothing happens when I press the button. Any ideas as to what I can do?
To show selected item (row) in listview :
create a variable in adapter class :
private int selectedPosition = 0;
Create method in side adapter :
public void setSelectedItem(int position) {
this.selectedPosition = position;
notifyDataSetChanged();
}
Add check in getView methode
if (position == selectedPosition)
{
convertView.setBackground(context.getResources().getDrawable(R.drawable.selector_rect_black_trans_bg));
}
else convertView.setBackgroundColor(context.getResources().getColor(android.R.color.transparent));
selector_rect_black_trans_bg.xml :
<solid android:color="#color/gray1" />
<stroke
android:width="2dp"
android:color="#color/gray6" />
<corners
android:bottomLeftRadius="0dp"
android:bottomRightRadius="0dp"
android:topLeftRadius="0dp"
android:topRightRadius="0dp" />
In Activity, only you need to pass position in adapter to show highlighted.
its tested code and it will work.

Android: Layer drawable loses information

I am messing around with creating my own dialog class, but I've run into an interesting issue. I use a layer-list drawable for the dialog's close button, but for some reason, the shadow layer disappears after the first time I show the dialog. You can see in the image below:
I really have absolutely no idea what could be causing this. I've tried debugging, but I wasn't able to find any leads. Does anyone have any ideas? My code is below.
TwoColorDialog.java
public class TwoColorDialog extends DialogFragment {
private static final String TAG = "TwoColorDialog";
private ViewGroup _root;
private TextView _textTitle;
private ImageView _buttonClose;
private Button _button1;
private Button _button2;
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Dialog dialog = new Dialog(getActivity());
_root = (ViewGroup) getActivity().getLayoutInflater().inflate(R.layout.two_color_dialog, null);
_textTitle = (TextView) _root.findViewById(R.id.dialog_title);
_buttonClose = (ImageView) _root.findViewById(R.id.dialog_close_button);
_button1 = (Button) _root.findViewById(R.id.dialog_button1);
_button2 = (Button) _root.findViewById(R.id.dialog_button2);
dialog.requestWindowFeature(STYLE_NO_TITLE);
View decorView = dialog.getWindow().getDecorView();
decorView.setBackgroundResource(getResources().getColor(android.R.color.transparent));
dialog.setContentView(_root);
_buttonClose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.cancel();
}
});
_button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d(TAG, "Button 1 pressed");
}
});
_button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d(TAG, "Button 2 pressed");
}
});
return dialog;
}
}
two_color_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout android:id="#+id/dialog_background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/dialog_background">
<LinearLayout android:id="#+id/dialog_title_spacing"
android:layout_width="match_parent"
android:layout_height="24dp"
android:orientation="horizontal" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<Button android:id="#+id/dialog_button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:background="#drawable/button"
android:text="Button1" />
<Button android:id="#+id/dialog_button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:background="#drawable/button"
android:text="Button2" />
</LinearLayout>
</LinearLayout>
<TextView android:id="#+id/dialog_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|top"
android:layout_marginTop="6dp"
android:background="#drawable/dialog_title_box"
android:textAppearance="#android:style/TextAppearance.Large"
android:text="TITLE" />
<ImageView android:id="#+id/dialog_close_button"
android:layout_width="42dp"
android:layout_height="44dp"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_gravity="top|right"
android:src="#drawable/dialog_close_button"/>
</FrameLayout>
dialog_title_box.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#88888888" />
<corners android:radius="1dp" />
<padding
android:left="1dp"
android:top="0dp"
android:right="1dp"
android:bottom="3dp" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="#color/white" />
<corners android:radius="1dp" />
<padding
android:left="15dp"
android:top="5dp"
android:right="15dp"
android:bottom="5dp" />
</shape>
</item>
</layer-list>
dialog_close_button.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid android:color="#88888888" />
<padding
android:left="1dp"
android:top="0dp"
android:right="1dp"
android:bottom="4dp" />
</shape>
</item>
<item>
<shape android:shape="oval">
<solid android:color="#color/white" />
</shape>
</item>
<item android:drawable="#drawable/ic_close" />
</layer-list>
Well, I've found the issue. The problem only shows up when you use the "padding" tag. I have no idea why, maybe it is a bug. I was able to work around it by changing my dialog_close_button.xml accordingly:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid android:color="#88888888" />
</shape>
</item>
<item
android:left="1dp"
android:right="1dp"
android:bottom="4dp">
<shape android:shape="oval">
<solid android:color="#color/white" />
</shape>
</item>
<item
android:drawable="#drawable/ic_close"
android:left="1dp"
android:right="1dp"
android:bottom="4dp" />
</layer-list>

Translating features from xml to java

Basically I'm trying to dynamically add buttons when I click another button
When I click the "Add a Class" button I would like the "Math button to appear above it like so: Link , the "Math" button is just an example of what it should look like, it is not programmed dynamically.
Currently, when I click the "Add a Class" button, this is what happens: link
I would like the dynamically added button in this case "New button" to look identical to "Math"
Here is my xml layout file:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#7BEDFC" >
<LinearLayout
android:id="#+id/classesLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Classes"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFFFFF"
android:textStyle="bold" />
<Button
android:id="#+id/bExample"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:layout_height="40dp"
android:layout_width="fill_parent"
android:background="#drawable/roundedcorners"
android:drawableRight="#drawable/rightarrow"
android:gravity="left|center_vertical"
android:text=" Math"
android:textStyle="bold" />
<Button
android:id="#+id/bClass"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/roundedcorners"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:text="Add a Class"
android:textStyle="bold" />
</LinearLayout>
Roundedcorners drawable file:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" >
<shape android:shape="rectangle" >
<corners android:radius="10dip" />
<stroke android:width="1dip" android:color="#FFFFFF" />
<gradient android:angle="-90" android:startColor="#FFFFFF"
android:endColor="#FFFFFF" />
</shape>
</item>
<item android:state_focused="true">
<shape android:shape="rectangle" >
<corners android:radius="10dip" />
<stroke android:width="1dip" android:color="#FFFFFF" />
<solid android:color="#FFFFFF"/>
</shape>
</item>
<item >
<shape android:shape="rectangle" >
<corners android:radius="10dip" />
<stroke android:width="1dip" android:color="#FFFFFF" />
<gradient android:angle="-90" android:startColor="#FFFFFF"
android:endColor="#FFFFFF" />
</shape>
</item>
</selector>
Relevant part of activity file:
public class MainActivity extends ActionBarActivity {
LinearLayout buttonsLayout;
Button addClass;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonsLayout = (LinearLayout) findViewById(R.id.classesLayout);
addClass = (Button) findViewById(R.id.bClass);
addClass.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Button newButton = new Button(MainActivity.this);
newButton.setBackgroundResource(R.drawable.roundedcorners);
newButton.setBackgroundResource(R.drawable.rightarrow);
newButton.setGravity(Gravity.CENTER_VERTICAL| Gravity.LEFT);
newButton.setText("New button");
buttonsLayout.addView(newButton, buttonsLayout.getChildCount() - 1);
}
});
}
}
Use newButton.setCompoundDrawables(0, 0, R.drawable.rightarrow, 0); instead of the second call to setBackgroundResource. If it doesn't look quite right or doesn't show the icon, you might also try setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.rightarrow, 0).
EDIT
You can still use XML for these buttons. Make an XML file (let's call it "my_button.xml") with the following:
<Button
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:layout_height="40dp"
android:layout_width="fill_parent"
android:background="#drawable/roundedcorners"
android:drawableRight="#drawable/rightarrow"
android:gravity="left|center_vertical"
android:textStyle="bold" />
Then you can write
addClass.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Button newButton = (Button) getLayoutInflater().inflate(R.layout.my_button, buttonsLayout, false);
newButton.setText("New button");
buttonsLayout.addView(newButton, buttonsLayout.getChildCount() - 1);
}
});

Categories

Resources