Android, how to set first tab as a default tab always

vishesh chandra picture vishesh chandra · Jun 20, 2011 · Viewed 15.9k times · Source
public class SoapBox extends TabActivity{

    private TabHost tabHost;

    private void setupTabhost()
    {
        tabHost=(TabHost)findViewById(android.R.id.tabhost);
        tabHost.setup();
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab);

        TabHost tabHost=getTabHost();
        setupTabhost();
        tabHost.getTabWidget().setDividerDrawable(R.drawable.tab_space);

        Intent intent1=new Intent().setClass(this, MySoapBox.class);
        setupTab(new TextView(this), "My SoapBox", intent1);
        Intent intent2=new Intent().setClass(this, FriendsSoapBox.class);
        setupTab(new TextView(this),"Friends SoapBox", intent2);
        Intent intent3=new Intent().setClass(this, Recommendations.class);
        setupTab(new TextView(this),"Recommendations", intent3);

        tabHost.setCurrentTab(0);

    }
    private void setupTab(final View view, final String tag, Intent intent)
    {
        View tabView=createTabView(tabHost.getContext(), tag);
        TabSpec tabSpec=tabHost.newTabSpec(tag).setIndicator(tabView).setContent(intent);
        tabHost.addTab(tabSpec);
    }
    private static View createTabView(final Context context,final String text)
    {
        View view= LayoutInflater.from(context).inflate(R.layout.tabs_bg,null);
        TextView textView=(TextView)view.findViewById(R.id.tabsText);
        textView.setText(text);
        return view;
    }
}

Here I have one more Tab widget inside that Tab button I implemented one nested Tab widget, when I switching one tab to another and come back to previous tab then I am didn't get focus on first tab.

The focus of nested tab should always on first tab when I again select the parent tab. Please guide me, where am I doing mistake in my code? Thanks in advance.

Answer

xyz picture xyz · Jun 20, 2011

setCurrentTab(); property decides the default tab.
If you use tabHost.setCurrentTab(n); then n th tab will be the default tab.