kamal
kamal

Reputation: 2398

how to add a view in another view using layout inflatter

I created mainLayout with two buttons

add: to add other layout

remove : to remove other layout.

    <Button
             android:id="@+id/btnAdd"
             android:textStyle="bold"

             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:layout_gravity="center_horizontal"
             android:text="Add View"
             android:onClick="addView" />

      <Button
             android:id="@+id/btnRemove"
             android:textStyle="bold"

             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:layout_gravity="center_horizontal"
             android:text="Remove View"
             android:onClick="removeView" />

now i wrote following code to add the view when I click on addView button

               LayoutInflater inflater= (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
    view=inflater.inflate(R.layout.other_layout,null);


    mainLayout.addView(view);

the view is added below the main layout. But I want the view to add right below addView button(and above removeView button and not in the bottom of main Layout)

How can I do that?

Upvotes: 1

Views: 1363

Answers (3)

10s
10s

Reputation: 1699

Create a new layout that you wish to add using the <merge> tag like:

<merge ...>
   //your layout
</merge>

Add the <include> layout with visibility gone(or invisible) in your primary layout. And by handling the event that you want you can simply adjust the visibility the way you like.

Upvotes: 0

user1351659
user1351659

Reputation: 54

Best way to reuse layouts in Android is by using <include> tag. Check here for more ways of using this: Re-using layouts with include tag

Upvotes: 0

Umesh
Umesh

Reputation: 4256

add a framelayout between the two buttons.

Then on runtime inflate your view into the framelayout.

LayoutInflater inflater= (LayoutInflater)this.getSystemService (LAYOUT_INFLATER_SERVICE);     view=inflater.inflate(R.layout.other_layout,null);  
    myframeLayout.addView(view); 

Upvotes: 1

Related Questions