0

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.

Juned
  • 6,290
  • 7
  • 45
  • 93
human
  • 637
  • 4
  • 15
  • 41

2 Answers2

1

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);
}
   }
Babu
  • 957
  • 1
  • 9
  • 21
  • tabactivity is deprecated! – human Jan 29 '13 at 10:22
  • 1
    absolutely i had same problem i have tried and i am still using this code in my many apps, for more please browse this link: http://adilsoomro.blogspot.in/2011/06/iphone-like-tabs-in-android.html and feel free to contact me if any problem arise because i guess i can help you out.... – Babu Jan 29 '13 at 10:24
  • can you explain this code with comment lines. I am a beginner. pls – human Jan 29 '13 at 14:17
1

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

Community
  • 1
  • 1
Juned
  • 6,290
  • 7
  • 45
  • 93