Search code examples
androidtabsandroid-tabhostfragment-tab-host

Changing Text of each TAB in Tabhost


Here's what my TABS look like at the moment:

enter image description here

How would I change the text of the TextView I am using to display the text, for each individual TAB?

As you can see, the text of each TAB is the same. I have tried using methods of changing the text for each individual one, but nothing seems to work.

Any guidance would be much appreciated.

XML using to display the TextView:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabsLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >

<TextView
    android:id="@+id/Tab_Text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

<ImageView
    android:id="@+id/tab_icon"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

Class:

public class MainActivity extends FragmentActivity {

/* Tab identifiers */
private final String TAB_A = "Appointments";
private final String TAB_B = "Calender";
private final String TAB_C = "Profile";
private final String TAB_D = "Settings";

private String selectedTab;

private static TabHost mTabHost;

private Fragment1 fragment1;
private Fragment2 fragment2;
private Fragment3 fragment3;
private Fragment4 fragment4;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    fragment1 = new Fragment1();
    fragment2 = new Fragment2();
    fragment3 = new Fragment3();
    fragment4 = new Fragment4();

    mTabHost = (TabHost) findViewById(android.R.id.tabhost);
    mTabHost.setOnTabChangedListener(listener);
    mTabHost.setup();

    initializeTab();
}

// TABS



public void initializeTab() {
    TabHost.TabSpec spec = mTabHost.newTabSpec(TAB_A);
    mTabHost.setCurrentTab(0);

    spec.setContent(new TabHost.TabContentFactory() {
        public View createTabContent(String tag) {
            return findViewById(android.R.id.tabcontent);
        }
    });

    //First TAB

    spec.setIndicator(createTabView(TAB_A, R.drawable.tab1_selector));
    mTabHost.addTab(spec);

    spec = mTabHost.newTabSpec(TAB_B);



    spec.setContent(new TabHost.TabContentFactory() {
        public View createTabContent(String tag) {
            return findViewById(android.R.id.tabcontent);
        }

    });

    //Second TAB

    spec.setIndicator(createTabView(TAB_B, R.drawable.tab2_selector));
    mTabHost.addTab(spec);

    spec = mTabHost.newTabSpec(TAB_C);
    spec.setContent(new TabHost.TabContentFactory() {
        public View createTabContent(String tag) {
            return findViewById(android.R.id.tabcontent);
        }
    });

    //Third TAB

    spec.setIndicator(createTabView(TAB_C, R.drawable.tab3_selector));
    mTabHost.addTab(spec);

    spec = mTabHost.newTabSpec(TAB_D);
    spec.setContent(new TabHost.TabContentFactory() {
        public View createTabContent(String tag) {
            return findViewById(android.R.id.tabcontent);
        }
    });

    //Fourth TAB

    spec.setIndicator(createTabView("TAB_D", R.drawable.tab4_selector));
    mTabHost.addTab(spec);

    spec.setIndicator("Tab4");

    mTabHost.setCurrentTab(0);
}

TabHost.OnTabChangeListener listener = new TabHost.OnTabChangeListener() {
    public void onTabChanged(String tabId) {
        /* Set current tab.. */
        if (tabId.equals(TAB_A)) {
            pushFragments(tabId, fragment1);
            selectedTab = TAB_A;
        } else if (tabId.equals(TAB_B)) {
            pushFragments(tabId, fragment2);
            selectedTab = TAB_B;
        } else if (tabId.equals(TAB_C)) {
            pushFragments(tabId, fragment3);
            selectedTab = TAB_C;
        } else if (tabId.equals(TAB_D)) {
            pushFragments(tabId, fragment4);
            selectedTab = TAB_D;
        }
    }
};

/*
 * adds the fragment to the FrameLayout
 */
public void pushFragments(String tag, Fragment fragment) {

    FragmentManager manager = getSupportFragmentManager();
    FragmentTransaction ft = manager.beginTransaction();

    ft.replace(android.R.id.tabcontent, fragment);
    ft.commit();
}

