20

Is it possible to have both, an spinner/dropdown at the top and tabs just below in the action bar? I want to use the spinner at the top of the action bar as view-switch controller (i.e. table view or chart view) while the tabs below is going to show different lists of data.

The following screen shot illustrates exactly what I want to achieve:

enter image description here

If it's not possible having both modes in the action bar, I could put an TabWidget at the top of my content view. However, I'm little bit unsure if I should have one fragment or one activity per tab.

Jeff Axelrod
  • 27,676
  • 31
  • 147
  • 246
Ismar Slomic
  • 5,315
  • 6
  • 44
  • 63

4 Answers4

3

EDIT:

Is it possible to use dropdown AND tabs as navigation in the action bar?

No. The code in ActionBarView (where the setNavigationMode(int) call ends up) uses a switch statement, so the modes can't be combined.

Some combination of the action bar navigation modes and my previous answer is still a valid option though: You could use a custom view in your Action Bar and use a ViewPager for the content. Each tab in the ViewPager would be a fragment that you can update manually (change the view type) from the parent activity when the spinner is changed.

Side note, there are 3rd party libraries such as ViewPagerExtensions that give the fixed tabs appearance without having to use the ActionBar tabs.

roflharrison
  • 2,300
  • 23
  • 26
  • In this case I will need to change the navigation mode of the action bar from NAVIGATION_MODE_LIST to NAVIGATION_MODE_TAB. This will make the spinner in action bar dissapear. – Ismar Slomic Aug 02 '12 at 05:37
  • No, in this case the action bar would have nothing to do with navigation, and the tabs wouldn't be part of the action bar. EDIT: changed my description in the answer – roflharrison Aug 02 '12 at 07:58
  • Could you please refer to an example? I'm not sure if I understand what you mean. Thanks! – Ismar Slomic Aug 02 '12 at 09:13
  • If you follow the links to ViewPager and/or ViewPagerExtensions they both have examples of Tabbed fragment pagers. One of the answers to this question http://stackoverflow.com/questions/8941098/full-width-spinner-in-actionbar has a custom spinner view in the action bar – roflharrison Aug 02 '12 at 09:29
  • 2
    I also found following tutorial useful: http://thepseudocoder.wordpress.com/2011/10/13/android-tabs-viewpager-swipe-able-tabs-ftw/ – Ismar Slomic Aug 02 '12 at 10:52
  • Another nice link is here: http://storkme.org/2011/12/tabs-done-right-viewpager-and-fragments/ – Ismar Slomic Aug 16 '12 at 14:21
2

It's possible. I was trying to achieve an identical UI to what you have shown, and was stuck until I tried something pretty simple but a bit confusing as well.

What I've done is set a Spinner as my custom view for the ActionBar, and then:

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

Here's an example of what I mean.

Tabs Code:

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

actionBar.addTab(actionBar.newTab().setText("Today").setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("List").setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("Month").setTabListener(this));

In the parent activity for this activity, I've done this:

Context contextTheme = new ContextThemeWrapper(this, R.style.ActionBarSpinner);
Spinner actionBarSpinner = new Spinner(contextTheme);

// Specify a SpinnerAdapter to populate the dropdown list.
actionBarSpinnerAdapter = new ActionBarSpinnerAdapter(actionBar.getThemedContext(), dropdownValues);
actionBarSpinner.setAdapter(actionBarSpinnerAdapter);

// Set up the dropdown list navigation in the action bar.
actionBarSpinner.setOnItemSelectedListener(this);

actionBar.setCustomView(actionBarSpinner);

This effectively makes gives me a custom view for the ActionBar, but then I can set the navigationMode to tabs to allow me to have the tabs I want. Set the appropriate event listeners on each item, and voila!

LukeWaggoner
  • 8,869
  • 1
  • 29
  • 28
  • I don't recommend doing this. When rotating the device, the actionbar is hidden and the custom view is placed to the right of the tabs, which at least in my case is not what I want. Also, you'd need to provide to ActionBarSpinner style in order to complete your answer. And, you need to hide the title, otherwise your spinner is placed to the right of the title: actionBar.setDisplayShowTitleEnabled(false); – Maragues Aug 25 '14 at 09:03
1
Is it possible to use dropdown AND tabs as navigation in the action bar?

Not possible in default lib using bat you can create custom action bar view and set that one possible

try this one code

action_bar.xml 

<LinearLayout
    android:id="@+id/actionbar_linearLayout_menu"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="2dip"
    android:gravity="right" >

    <ImageView
        android:id="@+id/actionbar_imageView_menu_search"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="5dip"
        android:padding="8dp"
        android:src="@drawable/button_search_selector" />

    <ImageView
        android:id="@+id/actionbar_imageView_menu_myaccount"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="5dip"
        android:padding="8dp"
        android:src="@drawable/button_myaccount_selector" />

    <ImageView
        android:id="@+id/actionbar_imageView_menu_settings"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="5dip"
        android:padding="8dp"
        android:src="@drawable/button_settings_selector" />
</LinearLayout>

