Coenni
Coenni

Reputation: 197

How to Change Label of TabHost Programmatically/Dynamically

I have a tabhost created by

  this.tabHost = getTabHost();

     // Resusable TabSpec for each tab
    Intent intent;  // Reusable Intent for each tab

    // Create an Intent to launch the first Activity for the tab (to be reused)
    intent = new Intent().setClass(this, FirstGroup.class);

    // Initialize a TabSpec for the first tab and add it to the TabHost
    spec1 = tabHost.newTabSpec("FirstGroup").setIndicator("Regionlar",
            getResources().getDrawable(R.drawable.region2)) // Replace null with R.drawable.your_icon to set tab icon
                    .setContent(intent);
    tabHost.addTab(spec1);

And I want to change label of tabhost programmatically: "Regionlar" to "newMenuTabbar". I couldn't find any example. thanks for attention.

Edit: I want to change second tabitem's label from "Mənzərələr"=> "secondTabitem"

intent = new Intent().setClass(this, FirstGroup.class);

    // Initialize a TabSpec for the first tab and add it to the TabHost
    spec1 = tabHost.newTabSpec("FirstGroup").setIndicator("Regionlar",
            getResources().getDrawable(R.drawable.region2)) // Replace null with R.drawable.your_icon to set tab icon
                    .setContent(intent);
    tabHost.addTab(spec1);

        // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent().setClass(this, SecondActivityGroup.class);
    spec2 = tabHost.newTabSpec("SecondActivityGroup").setIndicator("Mənzərələr",
            getResources().getDrawable(R.drawable.img_gallery_icon)) // Replace null with R.drawable.your_icon to set tab icon
                    .setContent(intent);
    tabHost.addTab(spec2);

Upvotes: 2

Views: 5493

Answers (3)

Mr. Kro
Mr. Kro

Reputation: 138

b.i 's code can only change the first tab's title. I think this is a more straight way to get the thing done:

((TextView) mTabHost.getTabWidget().getChildTabViewAt(position)
.findViewById(android.R.id.title))
.setText(yourTitle);

where position is the position of the tab and yourTitle is the title you wish to set for the tab. If you want to change the text for the current tab, then instead of replacing position by getCurrentTab, you can just replace getTabWidget().getChildTabViewAt(position) by getCurrentTabView()

Whoever wrote this should have defined a setTabText(int position, String text) method, otherwise who would know they have a text view id'ed android.R.id.title? Or if they already had, please enlighten me.

Upvotes: 1

Aerrow
Aerrow

Reputation: 12134

Try this code

public class SlideMainActivity extends TabActivity {
    public static RelativeLayout headerLayout;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.main_silde_tab);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
                R.layout.hd_header);
        setTabs();
    }

    private void setTabs() {
        addTab("FirstGroup", R.drawable.tab_home, FirstGroup.class);
        addTab("Regionlar", R.drawable.tab_search, Regionlar.class);

    }

    private void addTab(String labelId, int drawableId, Class<?> c) {
        TabHost tabHost = getTabHost();
        Intent intent = new Intent(this, c);
        TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);

        View tabIndicator = LayoutInflater.from(this).inflate(
                R.layout.tab_indicator, getTabWidget(), false);
        TextView title = (TextView) tabIndicator.findViewById(R.id.title);
        title.setText(labelId);
        ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
        icon.setImageResource(drawableId);

        spec.setIndicator(tabIndicator);
        spec.setContent(intent);
        tabHost.addTab(spec);
    }

}

Upvotes: 0

b.i
b.i

Reputation: 1107

Try this:

final TextView label = (TextView) tabHost.getTabWidget().findViewById(android.R.id.title);
label .setText(YOUR NEW LABEL);

Hope it will help.

Upvotes: 5

Related Questions