Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inflate a layout dynamically?

I have the following layout defined in XML:

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/streamRelativeLayout">
        <ListView android:layout_height="fill_parent" android:layout_width="fill_parent" android:id="@+id/streamListView"></ListView>
        <ProgressBar android:layout_centerInParent="true" android:layout_height="wrap_content" android:id="@+id/streamProgressBar" android:layout_width="wrap_content"></ProgressBar>
    </RelativeLayout>

How can I use the LayoutInflater to grab the ListView and the ProgressBar and assign it in code?

like image 308
Sheehan Alam Avatar asked May 20 '11 10:05

Sheehan Alam


People also ask

How will you inflate a layout in Android?

inflate(R. layout. custom_button, mLinearLayout, true); We specified that we want to inflate the Button from its layout resource file; we then tell the LayoutInflater that we want to attach it to mLinearLayout .

What is a ViewGroup?

ViewGroup is a collection of Views(TextView, EditText, ListView, etc..), somewhat like a container. A View object is a component of the user interface (UI) like a button or a text box, and it's also called a widget.


1 Answers

In this way:

View v = getLayoutInflater().inflate(R.layout.YOUR_LAYOUT_ID, null);
ListView listview = (ListView) v.findViewById(R.id.streamListView);
ProgressBar progress = (ProgressBar) v.findViewById(R.id.streamProgressBar);
like image 138
BFil Avatar answered Sep 20 '22 17:09

BFil