johann
johann

Reputation: 1135

android: add view to an inflated View

I want to inflate a layout from my res/layout directory, and add some views to it in my activity class.

My layout xml file:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_marginTop="30dip"
    android:layout_height="fill_parent" >

    <Button
        android:id="@+id/reg_button"
        style="@style/simple_button"
        android:onClick="clickButton"
        android:text="@string/reg_button" />


    <!--  I WANT TO PUT CONTENT HERE ! -->

</LinearLayout>

In my java code, I try to inflate the above layout and add a linearLayout with a Button.

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    mContext = getApplicationContext();

    //inflate xml
    LinearLayout mainLayout = (LinearLayout)     LayoutInflater.from(getBaseContext()).inflate(R.layout.regions_search, null);


    //Create a new LinearLayout
    LinearLayout newLinear = new LinearLayout(mContext);        
    newLinear.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

    //create  a new Button
    Button test = new Button(mContext);
    test.setText("My button");

    //first , I add button to the LinearLayout
    newLinear.addView(test);

    //Then, I add layout to the inflated layout
    mainLayout.addView(newLinear);


    //display
    setContentView(mainLayout);

But the newLinear view (and its children) aren't getting displayed.

Upvotes: 2

Views: 5343

Answers (3)

Anil Jadhav
Anil Jadhav

Reputation: 2158

Use this in your activity,

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // display
    setContentView(R.layout.activity_main);

    Context mContext = getApplicationContext();
    LinearLayout mainLayout = (LinearLayout) findViewById(R.id.regions_search);
    // Create a new LinearLayout
    LinearLayout newLinear = new LinearLayout(mContext);
    newLinear.setLayoutParams(new LinearLayout.LayoutParams(
            LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

    // create a new Button
    Button test = new Button(mContext);
    test.setText("My button");

    // first , I add button to the LinearLayout
    newLinear.addView(test);

    // Then, I add layout to the inflated layout
    mainLayout.addView(newLinear);
}

And in your xml file (activity_main.xml)in write as shown below,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/regions_search"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="30dip" 
android:orientation="vertical">

<Button
    android:id="@+id/reg_button"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:onClick="clickButton"
    android:text="RegButton" />

<!-- I WANT TO PUT CONTENT HERE ! -->

</LinearLayout>

Try this it will work 100%.

Upvotes: 3

mukesh
mukesh

Reputation: 4140

test.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,1f));

comment line

setContentView(mainLayout);

Upvotes: 0

midhunhk
midhunhk

Reputation: 5554

Can you try adding the width and height parameters for both the new LinearLayout and the Button that you have added? Those parameters are required when creating views.

like this.

newLinear.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

Upvotes: 0

Related Questions