Showing posts with label actionbar. Show all posts
Showing posts with label actionbar. Show all posts

Saturday, May 25, 2013

ActionBar/ActionBarSherlock Tabs

Navigation Tabs:
ActionBar supports built-in Tabs or drop-down lists that you can use to change the Fragment is currently  visible. We are going to work with ActionBarSherlock library so we can have backwards compatibility.

Note 1 :  Either Tabs or drop-down lists but not both in ActionBar.
Note 2 :  Every Tab is associated with one Fragment. To change Fragments by using the Tabs,  perform a FragmentTransaction each time a Tab is selected.

Display Tabs :  

Add Tabs :

You can add Tabs with the addTab() method. The first Tab added is the selected one therefore the visible.
Code is like below :

Implement ActionBar.TabListener :

We 're going to split it into 3 parts :

  1. First part the implementation of the Fragments.
  2. Second part the implementation of ActionBar.TabListener 
  3. Third part is to add the Tabs using the TabListener we created before.
The result we expect is the screenshot below :

1) Fragments 
Fragment class is just a basic Fragment class in which we only inflate the layout :

And the corresponding XML layout :

We repeat this process for Fragment_2.

2) TabListener 
TabListener's implementation is the most difficult part of the work.

Our constructor is  public TabListener(Activity activity, String tag, Class<T> clz)  where activity is the host Activity, tag is the identifier Tag of the fragment and cls the Fragment's class so we can instantiate the fragment.

Callbacks in this Interface will handle the event. Specifically when a Tab is clicked then onTabSelected() will execute the Fragment transaction and add the fragment to the Activity.

In our case the Tab content will fill the activity's layout, so our activity doesn't needs a layout. Each fragment is placed in the default root ViewGroup, which we can refer to with the ID.

3) MainActivity
Because we have to deal with Fragments our MainActivity  will extend FragmentActivity ( or just Activity for Android 3.0 and up ) but in our case with ActionBarSherlock will extend the SherlockFragmentActivity.

All we are doing here is to get the ActionBar item, and add Tabs with the addTab() method.
We set a TabListener for each Tab by calling the setTabListener().
Also we set the tab's title and/or icon with setText() and/or setIcon().

That's all ! Check all the code and/or fork the project from github here so you can see by yourself how it works. 

Monday, May 6, 2013

ActionBarSherlock configuration

I'm creating a custom Theme, let's call it TestABS with parent the HOLO Theme ( in our case Sherlock ).
So now in my styles.xml I add the line :
<style name="Theme.TestABS" parent="@style/Theme.Sherlock"></style> 

Later I'll edit Manifest file like this :


When Activity starts, the system adds ActionBar and the overflow menu by calling onCreateOptionsMenu().
This method inflates an XML resource which defines the menu items. This files is in the folder /res/menu. In those items on the XML file I have to add the android:showAsAction keyword, so now the items will appear as Action Items in my ActionBar and not Options Menu.

So a simple item should be like :

        android:title="Text Here" />

This works as long as I have an ActionBar in my Activity( When do I have an ActionBar ? When I add   android:theme="@style/Theme.HOLO" in my manifest file for android 3.0 and greater. For lower versions I need Theme.Sherlock . In both cases I can declare mine custom theme like I did above with TestABS ).

Links :
1) Adding ActionBarSherlock to your Project
2) Adding Items to the ActionBar 
3) The Overflow menu ( As a rule of thumb you should always use ifRoom, if you want the icon to be part of your action bar and you should use never if you want the item to always be part of the overflow menu. )