views:

1096

answers:

1

I am having a problem with views overlapping in a RelativeLayout on Android 1.5... Everything is working fine on Android 1.6 and above.

I do understand that Android 1.5 has some issues with RelativeLayout, but I was not able to find anything on StackOverflow or the android beginners group for my specific problem.

My layout consists of four sections, each of which are made up of a TextView, a Gallery, and another TextView aligned vertically:

Running Apps
Recent Apps
Services
Processes

When all four sets of these items are displayed everything works fine. However, my app allows the user to specify that some of these are not displayed. If the user turns off Running Apps, Recent Apps, or Services then the remaining sections all of a sudden overlap eachother.

Here is my code for the layout. I'm not sure what I am doing wrong. When the user turns off display of a section I use the View.GONE visibility setting:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_vertical"
    android:layout_gravity="center_vertical"
    android:background="@null"
>
<!-- Running Gallery View Items -->
<TextView 
    style="@style/TitleText"
    android:id="@+id/running_gallery_title_text_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="left"
    android:paddingLeft="1sp"
    android:paddingRight="10sp"
    android:text="@string/running_title"
/>

<Gallery
    android:id="@+id/running_gallery_id"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/running_gallery_title_text_id"
    android:spacing="5sp"
    android:clipChildren="false"
    android:clipToPadding="false"
    android:unselectedAlpha=".5"
/>

<TextView 
    style="@style/SubTitleText"
    android:id="@+id/running_gallery_current_text_id"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/running_gallery_id"
    android:gravity="center_horizontal"
/>

<!-- Recent Gallery View Items -->
<TextView 
    style="@style/TitleText"
    android:id="@+id/recent_gallery_title_text_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/running_gallery_current_text_id"
    android:gravity="left"
    android:paddingLeft="1sp"
    android:paddingRight="10sp"
    android:text="@string/recent_title"
/>

<Gallery
    android:id="@+id/recent_gallery_id"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/recent_gallery_title_text_id"
    android:spacing="5sp"
    android:clipChildren="false"
    android:clipToPadding="false"
    android:unselectedAlpha=".5"
/>

<TextView 
    style="@style/SubTitleText"
    android:id="@+id/recent_gallery_current_text_id"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/recent_gallery_id"
    android:gravity="center_horizontal"
/>

<!-- Service Gallery View Items -->
<TextView 
    style="@style/TitleText"
    android:id="@+id/service_gallery_title_text_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/recent_gallery_current_text_id"
    android:gravity="left"
    android:paddingLeft="1sp"
    android:paddingRight="10sp"
    android:text="@string/service_title"
/>

<Gallery
    android:id="@+id/service_gallery_id"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/service_gallery_title_text_id"
    android:spacing="5sp"
    android:clipChildren="false"
    android:clipToPadding="false"
    android:unselectedAlpha=".5"
/>

<TextView 
    style="@style/SubTitleText"
    android:id="@+id/service_gallery_current_text_id"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/service_gallery_id"
    android:gravity="center_horizontal"
/>
</RelativeLayout>

I ommitted the xml for the Processes section in a (somewhat vain) attempt to keep this shorter...

What can I do to make this work in Android 1.5? I don't think it is just a matter of reordering the views in the xml because it works fine when everything is displayed.

+2  A: 

Two possible solutions:

  • Try setting the element's height to 0 or 1 px and visibility to INVISIBLE instead of GONE.
  • Wrap each Gallery/TextView in a LinearLayout set to wrap_height, and set the above/belows on the layouts instead of the subviews. Then set the subelements to View.GONE, leaving the linear layouts used for the relative positioning still visible but with wrapped height 0.

The idea with either solution is to make sure you're never positioning something relative to a view that's View.GONE; I'd suspect that's the source of the bug you're running into.

If I may ask, though... why do you even need to use a RelativeLayout here at all? From what I can see at a glance, everything here would fit fine into a vertical LinearLayout, and in fact seems to be conceptually simpler for this arrangement.

Yoni Samlan
Thanks for your suggestions... I will give them a try.This is my layout for portrait mode... With landscape mode it gets a little more complicated and would require several nested LinearLayouts. While I could use LinearLayout in portrait mode I still have the same problem for landscape mode.
Justin