Home » Android » android – Selector color on LinearLayout

android – Selector color on LinearLayout

Posted by: admin June 15, 2020 Leave a comment


I’m trying to assing a color selector to an extended class of LinearLayout, so, i think its like if we speak about linearLayout.

i followed the instructions on this post, the answer talking about shapes.

Now i have 3 xml on drawables folders:

normal.xml file

<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle" >
  <solid android:color="#ffffffff" />

pressed.xml file

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
   <solid android:color="#00000000" />

and finally, bg.xml file

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/pressed" />
    <item android:state_focused="true" android:drawable="@drawable/pressed" />
    <item android:state_selected="true" android:drawable="@drawable/pressed" />
    <item android:drawable="@drawable/normal" />

I am accessing this in the following way:

    Drawable d = getResources().getDrawable(context.getResources().getIdentifier("mypackageuri.tProject:drawable/bg", null, null));

The “normal” state its fine, with the color set at “normal.xml”, but no way with the other ones, I press my view and nothing happens, it’s not changing color in any way…

I can’t see what i’m doing wrong…

Thank you

How to&Answers:

Your view needs to be clickable in order to get the state pressed when you click on it.
Use :


or in the layout xml :