Round button angles and color change for android button

I want to use the following functions in buttons in an android button:

  • Buttons should change colors when pressed

  • Some specific buttons have features such as the background color of the button, which must remain changed until we press another button.

+4
source share
2 answers

For rounded corners, you can use the code below:

 @drawable/rounderd_corner.xml

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

<stroke
    android:width="1dp"
    android:color="@color/YOUR_COLOR" />

<corners
    android:bottomLeftRadius="8dp"
    android:bottomRightRadius="8dp"
    android:topLeftRadius="8dp"
    android:topRightRadius="8dp" />

And for select buttons use below @ drawable / select_button_color.xml

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

<corners
    android:bottomLeftRadius="8dp"
    android:bottomRightRadius="8dp"
    android:topLeftRadius="8dp"
    android:topRightRadius="8dp" />

And change the background resource when the button is pressed

+1
source

For the circular and for changing the color with the selected / not selected button, you can use the following code fragment

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="false">
        <shape android:shape="oval">
            <solid android:color="@color/colorPrimary"/>
        </shape>
    </item>
    <item android:state_pressed="true">
        <shape android:shape="oval">
            <solid android:color="@color/colorPrimaryDark"/>
        </shape>
    </item>
</selector>

, , .

0

Source: https://habr.com/ru/post/1680464/


All Articles