Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including views using merge tag and ViewStub

Tags:

android

I wanted to extend my TableLayout with some additional rows through ViewStab element. This is my Activity's layout:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/root_table_layout">

    <TableRow>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Row 1"/>
    </TableRow>

    <TableRow>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Row 2"/>
    </TableRow>

    <ViewStub
        android:id="@+id/view_stub_1"
        android:layout="@layout/merge_table_1"/>

    <ViewStub
        android:id="@+id/view_stub_2"
        android:layout="@layout/merge_table_2"/>

</TableLayout>

Either view_stub_1 or view_stub_2 is inflated on Activity onCreate (it depends on app's condition).

ViewStub was supposed to inflate a layout defined with <merge> tag as the root element. Like this:

<merge xmlns:android="http://schemas.android.com/apk/res/android">

    <TableRow>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Row 5"/>
    </TableRow>

    <TableRow>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Row 6"/>
    </TableRow>

</merge>

But when the Activity was about to start, I got this exception:

merge can be used only with a valid ViewGroup root and attachToRoot=true - worked with getLayoutInflater

like image 548
Ondřej Z Avatar asked Mar 30 '15 11:03

Ondřej Z


People also ask

What is a ViewStub?

android.view.ViewStub. A ViewStub is an invisible, zero-sized View that can be used to lazily inflate layout resources at runtime. When a ViewStub is made visible, or when inflate() is invoked, the layout resource is inflated. The ViewStub then replaces itself in its parent with the inflated View or Views.

How to use include layout in Android?

Reuse layouts with <include/> To efficiently reuse complete layouts, you can use the <include/> and <merge/> tags to embed another layout inside the current layout. Reusing layouts is particularly powerful as it allows you to create reusable complex layouts.

How do I use view stubs on Android?

When the ViewStub is inflated, it replaces itself in its parent view with the inflated layout resource. Its visibility will exist in the view hierarchy only when setVisibility(int) or inflate() is invoked. The inflated view is added to ViewStub's parent using the layout parameter.


1 Answers

It seems like <merge> tag is not supported by ViewStub element right now:

The only drawback of ViewStub is that it currently does not support the tag.

Romain Guy's post on layout tricks

In the end I used Activity's LayoutInflater to include additional rows into the TableLayout:

ViewGroup rootTableLayout = (ViewGroup) findViewById(R.id.root_table_layout);
getLayoutInflater().inflate(R.layout.merge_table_1, rootTableLayout, true);

Since I only wanted to attach a few TableRows at the and of the ViewGroup, this solution works. For more complicated scenario (dynamically include TableRows in the middle of a TableLayout), I still don't know what would be a proper way to do it.

like image 64
Ondřej Z Avatar answered Sep 23 '22 04:09

Ondřej Z