<LinearLayout
    android:id="@+id/actionbar_linearLayout_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_toLeftOf="@+id/actionbar_linearLayout_menu" >

    <Spinner
        android:id="@+id/actionbar_spi_menu"
        style="@style/MenuSpinnerTextViewItem"
        android:layout_width="80dip"
        android:layout_height="34dip"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="6dip"
        android:background="@drawable/spinner_bg"
        android:paddingLeft="3dip"
        android:spinnerMode="dropdown"
        android:visibility="gone" />

    <com.smartdeal.util.SmartDealTextView
        android:id="@+id/actionbar_txt_title"
        style="@style/Style_Text_Bold"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginTop="4dip"
        android:gravity="left"
        android:padding="5dip"
        android:text="Smart Deal"
        android:textColor="@android:color/white"
        android:textSize="18dip" />
</LinearLayout>

<LinearLayout
    android:id="@+id/actionbar_linearLayout_search"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:background="@drawable/serch_bg"
    android:visibility="gone" >

    <ImageView
        android:id="@+id/actionbar_imageView_searchMenu_search"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/search_icon_selected" />

    <EditText
        android:id="@+id/actionbar_edt_searchMenu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dip"
        android:layout_weight="1"
        android:background="@null"
        android:singleLine="true"
        android:textColor="@android:color/white"
        android:textSize="18dip" >
    </EditText>

    <ImageView
        android:id="@+id/actionbar_imageView_searchMenu_cancel"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal"
        android:layout_marginLeft="5dip"
        android:src="@drawable/search_cancel" />
</LinearLayout>

BaseActivity.java

{
final ActionBar ab = getSupportActionBar();
        ab.setDisplayHomeAsUpEnabled(true);
        final LayoutInflater inflater = (LayoutInflater) getSystemService("layout_inflater");
        ab.setDisplayShowTitleEnabled(false);
        // ab.setTitle("Settings");
        View view = inflater.inflate(R.layout.actionbar_view, null);

        linLayoutMenu = (LinearLayout) view
                .findViewById(R.id.actionbar_linearLayout_menu);
        linLayoutSearch = (LinearLayout) view
                .findViewById(R.id.actionbar_linearLayout_search);
        linLayoutTitle = (LinearLayout) view
                .findViewById(R.id.actionbar_linearLayout_title);

        txtTitle = (TextView) view.findViewById(R.id.actionbar_txt_title);
        spiMenu = (Spinner) view.findViewById(R.id.actionbar_spi_menu);

        menuSearch = (ImageView) view
                .findViewById(R.id.actionbar_imageView_menu_search);
        menuMyaccount = (ImageView) view
                .findViewById(R.id.actionbar_imageView_menu_myaccount);
        menuSettings = (ImageView) view
                .findViewById(R.id.actionbar_imageView_menu_settings);
        menuSearchCancel = (ImageView) view
                .findViewById(R.id.actionbar_imageView_searchMenu_cancel);

        menuSearch.setOnClickListener(this);
        menuMyaccount.setOnClickListener(this);
        menuSettings.setOnClickListener(this);
        menuSearchCancel.setOnClickListener(this);

        if (this instanceof HistoryActivity) {
            txtTitle.setVisibility(View.GONE);
            spiMenu.setVisibility(View.VISIBLE);

            List<String> list = new ArrayList<String>();
            list.add("History");
            list.add("Activity");

            MenuListAdapter menuList = new MenuListAdapter(this, list);
            spiMenu.setAdapter(menuList);

        } else {
            txtTitle.setVisibility(View.VISIBLE);
            spiMenu.setVisibility(View.GONE);
        }

        ab.setCustomView(view);
        ab.setDisplayShowCustomEnabled(true);
}

ViewActivity.java

TabListener {

private ListView historyList;
private ArrayList<String> list;
LinearLayout linLayoutHistory, linLayoutActivity;
private ActionBar actionBar;

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.....);

    initComponent();

}

private void initComponent() {
    // setActionBarHomeUpAsEnable(true);

    linLayoutActivity = (LinearLayout) findViewById(R.id.history_activity_linLayout_activity);
    linLayoutHistory = (LinearLayout) findViewById(R.id.history_activity_linLayout_history);

    historyList = (ListView) findViewById(R.id.history_activity_listView);
    list = new ArrayList<String>();

    spiMenu.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            // TODO Auto-generated method stub


        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    });

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub

    actionBar = getSupportActionBar();

    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    ActionBar.Tab month_tab = actionBar.newTab()
            .setText(getResources().getString(R.string.tab_month))
            .setTabListener(this);
    ActionBar.Tab year_tab = actionBar.newTab()
            .setText(getResources().getString(R.string.tab_year))
            .setTabListener(this);
    ActionBar.Tab all_tab = actionBar.newTab()
            .setText(getResources().getString(R.string.tab_all))
            .setTabListener(this);

    actionBar.addTab(month_tab);
    actionBar.addTab(year_tab);
    actionBar.addTab(all_tab);

    return super.onCreateOptionsMenu(menu);

}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub

}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub

}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub

}
Android
  • 1,417
  • 9
  • 11
1

You can also create a custom action provider to achieve a spinner in the action bar. So there is no need to create a whole action bar. By doing this and use tabs as navigation mode your goal is fulfilled.