Skip to content Skip to sidebar Skip to footer

Android Relativelayout Change Color Onclick

How do i change the color of a Relative Layout i use as a clickable on Click like the normal Button? Like i want a visual feedback the layout was pressed. I tried it with a selecto

Solution 1:

Try the following steps:

In res --> values folder create color.xml with the content:

<?xml version="1.0" encoding="utf-8"?><resources><colorname="black">#000000</color><colorname="white">#ffffff</color></resources>

As <item> tag in selector requires a drawable attribute or child tag defining a drawable, your layout_selector.xml file (which is saved in res --> drawable) should look like this:

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

Also, as said earlier, the relative layout should be clickable (android:clickable="true")

and its background set as android:background="@drawable/layout_selector"

Hope it helps

Solution 2:

Use selector on the android:background attribute of your RealtiveLayout. Also make the layout clickable (through android:clickable="true").

Solution 3:

Layouts are not displayed into the screen. They only may to conrain views. You shold add some View and then add onClick listener to that view.

Possible dublicate: Android clickable layout

Post a Comment for "Android Relativelayout Change Color Onclick"