Skip to content Skip to sidebar Skip to footer

Button With Selector For Background Color - When Clicked The Background Color Is Extending Out Of The Button

To begin with.. the button is added dynamically through code and hence cannot apply the styles in xml. I have few buttons in my activity and i am using a selector to change the bac

Solution 1:

This should be your selector:

<?xml version="1.0" encoding="utf-8"?><selectorxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:drawable="@drawable/image_border_blue"android:state_pressed="true"/><itemandroid:drawable="@drawable/image_border_dark_grey"android:state_focused="true"/><itemandroid:drawable="@drawable/image_border"/></selector>

And just add these drawables:

image_border_blue

<?xml version="1.0" encoding="UTF-8"?><shapexmlns:android="http://schemas.android.com/apk/res/android" ><solidandroid:color="@color/blue" /><strokeandroid:width="4dp"android:color="@color/light_grey" /><paddingandroid:bottom="4dp"android:left="4dp"android:right="4dp"android:top="4dp" /></shape>

image_border_dark_grey

<?xml version="1.0" encoding="UTF-8"?><shapexmlns:android="http://schemas.android.com/apk/res/android" ><solidandroid:color="@color/dark_grey" /><strokeandroid:width="4dp"android:color="@color/light_grey" /><paddingandroid:bottom="4dp"android:left="4dp"android:right="4dp"android:top="4dp" /></shape>

Solution 2:

Create another shape using those color you used in selector...suppose for Blue color...create shape named btn_pressed.xml...

<?xml version="1.0" encoding="UTF-8"?><shapexmlns:android="http://schemas.android.com/apk/res/android" ><solidandroid:color="@color/blue" /><strokeandroid:width="4dp"android:color="@color/blue" /><paddingandroid:bottom="4dp"android:left="4dp"android:right="4dp"android:top="4dp" /></shape>

Then place this shape in place of @color/blue in the selector as follows...

<?xml version="1.0" encoding="utf-8"?><selectorxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:drawable="@drawable/btn_pressed"android:state_pressed="true"/><itemandroid:drawable="@color/dark_grey"android:state_focused="true"/><itemandroid:drawable="@drawable/image_border"/></selector>

Follow same process for @color/dark_grey.

Post a Comment for "Button With Selector For Background Color - When Clicked The Background Color Is Extending Out Of The Button"