Android ActionBar (ActionBarCompat) Spinner Dropdown list ?
Asked Answered
H

5

6

I am currently working ActionBar (ActionBarCompat) oriented project, I Need some Clarification about actionbar.

I am using spinner layout like this,

<Spinner 
        android:id="@+id/SpinnerList"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

My code,

public class MainActivity extends ActionBarActivity implements  OnItemSelectedListener{
    private boolean mAlternateTitle = false;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ActionBar bar = getActionBar();
        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

        Spinner spinner = (Spinner) findViewById(R.id.SpinnerList);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.locations, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(this);
}
}

It is displaying normal spinner in my android galaxy ace. I need output like given below, Is that position android 2.3. Please give suggestion.

enter image description here

thanks.

Humdrum answered 23/11, 2012 at 10:12 Comment(4)
You want the bar for android 2.3? actionbarsherlock.com Is a library that supports such. It mimics the behaviour on devices that dont support it.Lundeen
Is it possible action bar using support library??Humdrum
Yes, use ActionBarSherlok to get this spinner. You can see this example #11738000Boutique
The support library doesn't support it im afraid. But sherlock is easy to use. See Georgy answer for example code.Lundeen
F
9

Try this code . In menu/main.xml :

<item
    android:id="@+id/menu_spinner1"
    android:showAsAction="always"
    android:orderInCategory="1"
    android:actionViewClass="android.widget.Spinner" >

in Activity :

private MenuItem mSpinnerItem1 = null;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.


    MenuInflater mi=getMenuInflater();
    mi.inflate(R.menu.main, menu);
    mSpinnerItem1 = menu.findItem( R.id.menu_spinner1);
    View view1 = mSpinnerItem1.getActionView();
    if (view1 instanceof Spinner)
    {
        final Spinner spinner = (Spinner) view1;
        spinner.setAdapter(ad1);


        spinner.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

            }
        });

    }

    return true;
}
Felicita answered 21/2, 2013 at 11:32 Comment(2)
How to use getActionView if I'm supporting android 2.3?Inoperative
the line: View view1 = mSpinnerItem1.getActionView(); requires API 11+. To make it work with API 4+, write: View view1 = MenuItemCompat.getActionView(mSpinnerItem1);Polly
T
4

I feel it's time to start migrating from ABS to ABC easily by ActionBarCompat library!

Solution for spinner (Dropdown list) using ActionBarCompat library

