Changing Linear layout background color while clicking

android

Solution

Define background.xml in drawable folder

<?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="false" 
    android:drawable="@drawable/normal" />
</selector>

normal.xml in drawable folder

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
<solid android:color="#FFFFFF"/>    
</shape>

pressed.xml in drawable folder

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
<solid android:color="#FF1A47"/>      
</shape>

Then set background to your layout

  android:background="@drawable/background"

You can also set the background as below

On Touch of your layout

  ll.setOnTouchListener( new View.OnTouchListener()
    {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            switch(event.getAction())
            {
        case MotionEvent.ACTION_DOWN:
            ll.setBackgroundColor(Color.RED);
            break;
            case MotionEvent.ACTION_UP:

            //set color back to default
            ll.setBackgroundColor(Color.WHITE);  
            break;
            }
            return true;        
        }
    });

Problem

I am having a linear layout in my xml and am adding another linear layout(containing two textviews) to that linear layout through java. Touch events works perfect, but i want to highlight the selected linear layout by setting background color. Please advice.

Original source