1

Is it possible to add a tabhost in an activity that has an action bar ? I want it to look like this:

|--------------------------------------|
|< Title      ActionBar        Settings|
|--------------------------------------|
|                                      | 
|                                      | 
|                                      | 
|                                      | 
|          Tab 1 Content               | 
|                                      | 
|                                      | 
|                                      | 
|                                      | 
|--------------------------------------|
|    Tab 1  |   Tab 2     |   Tab 3    |  
|--------------------------------------|

The reason I don't want to use ActionBar tabs because they do not support bottom tabs. I am using bottom tabs as mentioned in this question here: Android: Tabs at the BOTTOM

And I still want to use ActionBar.

Can this be done ?

Edit:

Here is the code I am using

public class TabsFragment extends Fragment {
    public TabsFragment() {
    }

    private TabHost mTabHost;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        Intent i = new Intent(getActivity(), Timeline.TimelineFragment.class);

         View view = inflater.inflate(R.layout.activity_tab_host, container, false);
         mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
         mTabHost.setup();

        TabHost.TabSpec tab = mTabHost.newTabSpec("my tab content");
        tab.setIndicator("my tab content");
        tab.setContent(i);
        mTabHost.addTab(tab);
        mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
        return view;

    }
}

And TimelineFragment. The fragment_timeline.xml contains normal views.

public static class TimelineFragment extends Fragment {

    public TimelineFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_timeline, container, false);
        return rootView;
    }
}

And this is the activity that is called on launch:

public class TabsFragmentActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tabs_fragment);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new TabsFragment()).commit();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.tabs, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);

    }
}

This code is giving me error:

05-02 20:04:14.525: E/AndroidRuntime(2884): Caused by: java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
05-02 20:04:14.525: E/AndroidRuntime(2884):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:747)
Community
  • 1
  • 1
circler
  • 359
  • 1
  • 3
  • 16

1 Answers1

0

I don't see why not. TabHost is a regular view. As long as you don't need a (deprecated) TabActivity to show multiple activities as the tabs, you can add tabs using instances of the TabContentFactory interface.

A very simple example would be something like:

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab_control_layout);

    TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
    tabHost.setup();

    for (int i = 0; i < 3; i++)
    {
        final String tabText = "Content of tab number " + i;
        TabSpec spec = tabHost.newTabSpec("tab" + i);
        spec.setIndicator("Tab number " + i);
        spec.setContent(new TabHost.TabContentFactory()
        {
            @Override
            public View createTabContent(String tag)
            {
                TextView tabContent = new TextView(MainActivity.this);
                tabContent.setText(tabText);
                return tabContent;
            }

        });

        tabHost.addTab(spec);
    }
}

However, you should check this.

Don't use bottom tab bars

Other platforms use the bottom tab bar to switch between the app's views. Per platform convention, Android's tabs for view control are shown in action bars at the top of the screen instead. In addition, Android apps may use a bottom bar to display actions on a split action bar.

You should follow this guideline to create a consistent experience with other apps on the Android platform and to avoid confusion between actions and view switching on Android.

matiash
  • 54,791
  • 16
  • 125
  • 154
  • 1
    I know that I am not supposed to use bottom tabs on Android, but even some apps like instagram do that. I added the code and it is giving me some error. – circler May 03 '14 at 00:07
  • I see, you are trying to add Activties as tabs. That's why I mentioned the TabActivity thing. I don't think that's possible, because ActionBarActivity is not derived from TabActivity (plus LocalActivityManager is deprecated). If it's feasible for you, use a TabContentFactory for each tab instead. We have used that in our project without problems. – matiash May 03 '14 at 00:49
  • Is there a code example on how to do it and still have an actionbar ? – circler May 03 '14 at 00:57
  • If i want to inflate an xml i should handle all the listeners in the overrided method ? Does it work well in your project ? – circler May 03 '14 at 05:51
  • Yes, we've had no problems with it. – matiash May 03 '14 at 15:59