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.
To change the default Button style of the application we can use the android:buttonStyle attribute in the AppTheme style inside the styles. 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.
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.
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.
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);
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With