Android Studio: Setting up tabs in a TabHost Container

A short tutorial of how to add an popularize a tabhost container in Android Studio.

  1. In the design view add a tabhost container.
  2. Add/remove the tabs as you need and rename the tabs to something meaningful, prefixing the id’s with “tab” might be a good idea, so they can be easily accessed via auto completion.
  3. In the onCreate function of your activity set up the tabhost
    TabHost th = (TabHost) findViewById(R.id.tabHostId);
    th.setup();
    
  4. After the TabHost is setup, add specs for each tab
    TabHost.TabSpec ts = th.newTabSpec("tab_name");
    ts.setContent(R.id.tabId);
    ts.setIndicator("Tab Label");
    th.addTab(ts);
    

Repeat Step 4 for each tab you want to add.

This entry was posted in Android and tagged , . Bookmark the permalink.

1 Response to Android Studio: Setting up tabs in a TabHost Container

  1. Bsou's avatar Bsou says:

    THANK YOU , THANK YOU , finally something clear that works !

Leave a comment