/*
 * returns the tab view i.e. the tab icon and text
 */
private View createTabView(final String tabText, final int id) {
    View view = LayoutInflater.from(this).inflate(R.layout.tabs_icon, null);

    //Tab image

    ImageView imageViewTabIcon = (ImageView) view
            .findViewById(R.id.tab_icon);
    imageViewTabIcon.setImageDrawable(getResources().getDrawable(id));

    //Tab text

    TextView TextViewTAB = (TextView) findViewById(R.id.Tab_Text);



    return view;
}

public static void tabfresh() {
    mTabHost.setCurrentTab(0);
}

public static void tabfresh_sal() {
    mTabHost.setCurrentTab(1);
}
}

Crash LOG for @kalyan pvs

11-04 11:29:36.139: E/AndroidRuntime(18623): FATAL EXCEPTION: main
11-04 11:29:36.139: E/AndroidRuntime(18623): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.question.question/com.question.question.MainActivity}: java.lang.NullPointerException
11-04 11:29:36.139: E/AndroidRuntime(18623):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2071)
11-04 11:29:36.139: E/AndroidRuntime(18623):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2096)
11-04 11:29:36.139: E/AndroidRuntime(18623):    at android.app.ActivityThread.access$600(ActivityThread.java:138)
11-04 11:29:36.139: E/AndroidRuntime(18623):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1207)
11-04 11:29:36.139: E/AndroidRuntime(18623):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-04 11:29:36.139: E/AndroidRuntime(18623):    at android.os.Looper.loop(Looper.java:213)
11-04 11:29:36.139: E/AndroidRuntime(18623):    at android.app.ActivityThread.main(ActivityThread.java:4787)
11-04 11:29:36.139: E/AndroidRuntime(18623):    at java.lang.reflect.Method.invokeNative(Native Method)
11-04 11:29:36.139: E/AndroidRuntime(18623):    at java.lang.reflect.Method.invoke(Method.java:511)
11-04 11:29:36.139: E/AndroidRuntime(18623):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
11-04 11:29:36.139: E/AndroidRuntime(18623):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
11-04 11:29:36.139: E/AndroidRuntime(18623):    at dalvik.system.NativeStart.main(Native Method)
11-04 11:29:36.139: E/AndroidRuntime(18623): Caused by: java.lang.NullPointerException
11-04 11:29:36.139: E/AndroidRuntime(18623):    at com.question.question.MainActivity.createTabView(MainActivity.java:165)
11-04 11:29:36.139: E/AndroidRuntime(18623):    at com.question.question.MainActivity.initializeTab(MainActivity.java:70)
11-04 11:29:36.139: E/AndroidRuntime(18623):    at com.question.question.MainActivity.onCreate(MainActivity.java:51)
11-04 11:29:36.139: E/AndroidRuntime(18623):    at android.app.Activity.performCreate(Activity.java:5008)
11-04 11:29:36.139: E/AndroidRuntime(18623):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
11-04 11:29:36.139: E/AndroidRuntime(18623):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2035)
11-04 11:29:36.139: E/AndroidRuntime(18623):    ... 11 more

Solution

  • TextViewTAB.setText(tabText); //Added to your code

    In this method set the text for the individual tabs

    private View createTabView(final String tabText, final int id) {
    View view = LayoutInflater.from(this).inflate(R.layout.tabs_icon, null);
    
    //Tab image
    
    ImageView imageViewTabIcon = (ImageView) view
            .findViewById(R.id.tab_icon);
    imageViewTabIcon.setImageDrawable(getResources().getDrawable(id));
    
    //Tab text
    
    TextView TextViewTAB = (TextView)view. findViewById(R.id.Tab_Text);  //Added to your code
    
    TextViewTAB.setText(tabText);   //Added to your code
    
    return view;
    }
    

    tabText which already passed as a paramenter in method