Android Programmatically apply style to your view

Applying style to your view (button in this case) dynamically is pretty easy.
All you have to do is place the following in your layout folder (res/layout)

Let's call this file : buttonstyle.xml

<?xml version="1.0" encoding="utf-8"?> <
selector xmlns:android="">
<item android:state_pressed="true" >
<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" />
<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" />

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

Button transferBtn = new Button(this);

That should be it!


Popular posts from this blog

ionic2 cordova build android - Unable resolve gradle 2.2.3

A quick tutorial for OWASP ZAP tool for beginners