You can change the button background programmatically. And yes, call this method only once in every Activity
such as you declare a theme for an Activity
in manifest.xml
. One color
or drawable
resource is applied for all button in your Activity
. So, follow my steps:
Create a new third party class
Here, I named this class with ButtonBackground
:
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.widget.Button;
public class ButtonBackground {
/* This method is used to change the background with "drawable" resource. Maximum
* button to change its background is 5, no more. It caused by button's parameter
* only available for 5. You can add new parameter (button6, button 7, etc) with "int" type if needed.
*/
public void setButtonBackgroundDrawable(Context context, Activity activity, int drawable,
int button1, int button2, int button3, int button4, int button5) {
Drawable backgroundDrawable = context.getResources()
.getDrawable(drawable);
Button a = (Button) activity.findViewById(button1);
Button b = (Button) activity.findViewById(button2);
Button c = (Button) activity.findViewById(button3);
Button d = (Button) activity.findViewById(button4);
Button e = (Button) activity.findViewById(button5);
if (button1 != 0) {
a.setBackground(backgroundDrawable);
}
if (button2 != 0) {
b.setBackground(backgroundDrawable);
}
if (button3 != 0) {
c.setBackground(backgroundDrawable);
}
if (button4 != 0) {
d.setBackground(backgroundDrawable);
}
if (button5 != 0) {
e.setBackground(backgroundDrawable);
}
}
/* This method is used to change the background with "color" resource. Maximum
* button to change its background is 5, no more. It caused by button's parameter
* only available for 5. You can add new parameter (button6, button 7, etc) with "int" type if needed.
*/
public void setButtonBackgroundColor(Context context, Activity activity, int color,
int button1, int button2, int button3, int button4, int button5) {
int backgroundColor = context.getResources().getColor(color);
Button a = (Button) activity.findViewById(button1);
Button b = (Button) activity.findViewById(button2);
Button c = (Button) activity.findViewById(button3);
Button d = (Button) activity.findViewById(button4);
Button e = (Button) activity.findViewById(button5);
if (button1 != 0) {
a.setBackgroundColor(backgroundColor);
}
if (button2 != 0) {
b.setBackgroundColor(backgroundColor);
}
if (button3 != 0) {
c.setBackgroundColor(backgroundColor);
}
if (button4 != 0) {
d.setBackgroundColor(backgroundColor);
}
if (button5 != 0) {
e.setBackgroundColor(backgroundColor);
}
}
}
How to use?
For example, I have 3 buttons and I want to change its background using drawable
resource. They are buttonId1
, buttonId2
and buttonId3
. My drawable
's name is background_button_drawable.xml
, and my current Activity
named MainActivity
. So I write the code below inside onCreate()
method:
ButtonBackground buttonBackground = new ButtonBackground();
buttonBackground.setButtonBackgroundDrawable(getApplicationContext(), MainActivity.this,
R.drawable.background_button_drawable, R.id.buttonId1, R.id.buttonId2, R.id.buttonId3, 0, 0);
Note:
There are two 0
on the code above. It means button4
and button5
are empty in the parameter. So give 0
value if a button is empty or excess parameter.
What if I have 6 buttons?
You need one parameter again and must add a new parameter (button6
). The parameter must int
type:
public void setButtonBackgroundDrawable(... , int button6) {
And add it inside:
Button f = (Button) activity.findViewById(button6);
if (button6 != 0) {
f.setBackground(backgroundDrawable);
}
So when you use it:
ButtonBackground buttonBackground = new ButtonBackground();
buttonBackground.setButtonBackgroundDrawable(getApplicationContext(), MainActivity.this,
R.drawable.background_button_drawable, R.id.buttonId1, R.id.buttonId2, R.id.buttonId3, buttonId4, buttonId5, buttonId6);
What about change its background using color
resource?
You need to change setButtonBackgroundDrawable()
method with setButtonBackgroundColor()
. It will looks like this:
ButtonBackground buttonBackground = new ButtonBackground();
buttonBackground.setButtonBackgroundColor(getApplicationContext(), MainActivity.this,
R.color.background_button_blue, R.id.buttonId1, R.id.buttonId2, R.id.buttonId3, 0, 0);
It's very simple to use, isn't?