5

How do you programmatically add/remove style to an android button? Is it possible to apply the styling at runtime?

I have two buttons that look like these

     ----------   ----------
    | Button A | | Button B |
     ----------   ----------

what i wanted to do is when a button is clicked (lets say Button B), it runs some code, then changes the style of button B to something else (i.e highlighted borders) and will be something like this:

     ----------    ==========
    | Button A | || Button B ||
     ----------    ==========

I know how to do the styling(i.e create the style) in XML, all I want to know is how to apply the styles on runtime/using java code.

Cœur
  • 37,241
  • 25
  • 195
  • 267
ibaguio
  • 2,298
  • 3
  • 20
  • 32
  • 1
    Do you mean "programatically"? – Code-Apprentice Jan 17 '13 at 19:28
  • Anything you can set by xml can be set at runtime in Java code. Generally there's a setXXX function, where XXX is the thing you want to change. Look on the Button class and its ancestors to find it. – Gabe Sechan Jan 17 '13 at 19:28
  • All layout attributes in an XML file have corresponding getter and setter functions in Java. You should check out the Android SDK docs to find the exact ones you want. If you need help finding these, please post a snippet of the XML style that you wish to change the button to. – Code-Apprentice Jan 17 '13 at 19:30
  • im currently trying something now, im not sure if it works. @Code-Guru yes i meant programatically, i appologize for that – ibaguio Jan 17 '13 at 19:32

3 Answers3

12

Let's do some code for you case...:) For applying style to your view (button in this case) dynamically is you have to do the following in your layout folder (res/layout).

I named it as,buttonstyle.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true">
        <shape>
            <solid android:color="#449def"/>
            <stroke android:width="1dp" android:color="#2f6699"/>
            <corners android:radius="3dp"/>
            <padding android:left="10dp" android:top="10dp" android:right="10dp"
                     android:bottom="10dp"/>
        </shape>
    </item>

    <item>
        <shape>
            <gradient android:startColor="#449def" android:endColor="#2f6699" android:angle="270"/>
            <stroke android:width="1dp" android:color="#2f6699"/>
            <corners android:radius="4dp"/>
            <padding android:left="10dp" android:top="10dp" android:right="10dp"
                     android:bottom="10dp"/>
        </shape>
    </item>

</selector>

Now apply style to your button, add the following code to onCreate() method of your activity..

Button transferBtn = new Button(this);
transferBtn.setText("Test Example");
transferBtn.setId(R.string.transferBtn);
transferBtn.setBackgroundResource(R.layout.buttonstyle);
kartikag01
  • 1,539
  • 1
  • 18
  • 36
ridoy
  • 6,274
  • 2
  • 29
  • 60
  • I found it necessary to place the **buttonstyle.xml** file in the **res/drawable** directory. (note I had to create the directory first) – Sydwell Jul 08 '14 at 15:14
3

You can't apply xml-defined styles in runtime (from code). If you want to change background and font style when button is clicked (pressed) you should create selector which defines what background to use for normal button or for clicked state.

If selector is not what you want, you should manually set every button property to desired value via button's setXXX method of Button class.

P.S. You can swap old button for a new another one inflated from xml with different style. But this is not a good way I suppose...

Community
  • 1
  • 1
Leonidos
  • 10,482
  • 2
  • 28
  • 37
  • yeah i found a selector XML and modified the code, i used setBackgroundResource(...) to update it. thanks! – ibaguio Jan 17 '13 at 19:49
0

As I answered in this other thread, you could either set the background programatically as some folks suggest, or you could set the style programatically (as I suggest here) if you are using the support library.

Community
  • 1
  • 1
cesards
  • 15,882
  • 11
  • 70
  • 65