Question

I want to create layout with TabHost and I want to create activity for each tab to do some calculation. In addition i want this to work on all android >=Froyo. I searched for the solution everywhere which was not clear and conclusive. So if anybody could help me with this it could be a great help.

Was it helpful?

Solution

Nice question really helpful for others,

Use below code to work with Tabs and to call activities by click on a particular tab:

  public class TabSample extends TabActivity {
/** Called when the activity is first created. */

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tabmain);
    setTabs() ;
}
private void setTabs()
{
    addTab("Tab1", R.drawable.tab1,Home.class);
    addTab("Tab2", R.drawable.tab2,AboutUs.class);

    addTab("Tab3", R.drawable.tab3,Services.class);
    addTab("Tab4", R.drawable.tab4,Contact.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);
}
   }

OTHER TIPS

Here is the whole implementation of TabHost which works in all versions of android. see my answer in given link

tabs and images with some devices

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top