Question

I am creating a android app. which have a welcome screen where four button (a,b,c,d) have.

when click on the any button(a,b,c,d) its goes to the second activity. next activity have (tabhost)four tabs(a,b,c,d).

how can it work--

when i click on "a" button in welcome screen it goes to "a tab" of second activity,and another tabs are also working.

when i click on "b" button in welcome screen it goes to "b tab" of second activity,and another tabs are also working.

when i click on "c" button in welcome screen it goes to "c tab" of second activity,and another tabs are also working.

when i click on "d" button in welcome screen it goes to "d tab" of second activity,and another tabs are also working.

    public class Dashboard extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dashboard);
    }

    public void helpB(View v) {
        Button clickedButton = (Button) v;
        switch (clickedButton.getId()) {
        case R.id.points:
            Intent i = new      Intent(getApplicationContext(),AndroidActivity.class);
            startActivity(i);
            break;

        case R.id.Search:
            Intent i1 = new Intent(getApplicationContext(),AppleActivity.class);
            startActivity(i1);
            break;

        case R.id.AboutUs:
            Intent i2 = new       Intent(getApplicationContext(),BlackBerryActivity.class);
    startActivity(i2);
            break;

        case R.id.ContactUs:
            Intent i3 = new Intent(getApplicationContext(),WindowsActivity.class);
            startActivity(i3);
            break;

        }
    }

}

and second Activity code -->

     public class MainActivity extends TabActivity {

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

        Resources ressources = getResources();
        TabHost tabHost = getTabHost();

        // Android tab
        Intent intentAndroid = new Intent().setClass(this,
                AndroidActivity.class);
        TabSpec tabSpecAndroid = tabHost
                .newTabSpec("Android")
                .setIndicator("Points",
                        ressources.getDrawable(R.drawable.icon_android_config))
                .setContent(intentAndroid);

        // Apple tab
        Intent intentApple = new Intent().setClass(this, AppleActivity.class);
        TabSpec tabSpecApple = tabHost
                .newTabSpec("Apple")
                .setIndicator("Search",
                        ressources.getDrawable(R.drawable.icon_apple_config))
                .setContent(intentApple);

        // Windows tab
        Intent intentWindows = new Intent().setClass(this,
                WindowsActivity.class);
        TabSpec tabSpecWindows = tabHost
                .newTabSpec("Windows")
                .setIndicator("About us",
                        ressources.getDrawable(R.drawable.icon_windows_config))
                .setContent(intentWindows);

        // Blackberry tab
        Intent intentBerry = new Intent().setClass(this,
                BlackBerryActivity.class);
        TabSpec tabSpecBerry = tabHost
                .newTabSpec("Berry")
                .setIndicator(
                        "Contact us",
                        ressources
                                .getDrawable(R.drawable.icon_blackberry_config))
                .setContent(intentBerry);

        // add all tabs
        tabHost.addTab(tabSpecAndroid);
        tabHost.addTab(tabSpecApple);
        tabHost.addTab(tabSpecWindows);
        tabHost.addTab(tabSpecBerry);

        // set Windows tab as default (zero based)
        tabHost.setCurrentTab(0);
    }

}
Was it helpful?

Solution

Do you need this?

public void helpB(View v) {
Button clickedButton = (Button) v;
int tab = 0;
Intent i = new Intent(getApplicationContext(),MainActivity.class);
switch (clickedButton.getId()) {
case R.id.points:
    tab = 0;
    break;
case R.id.Search:
    tab = 1;
    break;

case R.id.AboutUs:
    tab = 2;
    break;

case R.id.ContactUs:
    tab = 3;
    break;

}
i.putExtra("extra.tab", tab);
startActivity(i);
}

And MainActivity:

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

Resources ressources = getResources();
TabHost tabHost = getTabHost();

// Android tab
Intent intentAndroid = new Intent().setClass(this,
        AndroidActivity.class);
TabSpec tabSpecAndroid = tabHost
        .newTabSpec("Android")
        .setIndicator("Points",
                ressources.getDrawable(R.drawable.icon_android_config))
        .setContent(intentAndroid);

// Apple tab
Intent intentApple = new Intent().setClass(this, AppleActivity.class);
TabSpec tabSpecApple = tabHost
        .newTabSpec("Apple")
        .setIndicator("Search",
                ressources.getDrawable(R.drawable.icon_apple_config))
        .setContent(intentApple);

// Windows tab
Intent intentWindows = new Intent().setClass(this,
        WindowsActivity.class);
TabSpec tabSpecWindows = tabHost
        .newTabSpec("Windows")
        .setIndicator("About us",
                ressources.getDrawable(R.drawable.icon_windows_config))
        .setContent(intentWindows);

// Blackberry tab
Intent intentBerry = new Intent().setClass(this,
        BlackBerryActivity.class);
TabSpec tabSpecBerry = tabHost
        .newTabSpec("Berry")
        .setIndicator(
                "Contact us",
                ressources
                        .getDrawable(R.drawable.icon_blackberry_config))
        .setContent(intentBerry);

// add all tabs
tabHost.addTab(tabSpecAndroid);
tabHost.addTab(tabSpecApple);
tabHost.addTab(tabSpecWindows);
tabHost.addTab(tabSpecBerry);

// set Windows tab as default (zero based)
int tab = getIntent().getExtras().getInt("extra.tab");
tabHost.setCurrentTab(tab);
} 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top