1

Is it possible to inflate and remove a layout multiple times within another layout. I have a main layout as below

tab_bar.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="vertical"
  android:paddingLeft="8dip"
  android:paddingRight="12dip"
  android:paddingTop="2dip"
  android:paddingBottom="1dip"
  android:background="@drawable/search_plate_browser" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >
    <LinearLayout
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:orientation="horizontal"
      >
      <ImageButton android:id="@+id/arrow_left"
         android:background="@drawable/ic_back"
         android:layout_width="wrap_content"
         android:layout_height="30dip"
         android:gravity="center_vertical"
         android:visibility="gone"
      />
      <ImageButton android:id="@+id/arrow_right"
         android:background="@drawable/ic_forward"
         android:layout_width="wrap_content"
         android:layout_height="30dip"
         android:gravity="center_vertical"
         android:visibility="gone"
       />
    </LinearLayout>
    <ImageButton android:id="@+id/addtab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="4dip"
    android:paddingRight="4dip"
    android:paddingTop="4dip"
    android:paddingBottom="4dip"
    android:gravity="center_vertical"
    android:background ="@drawable/tabicon"
    />
</LinearLayout>

Inside this layout i need to add and remove the below tab layout dynamically on click of some button

tabview.xml

  <LinearLayout 
         android:layout_width="220dip"
         android:layout_height="35dip"
         android:orientation="horizontal"
         android:background="@drawable/browsertab_add">
      <ImageView
         android:id="@+id/favicon"
         android:layout_width="15dip"
         android:layout_height="15dip"
         android:layout_marginTop="10dip"
         android:layout_marginLeft="15dip"
         android:layout_marginRight="2dip" 
         android:layout_marginBottom="2dip"/>
     <TextView
         android:id="@+id/title"
         android:layout_height="match_parent"
         android:layout_width="140dip"
         android:layout_marginTop="10dip"
         android:layout_marginLeft="5dip"
         android:textAppearance="?android:attr/textAppearanceSmall"
         android:textColor="@color/white"
         android:scrollHorizontally="true"
         android:lines="1"
         android:singleLine="true" />
      <ImageButton
         android:id="@+id/close"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_marginLeft="8dip"
         android:gravity="left"
         android:background="@drawable/ic_tab_close" />
 </LinearLayout>

Any suggestions on this would be great..

2 Answers 2

2

Do not remove it just simply set its visibility to Gone :)

Some links on the subject:

Android, add new view without XML Layout

Android Runtime Layout Tutorial

http://developer.android.com/reference/android/view/View.html

http://developer.android.com/reference/android/widget/LinearLayout.html

1
  • Although you can also remove them
    – AZ_
    Jul 14, 2011 at 12:30
1

Yes I Think you can inflate one layout in another e.g. I add views to my ViewFlipper multiple times. And these views are basically a Layout that I inflate and then add as a child. Here is the example:

LayoutInflater inflater = getLayoutInflater();
LinearLayout ll_main = (LinearLayout) inflater.inflate(R.layout.tabview,null); 

Now you have grabbed the LinearLayout as "ll_main" and now you can add it to the required Layout as a child.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.