Android tab help. How to set the second tab by default when opening the application?

I created an application with 3 tabs. The application works great, but I want the second tab to be selected and loaded when the application opens. How can i install this?

Here is my code:

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TabHost tabHost = getTabHost();



        // Tab for Home
        TabSpec homespec = tabHost.newTabSpec("Home");
        // setting Title and Icon for the Tab
        homespec.setIndicator("Home",getResources().getDrawable(R.drawable.icons_home_tab));
        Intent photosIntent = new Intent(this, HomeActivity.class);
        homespec.setContent(photosIntent);

        // Tab for Child
        TabSpec childspec = tabHost.newTabSpec("Child");
        childspec.setIndicator("Child",getResources().getDrawable(R.drawable.icons_child_tab));
        Intent homeIntent = new Intent(this, ChildActivity.class);
        childspec.setContent(homeIntent);

        // Tab for Account
        TabSpec accspec = tabHost.newTabSpec("Account");
        accspec.setIndicator("Account",getResources().getDrawable(R.drawable.icons_account_tab));
        Intent accIntent = new Intent(this, AccountActivity.class);
        accspec.setContent(accIntent);

        // Adding all TabSpec to TabHost
        tabHost.addTab(homespec); // Adding home tab
        tabHost.addTab(childspec); // Adding child tab
        tabHost.addTab(accspec); //Adding account tab
        }
+5
source share
3 answers

Use this method to set the current tab after adding a tab to tabHost

tabHost.setCurrentTab(1);  // here pass the tab index its starting from 0
+13
source

use tabHost.setCurrentTab(1);in your onCreate

+3
source
public override void OnResume() 
    {
        base.OnResume();
        tabHost.CurrentTab = 1; //index of the tab you want to set to default.
    }

OnResume() CurrentTab tabHost , .

0

All Articles