Adding dynamic tabs in android

in my application i want to add tabs dynamically about button click. How can I add? the rest of the gui shoul will be the same as it is. Tabs should be added. here xml file -

<?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="wrap_content"
android:background="@color/Beige"
android:focusable="true"
android:orientation="vertical" >
    <LinearLayout
    android:id="@+id/linearLayout3"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignLeft="@+id/linearLayout2"
    android:layout_below="@+id/linearLayout2" >
    <TabHost
        android:id="@+id/th"
        android:layout_width="match_parent"
        android:layout_height="50dp" >
        <TabWidget
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

         <FrameLayout android:id="@+id/tabcontent"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
            android:paddingTop="62px">
       <Button android:id="@+id/buttontab"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
         android:text="A semi-random button"
        />
       </FrameLayout>        
    </TabHost>
</LinearLayout>
<LinearLayout
    android:id="@+id/linearLayout4"
    android:layout_width="match_parent"
    android:layout_height="380dp"
    android:layout_alignLeft="@+id/linearLayout3"
    android:layout_below="@+id/linearLayout3"
    android:orientation="vertical" >
    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        <HorizontalScrollView
            android:id="@+id/hv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
            <ImageView
                android:id="@+id/imv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </HorizontalScrollView>
    </ScrollView>
</LinearLayout>
 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:layout_alignLeft="@+id/linearLayout4"
    android:layout_below="@+id/linearLayout4" >
    <Button
        android:id="@+id/addtab"
        android:layout_width="80dp"
        android:layout_height="50dp"
        android:onClick="addtab"
        android:text="@string/addtab"
        android:textColor="@color/Black" />
  </LinearLayout>
  </RelativeLayout>

      

rest of the scrollview, horizontal scrolling should be the same for each tab.

when the button is pressed -

    public void addtab(View v){

    TabHost tabHost = new TabHost(this);
    tabHost=(TabHost) findViewById(R.id.th);

    TabWidget tabWidget = new TabWidget(this);
    tabWidget=(TabWidget) findViewById(R.id.tabs); 
    tabHost.addView(tabWidget);
    FrameLayout tabContent = new FrameLayout(this);
    tabContent=(FrameLayout) findViewById(R.id.tabcontent);
    tabContent.setPadding(0, 65, 0, 0);
    tabHost.addView(tabContent);
    TextView content = new TextView(this);
    content.setText("This is the Frame Content");
    content.setId(100);
    tabHost.setup();
    TabSpec tspec1 = tabHost.newTabSpec("Tab1");
    tspec1.setIndicator("Button");
    tspec1.setContent(new PreExistingViewFactory(content));
    tabHost.addTab(tspec1);     

    }

class PreExistingViewFactory implements TabContentFactory{
    private final View preExisting;
    protected PreExistingViewFactory(View view){
    preExisting = view;
    }
    public View createTabContent(String tag) {
    return preExisting;
    }
    }

      

how to add tabs? Please help me

exceptions -

02-18 16:02:14.375: E/AndroidRuntime(737): java.lang.IllegalStateException: Could not execute method of the activity
02-18 16:02:14.375: E/AndroidRuntime(737):  at android.view.View$1.onClick(View.java:2072)
02-18 16:02:14.375: E/AndroidRuntime(737):  at android.view.View.performClick(View.java:2408)
02-18 16:02:14.375: E/AndroidRuntime(737):  at android.view.View$PerformClick.run(View.java:8816)
02-18 16:02:14.375: E/AndroidRuntime(737):  at android.os.Handler.handleCallback(Handler.java:587)
02-18 16:02:14.375: E/AndroidRuntime(737):  at android.os.Handler.dispatchMessage(Handler.java:92)
02-18 16:02:14.375: E/AndroidRuntime(737):  at android.os.Looper.loop(Looper.java:123)
02-18 16:02:14.375: E/AndroidRuntime(737):  at android.app.ActivityThread.main(ActivityThread.java:4627)
02-18 16:02:14.375: E/AndroidRuntime(737):  at java.lang.reflect.Method.invokeNative(Native Method)
02-18 16:02:14.375: E/AndroidRuntime(737):  at java.lang.reflect.Method.invoke(Method.java:521)
02-18 16:02:14.375: E/AndroidRuntime(737):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
 02-18 16:02:14.375: E/AndroidRuntime(737):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-18 16:02:14.375: E/AndroidRuntime(737):  at dalvik.system.NativeStart.main(Native Method)
02-18 16:02:14.375: E/AndroidRuntime(737): Caused by: java.lang.reflect.InvocationTargetException
02-18 16:02:14.375: E/AndroidRuntime(737):  at com.my.zproject.Work.addtab(Work.java:169)
02-18 16:02:14.375: E/AndroidRuntime(737):  at java.lang.reflect.Method.invokeNative(Native Method)
02-18 16:02:14.375: E/AndroidRuntime(737):  at java.lang.reflect.Method.invoke(Method.java:521)
02-18 16:02:14.375: E/AndroidRuntime(737):  at android.view.View$1.onClick(View.java:2067)
02-18 16:02:14.375: E/AndroidRuntime(737):  ... 11 more
02-18 16:02:14.375: E/AndroidRuntime(737): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child parent first.
02-18 16:02:14.375: E/AndroidRuntime(737):  at android.view.ViewGroup.addViewInner(ViewGroup.java:1970)

      

+3


source to share


1 answer


override getPagetitle method in FragmentPagerAdapter as,

 @Override
public CharSequence getPageTitle(int position) {
    return myFragmentTitleList.get(position);
}

      



where myFragmentTitleList is your arraylist of titles.

0


source







All Articles