Search This Blog

Support my cause by downloading

Sunday, 2 September 2012

Action Bar with Tab in android

In android latest version, TabLayout had deprecated because of its some constraint. Now we Action bar that mainly replace Menu in recent version. Most importantly we can use action bar as replacement of TabLayout also.

For using Action Bar, we need to use getActionBar(); method that will return action bar object. even you can use Menu xml to create a Action Bar but here i am creating Action Bar dynamically.

 ActionBar bar = getActionBar();  
                bar.addTab(bar.newTab().setText("Tab 1").setTabListener(this));  
                bar.addTab(bar.newTab().setText("Tab 2").setTabListener(this));  
                bar.addTab(bar.newTab().setText("Tab 3").setTabListener(this));  
                bar.addTab(bar.newTab().setText("Tab 4").setTabListener(this));  
                bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM  
                          | ActionBar.DISPLAY_USE_LOGO);  
                bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);  
                bar.setDisplayShowHomeEnabled(true);  
                bar.setDisplayShowTitleEnabled(false);  
                bar.show();  

you can hide Action bar if you want 

 bar.hide()  

The we can perform and detect action state of action bar tab by implementing TabListener .So out of final code will be like this
Action with tab
Action Bar with Tab
                                               
But this source will run only the version from 3.0
                                                                  Download Source Code

Android News and source code