Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • TabHosting using TabActivity in android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 674
    Comment on it

    TabHosting can be used to explore and switch between different views or functional aspects of your app. In this tutorial we will make an activity and host three activities to its tab by using tab hosting . Tab layout is used for this purpose.

    this code can be used to set the tabs.

    
            // Set TabChangeListener called when tab changed
    
                    tabHost = getTabHost();
            tabHost.setOnTabChangedListener(this);
    
            TabHost.TabSpec spec;
            Intent intent;
    
            /************* TAB1 ************/
            // Create Intents to launch an Activity for the tab (to be reused)
            intent = new Intent().setClass(this, Tab1.class);
            spec = tabHost.newTabSpec("First").setIndicator("").setContent(intent);
    
            // Add intent to tab
            tabHost.addTab(spec);
    
            /************* TAB2 ************/
            intent = new Intent().setClass(this, Tab2.class);
            spec = tabHost.newTabSpec("Second").setIndicator("sd").setContent(intent);
            tabHost.addTab(spec);
    
            /************* TAB3 ************/
            intent = new Intent().setClass(this, Tab3.class);
            spec = tabHost.newTabSpec("Third").setIndicator("").setContent(intent);
            tabHost.addTab(spec);
    
            // Set drawable images to tab
            tabHost.getTabWidget().getChildAt(1)
                    .setBackgroundResource(R.drawable.games);
            tabHost.getTabWidget().getChildAt(2)
                    .setBackgroundResource(R.drawable.sports);
    
            // Set Tab1 as Default tab and change image
            tabHost.getTabWidget().setCurrentTab(0);
            tabHost.getTabWidget().getChildAt(0)
                    .setBackgroundResource(R.drawable.foods);
    
    

    Put the following code for the onTabChange Listner

    @Override
        public void onTabChanged(String tabId) {
    
            /************ Called when tab changed *************/
    
            // ********* Check current selected tab and change according images
            // *******/
    
            for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
                if (i == 0)
                    tabHost.getTabWidget().getChildAt(i)
                            .setBackgroundResource(R.drawable.foods);
                else if (i == 1)
                    tabHost.getTabWidget().getChildAt(i)
                            .setBackgroundResource(R.drawable.games);
                else if (i == 2)
                    tabHost.getTabWidget().getChildAt(i)
                            .setBackgroundResource(R.drawable.sports);
            }
    
            Log.i("tabs", "CurrentTab: " + tabHost.getCurrentTab());
    
            if (tabHost.getCurrentTab() == 0)
                tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab())
                        .setBackgroundResource(R.drawable.foods);
            else if (tabHost.getCurrentTab() == 1)
                tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab())
                        .setBackgroundResource(R.drawable.games);
            else if (tabHost.getCurrentTab() == 2)
                tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab())
                        .setBackgroundResource(R.drawable.sports);
    
        }
    

    Put the three activities that has to be hosted .

    Refrence from http://www.androidhive.info/2011/08/android-tab-layout-tutorial/,

    http://developer.android.com/design/building-blocks/tabs.html

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: