2

I have created an application having action bar and two tabs in it. The tabs and the title of the application are in the same line as given in the screenshot. I want the tabs to be in the line below the title and having equal weight. Please have a look at the code below and help me out. Thanks.

package com.example.myproj;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.app.ActionBar.Tab;
import android.content.Context;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity1 extends Activity {
    public static Context appContext;
    static ArrayList<String> myarray = new ArrayList();
    static ArrayList<String> myarray1;
    static ArrayList<String> myarray2 = new ArrayList();
    static ArrayList<String> myarray21;
    static int check1 = 0;
    static int check2 = 0;
    static int stcount = 0;
    static int csicount = 0;
    static ActionBar.Tab PlayerTab;
    static ActionBar.Tab StationsTab;

    /** Called when the activity is first created. */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        appContext = getApplicationContext();

        ActionBar actionbar = getActionBar();
        actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        PlayerTab = actionbar.newTab().setText("ST");
        StationsTab = actionbar.newTab().setText("CSI");
        // settab1("Testing");
        Fragment PlayerFragment = new AFragment();
        Fragment StationsFragment = new BFragment();

        PlayerTab.setTabListener(new MyTabsListener(PlayerFragment));
        StationsTab.setTabListener(new MyTabsListener(StationsFragment));

        actionbar.addTab(PlayerTab);
        actionbar.addTab(StationsTab);

    }

    public static void settab1(String text) {
        PlayerTab.setText(text);
    }

    public static void settab2(String text) {
        StationsTab.setText(text);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

class MyTabsListener implements ActionBar.TabListener {
    public Fragment fragment;

    public MyTabsListener(Fragment fragment) {
        this.fragment = fragment;
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        Toast.makeText(MainActivity1.appContext, "Reselected!",
                Toast.LENGTH_LONG).show();
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        ft.replace(R.id.fragment_container, fragment);
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        ft.remove(fragment);
    }

}

enter image description here

Vyshakh
  • 632
  • 1
  • 11
  • 29

4 Answers4

6

I had luck using the following reflection 'hack':

private void forceStackedTabs() {
    ActionBar ab = getSupportActionBar();
    if ( ab instanceof ActionBarImpl ) {
        // Pre-ICS
        disableEmbeddedTabs( ab );
    } else if ( ab instanceof ActionBarWrapper ) {
        // ICS
        try {
            Field abField = ab.getClass().getDeclaredField( "mActionBar" );
            abField.setAccessible( true );
            disableEmbeddedTabs( abField.get( ab ) );
        } catch (NoSuchFieldException e) {
            Log.e( TAG, "Error disabling actionbar embedded", e );
        } catch (IllegalArgumentException e) {
            Log.e( TAG, "Error disabling actionbar embedded", e );
        } catch (IllegalAccessException e) {
            Log.e( TAG, "Error disabling actionbar embedded", e );
        }
    }
}
private void disableEmbeddedTabs(Object ab) {
    try {
        Method setHasEmbeddedTabsMethod = ab.getClass().getDeclaredMethod("setHasEmbeddedTabs", boolean.class);
        setHasEmbeddedTabsMethod.setAccessible(true);
        setHasEmbeddedTabsMethod.invoke(ab, false);
    } catch (Exception e) {
        Log.e( TAG, "Error disabling actionbar embedded", e );
    }
}

Please note that I didn't think of this myself, but simply rewrote the code given in this answer: replicate ActionBar Tab(s) with custom view

Community
  • 1
  • 1
JesperB
  • 4,625
  • 1
  • 36
  • 40
0

This is the behavior of the native action bar.

The action bar alone decides whether or not to put the tabs on a second row, and we cannot influence that.(Usually in tablet tabs are inline, in phones are below)

If you want to ensure that your tabs are always tabs, and are always below the action bar, remove the tabs from the action bar and switch to using a ViewPager for your content, with either PagerTabStrip (from the Android Support package, where ViewPager comes from) or the tab indicator from the ViewPagerIndicator project for the tabs themselves. As a side benefit, your contents are now horizontally swipe-able to move between tabs, which is a popular approach.

Just a note. It is not a right pattern. http://developer.android.com/design/patterns/pure-android.html

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
0

Here is a demo which may be help you!I use a customview in actionbar which is contains a TabHost,It can work out well like the picture above,but I don't test it in different screen sizes.

https://github.com/shellshy/actionbar

shellshy
  • 23
  • 1
  • 4
0

In ActionBarImpl.java, in the setHasEmbeddedTabs(boolean hasEmbeddedTabs) method, change the value:

mHasEmbeddedTabs = hasEmbeddedTabs;

to:

mHasEmbeddedTabs = false;
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
Marcus
  • 25
  • 9