1

I am using a linear layout and setting a drawable xml as background. This background should change on user click. This is the code and the drawable xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/user_row_layout"
android:background="@drawable/row_background"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/user_row_textlayout"
    android:background="@drawable/row_background"
    android:layout_weight="0.5"
    android:orientation="vertical"
    android:layout_gravity="left"
    android:padding="5dp"
    android:paddingLeft="10dip" >

    <TextView
        android:id="@+id/displayName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:textColor="@drawable/tab_text_selector"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/user_speciality_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/displayName"
        android:textColor="@drawable/tab_text_selector" />
</RelativeLayout>

    <ImageView
        android:id="@+id/call_image_button"
        android:layout_width="100dip"
        android:layout_height="wrap_content"
        android:layout_weight="0.25"
        android:src="@drawable/user_list_call_button"
        android:contentDescription="@string/call_contact" />

</LinearLayout>

Here is row_background

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

However, if I put the same "@drawable/row_background" inside just on element (ex:Relative Layout), it works like a charm. But I want the background to change for the entire row. The background doesnt change even for the relative layout part when I put it outside. Is there something obvious I missed? Also the images I use are transparent images. Thank you for your time.

2 Answers2

0

Thank you all for your time and inputs. I removed the nested layout and put the entire code in one relative layout to make it work. However, I would like to know (for general benefit of the human kind) about some of the issues with nested layouts. Is it that something like LinearLayout which is a 'ViewGroup' does not affect a child viewgroup (RelativeLayout in this case) same as a child 'View'?

  • No, that's not the case. I've been trying to reproduce your issue using all kinds of nested layouts but I couldn't, it works for me with all of them. I used API 16, which version do you use? – Andrii Chernenko Dec 07 '12 at 08:02
  • Another important thing to note is that the relativeLayout and Imageview here have separate onClick events. – user1872534 Dec 07 '12 at 09:35
-1

You can try this code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/user_row_layout"
android:background="@color/white"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/user_row_textlayout"
android:background="@color/white"
android:layout_weight="0.5"
android:orientation="vertical"
android:layout_gravity="left"
android:padding="5dp"
android:paddingLeft="10dip" >

<TextView
    android:id="@+id/displayName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:textColor="@drawable/tab_text_selector"
    android:textStyle="bold" />

<TextView
    android:id="@+id/user_speciality_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@id/displayName"
    android:textColor="@drawable/tab_text_selector" />
</RelativeLayout>

<ImageView
    android:id="@+id/call_image_button"
    android:layout_width="100dip"
    android:layout_height="wrap_content"
    android:layout_weight="0.25"
    android:src="@drawable/user_list_call_button"
    android:contentDescription="@string/call_contact" />

</LinearLayout>

Then, in your java file, you can set an onClickListener to the relative and the linear layout

private OnClickListener relative = new OnClickListener() {

    @Override
    public void onClick(View v) {
        //change color
    }

};

private OnClickListener Layout = new OnClickListener() {

    @Override
    public void onClick(View v) {
                //change color
    }
};
Ankush
  • 6,767
  • 8
  • 30
  • 42
  • Question is that why selector xml is not working for LinearLayout not a question for setting white color in LinearLayout – rajpara Dec 06 '12 at 10:48
  • I would like to change the color of background dynamically (which is why I pointed to a drawable resource). This would set a constant color. – user1872534 Dec 06 '12 at 10:49
  • It would make life little complicated... **while** clicked the color should change.. not on/after clicking... – user1872534 Dec 06 '12 at 10:56
  • check out this link: http://stackoverflow.com/questions/5789661/android-relativelayout-change-color-onclick – Ankush Dec 06 '12 at 11:25
  • @user1696863: the link speaks about changing color of layout on click. As I said, this code works when I make the the child relative layout point to the drawable (but only for that layout. I want the color of the whole row to change). – user1872534 Dec 07 '12 at 07:39