How to set Style for a button in Android?

谁都会走 提交于 2019-11-29 01:22:51

It would be rather simple if you do it this way.

First create a button_selector.xml in drawable folder.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" >
 <shape android:shape="rectangle"  >
     <corners android:radius="5dp" />
     <stroke android:width="1dip" android:color="@color/green_temp" />
     <gradient android:angle="-90" android:startColor="@color/green_temp" android:endColor="@color/green_temp"  />            
 </shape>
</item>
<item android:state_focused="true">
 <shape android:shape="rectangle"  >
     <corners android:radius="5dp" />
     <stroke android:width="1dip" android:color="#971E05" />
     <solid android:color="#58857e"/>       
 </shape>
</item>  
<item >
<shape android:shape="rectangle"  >
     <corners android:radius="5dp" />
     <stroke android:width="1dip" android:color="@color/bright_green" />
     <gradient android:angle="-90" android:startColor="@color/green_temp" android:endColor="@color/button_green" />            
 </shape>
</item>
</selector>

Add these colors in colors.xml of values folder.

<!-- Green -->
<color name="green_temp">#23A96E</color>
<color name="green_dark">#159204</color>
<color name="bright_green">#02D8B0</color>
<color name="button_green">#10a54a</color>

Finally in your desired button of layout.xml put background from above selector.

<Button
    android:id="@+id/btnAddTitle"
    android:layout_below="@id/edEnterTitleValue"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/btn_AddTitle"
    android:background="@drawable/button_selector"
    android:layout_margin="20dp"
/>

Then its all done your button will be styled with color you want.

Android Button Maker is online tool to generate buttons code for Android Apps. Android API provide Drawable Resources where XML file defines geometric shape, including colors, border and gradients. These button is generating based on shape drawable XML code which load faster compare to normal png buttons. You can customize button properties in setting panel and get source code.

Check this link Button Maker

No need to crack brains...this tool make it simple

behnamizadi

replace

android:background="@style/ButtonAppTheme"

with

style="@style/ButtonAppTheme"

and every thing is ok!

Try this out as alternative. create a drawable folder under res then create xml files and copypaste below code and try first then you customize as per your requirement.

button_bg.xml

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

<solid android:color="#00acc1"/>
<stroke android:width="2dp" android:color="#ffffff"/>
<corners android:radius="2dp"/> </shape>

button_bg_pressed.xml

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

<solid android:color="#006064"/>
<stroke android:width="1dp" android:color="#ffffff"/>
<corners android:radius="2dp"/> </shape>

button_selector.xml

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

<item android:state_focused="true" android:state_pressed="true" 
        android:drawable="@drawable/button_bg_pressed" /> 
<item android:state_focused="false" android:state_pressed="true" 
        android:drawable="@drawable/button_bg_pressed" /> 
<item android:drawable="@drawable/button_bg" /> </selector>

And now in your button xml set the background as button_selector.xml

<Button
    android:id="@+id/btnAddTitle"
    android:layout_below="@id/edEnterTitleValue"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/btn_AddTitle"
    android:background="@drawable/button_selector"
    android:layout_margin="20dp"/>

This will do the job for you. You can customize your entire button style by this way.

If you want to have the default animation of button by material design and change button color, try this.

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="@dimen/padding_12"
    android:text="button text here"
    android:theme="@style/NavyBtn" />

in style.xml

<style name="NavyBtn" parent="Theme.AppCompat.Light">
    <item name="colorButtonNormal">#020e39</item>
    <item name="android:textColor">#FFFFFF</item>
</style>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!