GMAKER GMAKER - 1 month ago 31
Android Question

Shape And Selector is not working

In my android application want my button to be green and rounded rectangle and when someone press the button it should change color to grey. So I have created an XML file with selector and give it to shape of rounded rectangle but problem is when I start my application the color is not green by default its transparent. Moreover when I press the button it shows rectangle shape so I think shapre is also not working. This is my Primary color:

<color name="colorPrimary">#669900</color>


My custom_button.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<item android:state_focused="true" android:state_pressed="false" android:color="@color/colorPrimary" android:drawable="@color/colorPrimary">
<shape
android:shape="rectangle" android:padding="10dp">
<solid android:color="@color/colorPrimary"/>
<corners
android:bottomRightRadius="15dp"
android:bottomLeftRadius="15dp"
android:topLeftRadius="15dp"
android:topRightRadius="15dp"/>
</shape>
</item>
<item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/gradient" >
<shape
android:shape="rectangle" android:padding="10dp">
<corners
android:bottomRightRadius="15dp"
android:bottomLeftRadius="15dp"
android:topLeftRadius="15dp"
android:topRightRadius="15dp"/>
</shape>
</item>
<item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/gradient" >
<shape
android:shape="rectangle" android:padding="10dp">
<corners
android:bottomRightRadius="15dp"
android:bottomLeftRadius="15dp"
android:topLeftRadius="15dp"
android:topRightRadius="15dp"/>
</shape>
</item>
</selector>


Also I have made a XML file for gradient which is gradient.xml

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

<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:angle="90"
android:startColor="#880f0f10"
android:centerColor="#880d0d0f"
android:endColor="#885d5d5e"/>
</shape>
</item>
</layer-list>

Answer

To get your rectangles to be rounded, try something like the following code:

<?xml version="1.0" encoding="utf-8"?>
    <layer-list
        xmlns:android="http://schemas.android.com/apk/res/android">
        <item>
            <shape
                android:shape="rectangle">
                <corners
                    android:radius="5dp"/>
                <stroke
                    android:width="2dp"   
                    android:color="@color/border_of_rectangle"/>
                <solid
                    android:color="@color/color_inside_rectangle"/>
           </shape>
       </item>
   </layer-list>
Comments