Search code examples
androidactionbarsherlock

ActionbarSherlock - how do I implement a tab navigation once and have that used in my app?


I am looking at the tab navigation example from the ActionbarSherlock download. The examples show one element type per class. But I would like all my screens to reuse the tab navigation.

So I added the tab_navigation.xml to my project

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:padding="20dip">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:text="@string/tab_navigation_content" />
    <TextView
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

And now I am wondering how I can add the code for the tabs to my project? Do I absolutely have to define the tabs in every Activity? Or is there a way to just have one definition I can reuse in multiple places?

Here is how my activities look like:

public class MainActivity extends FragmentActivity 
{   
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ...

Solution

  • Use Fragments

    Whether you have seen this?

    http://wptrafficanalyzer.in/blog/adding-navigation-tabs-containing-listview-to-action-bar-in-pre-honeycomb-versions-using-sherlock-library/