4

How to use a tabhost in android to control fragments.i have one activity and four Fragements and its corresponding xml files.i add my tab host in main_layout_activity.xml file.there i have another Linnerlayout i want to replace this linnerlayout from Fragement.to replace these Fragements i have to use tabhost.help me please? i've found one code but its messy.please find below the code.

final TabHost tabs= (TabHost) findViewById(R.id.tabhost);
  tabs.setup();

  TabHost.TabSpec spec=tabs.newTabSpec("tag1");
  spec.setContent(R.id.fragment1);
  spec.setIndicator("View Raves");
  tabs.addTab(spec);
  spec=tabs.newTabSpec("tag2");
  spec.setContent(R.id.fragment2);
  spec.setIndicator("My Badges");
  tabs.addTab(spec);
  tabs.setCurrentTab(0); 

after having lot of ideas i do correct my code as following.pls find below

MainActivity.java

package com.example.testtabs;
import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.TabActivity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.support.v4.app.Fragment;
import com.example.testtabs.R;




public class MainActivity extends TabActivity  {


 TabHost tab;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);


  setContentView(R.layout.activity_main);


     FragementTest testfrag=new FragementTest();
  FragmentManager manager=getFragmentManager();
   FragmentTransaction transacti
  transaction.add(R.id.my_layout1, testfrag, "");
  transaction.commit();


    tab= (TabHost) findViewById(android.R.id.tabhost);
    tab.setup();

 }

}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
     tools:c 
    android:id="@+id/my_layout"

    >

    <LinearLayout
        android:id="@+id/my_layout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@android:id/tabhost"
        android:layout_marginTop="85dp"
         android:orientati >

    </LinearLayout>

    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
             android:orientati >

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </TabWidget>

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >


            </FrameLayout>
        </LinearLayout>
    </TabHost>

</RelativeLayout>
VenushkaT
  • 1,152
  • 2
  • 19
  • 42

1 Answers1

4

Craete a method like this and pass your tab data like its view,its name and Its fragment class..

/**
 * Used to create a tab
 * 
 * @param view
 * @param tag
 * @param name
 */
private void setupTab(final View view, final String tag, Class<?> name) {
        View tabview = createTabView(this, tag);
        TabSpec setContent = tabHost.newTabSpec(tag).setIndicator(tabview)
            .setContent(new TabContentFactory() {
                public View createTabContent(String tag) {
                    return view;
                }
            });
    tabHost.addTab(setContent, name, null);
}



/**
 * Used to create tabview object and setting parameters
 * 
 * @param context
 * @param text
 * @return
 */
private View createTabView(final Context context, final String text) {
    View view = LayoutInflater.from(context).inflate(R.layout.tab_layout,
            null);
    Button _button = (Button) view.findViewById(R.id.tabText);
    _button.setText(text);
    return view;
}

And call that method like this..

setupTab(new Button(this), "Tab1", Fragment1.class);
setupTab(new Button(this), "Tab2", Fragment2.class);
kalyan pvs
  • 14,486
  • 4
  • 41
  • 59
  • i want to use final TabHost tabs= (TabHost) findViewById(R.id.tabhost); but my xml file "android:id="@android:id/tabhost" how it see then i couldnt intiated tab host are there any ideas? please – VenushkaT Feb 13 '14 at 05:14
  • you can get tabhost object like this..tabHost = (TabHost) findViewById(android.R.id.tabhost); – kalyan pvs Feb 13 '14 at 05:16
  • can you please explain what new Button(this) does.in tabhost there we have tabwidget i have to add content to these tab widgets not to buttons. – VenushkaT Feb 13 '14 at 05:45
  • its just one tab view like tab1 is one button and tab2 is another button..you can set text to that button whatever you want.. – kalyan pvs Feb 13 '14 at 05:48
  • i have edited my answer you can set text to tabs what you want to display check it.. – kalyan pvs Feb 13 '14 at 05:50
  • please find above my edited code.there i have posted my all code.now im working with my code and try to add your code.thank you very much – VenushkaT Feb 13 '14 at 06:00
  • here is example project on tabActivity with fragments https://github.com/albertopenas/TabFragment – kalyan pvs Feb 13 '14 at 06:10
  • this is for tabs with fragmentTabHost https://github.com/colaboy2004/FragmentTabHostExample – kalyan pvs Feb 13 '14 at 06:10
  • there he used ActionBar i have to Use Tabhost.sorry i didnt help me.anyway thank you. – VenushkaT Feb 13 '14 at 06:25
  • @kalyanpvs Are you using `android.support.v4.app.FragmentTabHost` ? – AlexAndro Sep 13 '14 at 11:55
  • @kalyanpvs I'm also having one main activity and three fragments with bottom tab buttons(I used one library for this). Here, I'm having listview for two fragments. One for search fragment and another one for favourite. If I add my fragments by add method. Then it calls each time when I click the tab button. So my listview state changes automatically and it begins with first list item. I need to save listitem. Help me please.. Thanks in advance – Vinoth Vino Oct 22 '16 at 14:08
  • What is "tab_layout" in this scope? – Md Imran Choudhury Dec 01 '21 at 10:07