Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Button Styling Programmatically

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.

like image 565
ibaguio Avatar asked Jan 17 '13 19:01

ibaguio


People also ask

How to give style to button in android?

To change the default Button style of the application we can use the android:buttonStyle attribute in the AppTheme style inside the styles. xml.

How to create a button in XML?

To define the click event handler for a button, add the android:onClick attribute to the <Button> element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event. The Activity hosting the layout must then implement the corresponding method.

How can change button background drawable programmatically in Android?

setBackgroundResource() method is used to change the button background programmatically. setBackgroundResource(int id) accepts id of drawable resource and applies the background to the button.

How to set background to button in android?

To set Android Button background color, we can assign android:backgroundTint XML attribute for Button in layout file with the required Color Value. To programmatically set or change Android Button background color, we may call pass the method Button.


2 Answers

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);
like image 133
ridoy Avatar answered Oct 03 '22 02:10

ridoy


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...

like image 35
Leonidos Avatar answered Oct 03 '22 04:10

Leonidos