Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set button selection color along with rounded corners in android?

I want to set a rounded corner for a button in android along with changing the button color on when selected. I am doing the following things.

drawable/push_button.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >    
    <item android:state_pressed="true"  android:drawable="@color/green"/>
    <item android:state_focused="true"  android:drawable="@color/green"/>
    <item android:state_focused="false"  android:drawable="@color/green"/>
    <item android:state_pressed="false" android:drawable="@color/red"/>
    <item  android:drawable="@drawable/push_button_background"/>         
</selector>

drawable/push_button_background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    >
    <solid android:color="#3F4040"/>
    <corners 
    android:radius="7dp"
    />
</shape>

and in code, I am using

android:background="@drawable/push_button"

Here, the problem is, button colors are setting properly when selected & deselected. But, rounded corners are not working.

How to do that? If I use

android:background="@drawable/push_button_background"

then, rounded corners are working but the button color change on selection is not working

How to implement this?

I have referred this link. Even then no help!!

like image 899
Bharath Avatar asked Sep 14 '12 15:09

Bharath


People also ask

How do you round fabric buttons?

With the Material Components Library:. Use app:cornerRadius attribute to change the size of corner radius. This will round off the corners with specified dimensions. You can also customize the corners using the shapeAppearanceOverlay attribute.

How can change background color of button in android?

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.

How do you set a button shape?

We can set custom shapes on our button using the xml tag <shape> . These xml files are created in the drawable folder too. shape can be used inside selectors . The shape can be set to rectangle (default), oval , ring , line .


2 Answers

I have found answer for my question with few trial & error attempts.

Here is the solution.

<?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="@color/green"/>
    <corners 
    android:radius="7dp"/>
    </shape>
 </item>

 <item android:state_focused="true" >
    <shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="@color/green"/>
    <corners 
    android:radius="7dp"/>
    </shape>
 </item>

 <item android:state_focused="false" >
    <shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="@color/red"/>
    <corners 
    android:radius="7dp"/>
    </shape>   
 </item>

 <item android:state_pressed="false" >
    <shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="@color/red"/>
    <corners 
    android:radius="7dp"
    />
    </shape>
 </item> 

</selector>
like image 139
Bharath Avatar answered Oct 02 '22 13:10

Bharath


What I did was defined the shape and specify the dp of each corner.

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="90dp">
<solid android:color="#FFFFFF"/>
<padding />
<corners
    android:bottomRightRadius="15dp"
    android:bottomLeftRadius="15dp"
    android:topLeftRadius="15dp"
    android:topRightRadius="15dp"/>

</shape>

If you increase the dp in each corner it will make the button more rounded.

like image 42
zabawaba99 Avatar answered Oct 02 '22 13:10

zabawaba99