private void setupSpinner(MenuItem item) {
    //  item.setVisible(getSupportActionBar().getNavigationMode() == ActionBar.NAVIGATION_MODE_LIST);
        item.setVisible(ab.getNavigationMode() == ActionBar.NAVIGATION_MODE_LIST);

        View view = item.getActionView();
        Context context = ab.getThemedContext(); //to get the declared theme
        if (view instanceof Spinner) {
            Spinner spinner = (Spinner) view;

            ArrayAdapter<CharSequence> listAdapter =ArrayAdapter.createFromResource(context,
                    R.array.spinner_data,
                    R.layout.support_simple_spinner_dropdown_item);
            listAdapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
            spinner.setAdapter(listAdapter);



        }

make sure you have added following in style.xml:

<style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light"></style>

<style name="AppTheme.MyActionBar" parent="@style/Widget.AppCompat.ActionBar">

More importantly: menu/main.xml should be:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:yourapp="http://schemas.android.com/apk/res-auto" >


    <item
        android:id="@+id/menu_spinner"
        yourapp:actionViewClass="android.widget.Spinner"
        android:visible="true"
        yourapp:showAsAction="always"/>

    <item
        android:id="@+id/action_contact"
        android:icon="@drawable/ic_action_user"

        yourapp:showAsAction="ifRoom"
        android:title="@string/action_contact"/>
       <item
        android:id="@+id/action_contact2"
        android:icon="@drawable/ic_action_user"

        yourapp:showAsAction="never"
        android:title="@string/aboutus"/>

</menu> 

FYI ActionBarSherlock way we used before:

private void setupSpinner(MenuItem item) {
        item.setVisible(getActionBar().getNavigationMode() == ActionBar.NAVIGATION_MODE_LIST);
        View view = item.getActionView();
        if (view instanceof Spinner) {
            Spinner spinner = (Spinner) view;
            spinner.setAdapter(ArrayAdapter.createFromResource(this,
                    R.array.spinner_data,
                    android.R.layout.simple_spinner_dropdown_item));
        }

comparing to ABS ABC you need to set listadapter as R.layout.support_simple_spinner_dropdown_item

Trichloromethane answered 15/10, 2013 at 11:0 Comment(0)
S
3

Try to change

android.R.layout.simple_spinner_item 

to

android.R.layout.simple_list_item_1
Seamy answered 24/4, 2013 at 9:46 Comment(0)
D
2

I think you are using ActionBarCompat, which is only a sample project and is far not enough to be usable.

Try to use open source project ActionBarSherlock. I have been used it in several projects, and it is powerful and easy to use.

Derick answered 23/11, 2012 at 10:36 Comment(2)
Ya i'm used ActionBarCompat previously, now i'm trying ActionBarSharlock i have one in sharlock, i'm using spinner and applied style style="@style/Theme.Sherlock" this one not working displaying normal spinner only, i need above image output using spinner, is it possible??Humdrum
@Ramachandran we can use ABC also!! see my answerTrichloromethane
I
1

If you will add spinner to your action bar through menu's xml like this

android:actionViewClass="android.widget.Spinner"

it will appear just like other menu actions - to the right.

If you want to make it appear to the left, you can do another way.

screenshot

I'm doing it inside a fragment (but you can do it with activity), and using Toolbar, so my code is:

activity_main.xml (theme and popupTheme are important for arrow color) :

...
<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:subtitleTextColor="@android:color/white"
        app:titleTextColor="@android:color/white" />
...

action_bar_spinner_title.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@android:id/text1"
    style="?attr/spinnerDropDownItemStyle"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:ellipsize="marquee"
    android:singleLine="true"
    android:textColor="@android:color/white" />

action_bar_spinner_list.xml:

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@android:id/text1"
    style="?android:attr/spinnerDropDownItemStyle"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/colorPrimary"
    android:ellipsize="marquee"
    android:singleLine="true"
    android:textColor="@android:color/white" />

Inside fragment:

private static final String[] arItems = {"Item 1", "Item 2", "Item 3", };

private Toolbar toolbar;
private Spinner actionBarSpinner;

public ActionBarSpinnerFragment() {
}

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

    return inflater.inflate(R.layout.fragment_action_bar_spinner, container, false);
}

@Override
public void onStart() {
    super.onStart();

    final Activity activity = getActivity();
    if(activity != null) {
        ActionBar actionBar = ((AppCompatActivity) activity).getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayShowTitleEnabled(false);

            toolbar = activity.findViewById(R.id.toolbar);
            actionBarSpinner = new Spinner(actionBar.getThemedContext());
            ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<>(activity,
                    R.layout.action_bar_spinner_title, android.R.id.text1, arItems);
            spinnerAdapter.setDropDownViewResource(R.layout.action_bar_spinner_list);
            actionBarSpinner.setAdapter(spinnerAdapter);
            actionBarSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    FragmentManager fragMngr = getFragmentManager();
                    if(fragMngr != null) {
                        FragmentTransaction fragTransact = fragMngr.beginTransaction();
                        fragTransact.replace(R.id.frag_frame,
                                TextViewFragment.newInstance(arItems[position]));
                        fragTransact.commit();

                    } else {
                        Log.w(LOG_TAG, "ActionBarSpinnerFragment: FragmentManager == null");
                    }
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {
                }
            });
            toolbar.addView(actionBarSpinner);
        } else {
            Log.w(LOG_TAG, "ActionBarSpinnerFragment.onStart: actionBar == null");
        }
    } else {
        Log.w(LOG_TAG,  "ActionBarSpinnerFragment.onStart: activity == null");
    }
}

@Override
public void onStop() {
    super.onStop();
    if(toolbar != null && actionBarSpinner != null) {
        toolbar.removeView(actionBarSpinner);
    } else {
        Log.w(LOG_TAG, "ActionBarSpinnerFragment: toolbar == " + toolbar
                + "; actionBarSpinner == " + actionBarSpinner);
    }
}
Incogitable answered 18/7, 2018 at 3:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.