Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add Layout programmatically inside another one

I have an xml file (option_element.xml) that contains a ImageView and a TextView

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="-18dp" >

    <ImageView
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/option_button" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/button"
        android:layout_alignLeft="@+id/button"
        android:layout_alignRight="@+id/button"
        android:layout_alignTop="@+id/button"
        android:layout_centerHorizontal="true"
        android:gravity="center" />

</RelativeLayout>

I should fill a LinearLayout with this View, based on a content of an array

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/options_list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" />

    <!-- other layout elements -->

</LinearLayout>

I'm adding it like

LinearLayout options_layout = (LinearLayout) findViewById(R.id.options_list);
String[] options = getActivity().getResources().getStringArray(R.array.options);
for (int i = 0; i < options.length; i++) {
    View to_add = inflater.inflate(R.layout.options_element,
                options_layout);

    TextView text = (TextView) to_add.findViewById(R.id.text);
    text.setText(options[i]);
    text.setTypeface(FontSelector.getBold(getActivity()));

}

But something gone wrong. I'm expecting options.length ImageView with relative TextView filled with options[i] text, but i obtain options.length ImageView, but only first one has text (and text it's not options[0] element, but last one).

For example, if option contains {"one", "two", "three"} inside first imageView i get "three", and other ones are empty. How can put each string inside each TextView?

like image 518
giozh Avatar asked Feb 12 '15 08:02

giozh


People also ask

Can we embed one layout inside another layout?

To efficiently re-use 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.

Can layouts be nested?

In Android all layout can be nested one another. In this example we create a Registration Form with multiple fields using Nested Linear Layouts. For that we set vertical orientation for parent Linear Layout and horizontal orientation for child Linear Layout.

How do you use nested linear layout?

Show activity on this post. add this tag to all your list views, so this will maintain symmetry in all your textView placement. Secondly: Keep a single Parent LinearLayout with orientation vertical, then have multiple LinearLayout with horizontal orientation and two TextView inside it.


1 Answers

The inflate(int resource, ViewGroup root) method return root if root is not null, thus to_add.findViewById() equals options_layout.findViewById() and it always return the Views in the first position. Change as following should help:

LinearLayout options_layout = (LinearLayout) findViewById(R.id.options_list);
String[] options = getActivity().getResources().getStringArray(R.array.options);
for (int i = 0; i < options.length; i++) {
    View to_add = inflater.inflate(R.layout.options_element,
                options_layout);

    TextView text = (TextView) to_add.findViewById(R.id.text);
    text.setText(options[i]);
    text.setTypeface(FontSelector.getBold(getActivity()));

}

to:

LinearLayout options_layout = (LinearLayout) findViewById(R.id.options_list);
String[] options = getActivity().getResources().getStringArray(R.array.options);
for (int i = 0; i < options.length; i++) {
    View to_add = inflater.inflate(R.layout.options_element,
                options_layout,false);

    TextView text = (TextView) to_add.findViewById(R.id.text);
    text.setText(options[i]);
    text.setTypeface(FontSelector.getBold(getActivity()));
    options_layout.addView(to_add);
}
like image 75
Lei Guo Avatar answered Sep 22 '22 13:09

Lei Guo