How to set rounded buttons with background color and change color on pressed

旧城冷巷雨未停 提交于 2019-12-04 16:34:43

Make two different shape xml. one with green color and other using another color..

And use them in selector.xml

 <item android:drawable="@drawable/rounded_btn_green" android:state_selected="true"/>

<item android:drawable="@drawable/rounded_btn_green" android:state_pressed="true"/>

<item android:drawable="@drawable/rounded_btn_green" android:state_focused="true"/>

<item android:drawable="@drawable/rounded_btn"/>

You have to Create 3 xml files for that...

2 for Drawable Shapesand 1 for Drawable Selector

See Below Code..

button_normal.xml

<?xml version="1.0" encoding="utf-8"?>

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:left="2dp" android:top="2dp">
        <shape>
            <corners android:radius="10dp" />
            <solid android:color="#22151515" />
        </shape>
    </item>

    <item android:bottom="3dp" android:right="3dp">
        <shape android:shape="rectangle">
            <solid android:color="@color/white"/>
            <corners android:radius="10dp" />
            <padding android:left="10dp"
                android:right="10dp"/>
            <stroke android:width="3px" 
                android:color="@color/border_pink" />
        </shape>
    </item>

</layer-list>

button_selected.xml

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

    <item android:left="2dp" android:top="2dp">
        <shape>
            <corners android:radius="10dp" />
            <solid android:color="#22151515" />
        </shape>
    </item>

    <item android:bottom="3dp" android:right="3dp">
        <shape android:shape="rectangle">
            <solid android:color="#55fff000"/>
            <corners android:radius="10dp" />
            <padding android:left="10dp"
                android:right="10dp"/>
            <stroke android:width="3px" 
                android:color="@color/border_pink" />
        </shape>
    </item>

</layer-list>

button_bg.xml

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

    <item 
        android:state_focused="true"
        android:drawable="@drawable/button_selected"/>

    <item
        android:state_pressed="true"
        android:drawable="@drawable/button_selected"/>

    <item android:drawable="@drawable/button_normal"/>

</selector>

and finally....

android:background="@drawable/button_bg"

change code for drawable shapes as your need..

this may help you

Follow the below steps-

  1. Define Button view in your main xml like this-

    <Button
    android:id="@+id/search_bt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/button_selector_green"
    android:text="Search"
    android:textColor="@drawable/button_text_color_green"
    />
    
  2. Create button_selector_green xml file in your drawable folder like this-

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/rounded_corner_transparent" android:state_pressed="true"/>
    <!-- pressed -->
    <item android:drawable="@drawable/rounded_corner_green"/>
    <!-- default -->
    </selector>
    
  3. Create button_text_color_green xml file in your drawable folder like this-

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
       android:color="#48b28a" /> <!-- pressed -->
    <item android:state_focused="true"
       android:color="#FFFFFF" /> <!-- focused -->
    <item android:color="#FFFFFF" /> <!-- default -->
    </selector>
    
  4. Create rounded_corner_transparent xml file in your drawable folder like this-

     <?xml version="1.0" encoding="utf-8"?>
     <shape xmlns:android="http://schemas.android.com/apk/res/android"
     android:shape="rectangle">
    
     <solid 
      android:color="@android:color/transparent" >
     </solid>
    
     <!-- view border color and width -->
     <stroke
       android:width="1dp"
       android:color="#2b8c68" ></stroke>
    
     <!-- If you want to add some padding -->
     <padding
       android:left="4dp"
       android:top="4dp"
       android:right="4dp"
       android:bottom="4dp"    >
     </padding>
    
     <!-- Here is the corner radius -->
     <corners android:radius="10dp"   >
     </corners>
     </shape>
    
  5. Create rounded_corner_green xml file in your drawable folder like this-

    <?xml version="1.0" encoding="utf-8"?>
    <shape  xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
    
    <solid
    android:color="#48b28a" >
    </solid>
    
    <!-- view border color and width -->
    <stroke
    android:width="1dp"
    android:color="#2b8c68" >
    </stroke>
    
    <!-- If you want to add some padding -->
    <padding
    android:left="4dp"
    android:top="4dp"
    android:right="4dp"
    android:bottom="4dp"    >
    </padding>
    
    <!-- Here is the corner radius -->
    <corners
    android:radius="10dp"   >
    </corners>
    </shape>
    

    Hope this will work for you. Happy Coding :)

Sreerag S Kumar

Adding a click-effect on a custom button with padded corners

Please refer my answer in the above link.

We can acheive it by adding only one file in the drawable folder and refer that as background for the button.

Hope this will help somebody. One can use my solution and reduce the objects count when there is a concern for performance issue.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!