Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Propagate parent state to child view

Tags:

android

I am experiencing some problem that I basically dont know how to fix. I have a LinearLayout with some child compenents in it, like ImageView, TextView and so on, one example can be seen in the xml below:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" 
android:layout_width="fill_parent"
android:layout_height="wrap_content" 
style="@style/list_buttom_top">

<LinearLayout android:gravity="center_vertical"
    android:paddingLeft="10dip" 
    android:orientation="vertical"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:layout_weight="1.0">

    <TextView android:text="title" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:id="@+id/title"
        style="@style/content_page_large_text" />

    <TextView android:text="summary" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:id="@+id/subtitle"
        android:visibility="visible" 
        style="@style/content_page_small_text" />
</LinearLayout>

<TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:id="@+id/itemCount"
    android:visibility="gone" 
    style="@style/content_page_large_count_text" />

<ImageView android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:id="@+id/chevron"
    style="@style/list_buttom_chevron" />

</LinearLayout> 

The style list_buttom_top says that when the LinearLayout is in the pressed state, the background color needs to be changed, and so far this is working. The problem is that the content_page_large_text also says that the text color of the TextView needs to be changed as well, but it doesnt happen. The only color changing when I am clicking the LinearLayout is its background.

<style name="list_buttom_top">
<item name="android:clickable">true</item>
<item name="android:paddingTop">10dip</item>
<item name="android:paddingBottom">10dip</item>
<item name="android:paddingLeft">10dip</item>
<item name="android:paddingRight">10dip</item>
<item name="android:gravity">center_vertical</item>
<item name="android:background">@drawable/background_view_rounded_top</item>
</style>

<style name="list_buttom_chevron">
    <item name="android:paddingLeft">4.0dip</item>
    <item name="android:paddingTop">2.0dip</item>
    <item name="android:paddingRight">4.0dip</item>
    <item name="android:paddingBottom">2.0dip</item>
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:src">@drawable/chevron</item>
</style>    

I have tried using this android:duplicateParentState="true" in my TextViews but it also didnt work. The same happens for the ImageView, which does not get its source image changed when I am clicking the LinearLayout.

Am I missing something here? Thanks for the attention T

like image 821
Thiago Avatar asked Sep 19 '11 01:09

Thiago


2 Answers

I think that you should set the android:addStatesFromChildren="true" in the linear layout, this way the children's state is propagated to the parent.

Also, make sure NOT to combine this with android:duplicateParentState="true" since it will cause a stack overflow exception (basically causing an infinite reference loop).

like image 147
Yoel Gluschnaider Avatar answered Nov 06 '22 13:11

Yoel Gluschnaider


It would be an easier issue to track if we could see your content_page_large_text style. Also, I don't see how you're actually setting the color, is your background_view_rounded_top a <selector> element or are you setting it in a listener in the activity?

But yeah I also recently discovered that android:duplicateParentState="true" isn't the panacea that I thought it was.

Should those two solutions be exhausted then my go-to solution usually is a custom component for the root layout. http://developer.android.com/guide/topics/ui/custom-components.html But that might not be necessary if the interface is this simple. I'm pretty sure you can do this with StateListDrawables.

Custom components are usually easier than trying to find the magic combination of xml variables, and will make the layout more understandable/editable later if you're planning on making this more complicated.

like image 28
Andrew Avatar answered Nov 06 '22 13:11

Andrew