0

I have a style for all buttons declared in values->styles.xml, like this.

 <style name="BlueButton" parent="android:style/Widget.Button">
        <item name="android:background">@color/blue</item>
 </style>

and in the overall theme as

<style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:buttonStyle">@style/BlueButton</item>
</style>

I want to change the color of all buttons in my application, depending on what color the user chooses. Is there a way to define different styles in styles.xml and assign them to buttons dynamically.

Please note, i want to change the color of all the buttons in my application at a time and not one by one. For example, let's say, currently all my buttons are in black and when user chooses a theme of red, i want all the buttons in the application to have red background.

Vamsi Challa
  • 11,038
  • 31
  • 99
  • 149

3 Answers3

0
Hi try this code. 

          ThemeUtils class:

            public class ThemeUtils {
            private static int cTheme;
            public final static int RED = 0;
            public final static int GREEN = 1;

            public static void changeToTheme(Activity activity, int theme) {
                cTheme = theme;
                activity.finish();
                activity.startActivity(new Intent(activity, activity.getClass()));
            }

            public static void onActivityCreateSetTheme(Activity activity) {
                switch (cTheme) {
                case BLUE:
                    activity.setTheme(R.style.AppBaseTheme);
                    break;

                default:
                }
            }
        }

        And to set theme use this code:

                ThemeUtils.changeToTheme(this, ThemeUtils.BLUE);

        If you want to create custom theme in android see this link: 
         http://romannurik.github.io/AndroidAssetStudio/
Sudheesh Mohan
  • 2,560
  • 3
  • 21
  • 38
0

Try to use theming concept.

  • First create attribute for the button

<attr format="reference" name="button_style"/>

  • Then create a style as you want:

<style name="BlueButton" parent="android:style/Widget.Button"> <item name="android:background">@color/blue</item> </style>

  • And then apply to Theme:

<style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <item name="button_style">@style/BlueButton</item> </style>

  • Finally apply this to button in Layout xml

<Button style="?button_style" />

Then if you want to change other theme create same like (2 and 3 point) it will automatically apply to your button

0

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?

Anggrayudi H
  • 14,977
  • 11
  • 54
  • 87