There are different ways to apply styles to a Button. You can apply the styles to the Button using xml file or using Java code. If styles of the Button can be changed at run-time, applying the style programmatically is the better choice.
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:textSize="18sp"
android:textColor="@drawable/text_color"
/>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<drawable name="bt_normal_color">#08088A</drawable>
<drawable name="bt_press_color">#00BFFF</drawable>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape>
<gradient android:angle="90" android:startColor="@drawable/bt_press_color" android:endColor="@drawable/bt_normal_color" />
<padding android:bottom="5dp" android:left="5dp" android:right="5dp" android:top="5dp" />
<corners android:radius="10dp"/>
</shape>
<shape>
<gradient android:angle="90" android:startColor="@drawable/bt_normal_color" android:endColor="@drawable/bt_press_color"/>
<padding android:bottom="5dp" android:left="5dp" android:right="5dp" android:top="5dp" />
<corners android:radius="10dp"/>
</shape>
Button bt=(Button)findViewById(R.id.button);
bt.setTextColor(Color.parseColor("#D8D8D8"));
bt.setBackgroundResource( R.drawable.bt_style);
|
This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.