12

I have the following layout.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            style="@style/list_item_bottom">

    <TextView android:id="@+id/list_item_name"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"/>

    <ViewStub android:id="@+id/stub_for_alt_name"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_below="@+id/list_item_name"
              android:layout="@layout/text_view_alt_name"/>

    <ImageView android:id="@+id/list_item_dopinfo1_image"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_below="@+id/stub_for_alt_name"
               android:src="@drawable/ic_released"/>

</RelativeLayout>

I want ViewStub to be below TextView and ImageView to be below ViewStub.

Elements with ViewStub inflated are shown as I expect. But elements without ViewStub have TextView overlapped with ImageView.

What's wrong with my layout?

UPDATE:

The only solution I've found is to give ViewStub and related TextView the same android:id value.

Onik
  • 19,396
  • 14
  • 68
  • 91
AHTOH
  • 446
  • 4
  • 16

4 Answers4

27

I know it's a bit of an old question but the trick in there is to set the inflatedId parameter to the same as the actual viewStub itself, like this:

<ViewStub android:id="@+id/stub_for_alt_name"
          android:inflatedId="@id/stub_for_alt_name"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_below="@+id/list_item_name"
          android:layout="@layout/text_view_alt_name"/>
Budius
  • 39,391
  • 16
  • 102
  • 144
  • Brilliant. You just saved me a couple of hours :) – Konrad Morawski May 04 '14 at 15:53
  • This actually worked. I wonder what's the purpose of "inflatedId". – android developer Nov 15 '14 at 15:57
  • As per the documentation, ViewStub is used to inflate layout at runtime. So when you call inflate() on the ViewStub, it is then replaced by the referenced layout and ViewStub is replaced by the inflated view. If you wish to refer this ViewStub after being inflated you can use the inflatedId. – Ishaan Oct 24 '17 at 14:06
5

The only solution I've found is to give ViewStub and related TextView the same android:id value.

AHTOH
  • 446
  • 4
  • 16
3

It's an old question, but I've got a useful addition

1) Yes, like others have posted - use same id and inflatedId:

<ViewStub 
   android:id="@+id/view_stub_id"
   android:inflatedId="@id/view_stub_id"
   ... 
/>

2) When you need to inflate view or refresh its content it's convenient to act like this:

    View v = parentView.findViewById(R.id.view_stub_id);
    if (v instanceof ViewStub)
        v = ((ViewStub) v).inflate();

    // here v is always the inflated view
    // ...
sberezin
  • 3,266
  • 23
  • 28
1
Hi you can try this one.

<RelativeLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/list_item_bottom">

//you can add another relative layout containing your textview and imageview
  <RelativeLayout
        android:id="@+id/layout_content" 
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >


        <TextView android:id="@+id/list_item_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

         <ImageView android:id="@+id/list_item_dopinfo1_image"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_below="@+id/stub_for_alt_name"
             android:src="@drawable/ic_released"
             android:layout_below="@+id/list_item_name" />

</RelativeLayout>

<ViewStub android:id="@+id/stub_for_alt_name"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_below="@+id/list_item_name"
          android:layout="@layout/text_view_alt_name"
          android:layout_below="@+id/layout_content" />

</RelativeLayout>
gg2012
  • 11
  • 1
  • Yes, this should work, but it's nested layout. In this case it would be better to replace RelativeLayout with LinearLayout – AHTOH Oct 25 '12 at 04:26