25

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 selector bound to the background property like this:

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

and used it in the Layouts backround...

android:background="@color/layout_selector"

but this gives me an Inflate Exception...

Any ideas?

whostolemyhat
  • 3,107
  • 4
  • 34
  • 50
GuyFawkes
  • 1,686
  • 3
  • 17
  • 30

4 Answers4

52

Try the following steps:

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

<?xml version="1.0" encoding="utf-8"?>
<resources>     
    <color name="black">#000000</color> 
    <color name="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"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android">   
    <item android:state_focused="true" android:drawable="@color/black"/> 
    <item android:state_pressed="true" android:state_enabled="false" android:drawable="@color/black" />
    <item android: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

AVirvara
  • 565
  • 5
  • 7
  • 2
    Excellent. This works like a charm. Problem in fact was it had to be a drawable resource and not in the android:color List. – GuyFawkes Apr 26 '11 at 14:06
  • @AVirvara : i have done exactly the same. When der is no focus on it .it shows white background but when i click on it, it doesn't switch it to black i don't know why? I have set all the properties. Any suggestion is appreciated. – Kushal Shah May 23 '12 at 06:48
  • Like GuyFawkes said, using `android:color` in the selector will give you a phat exception when you assign the selector to the layout with `android:background` – Someone Somewhere Dec 05 '14 at 01:31
  • thanx a lot. after remove of android:state_enabled="false".its working fine – madhu527 Feb 25 '15 at 11:21
  • In my case it only worked after removing `android:state_enabled="false"`, and it also worked with and without `android:clickable="true"` in the RelativeLayout – Mohamed Khamis Mar 25 '15 at 14:33
10

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

Saro Taşciyan
  • 5,210
  • 5
  • 31
  • 50
Kocus
  • 1,613
  • 17
  • 31
  • i tried as you suggested but it wont work. edited post accordingly. – GuyFawkes Apr 26 '11 at 11:56
  • Where did you put the selector file? If you put them in /drawable/ use @drawable/layout_selector – Kocus Apr 26 '11 at 12:08
  • i copied the file in the drawable and used it as you suggested but still the same error... `Unable to start activity ComponentInfo{com.app/com.views.MainView}: android.view.InflateException: Binary XML file line #50: Error inflating class ` – GuyFawkes Apr 26 '11 at 12:18
  • For unknown to me reasons, the `android:background` cannot use Color Selector - it must be Drawable Selector (it means that you should replace `android:color` tags with `android:drawable`, and put into drawables some 1x1 px black / white pngs. Not the nicest solution, but... – Kocus Apr 26 '11 at 12:40
  • AFAIK, the background resorce MUST BE drawable, not the color list (check http://stackoverflow.com/questions/3953606/android-trouble-with-color-state-list-resources-how-to-specify-a-background-col) – Kocus Apr 26 '11 at 12:45
0

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

Community
  • 1
  • 1
Anton Derevyanko
  • 3,405
  • 1
  • 24
  • 32
0

put this layout_selector.xml in drawable folder ie(res>drawable>layout_selector.xml) then set android:background="@drawable/layout_selector" instead android:background="@color/layout_selector"