Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ActionBar (ActionBarCompat) Spinner Dropdown list ?

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.

like image 518
Jeeva123 Avatar asked Nov 23 '12 10:11

Jeeva123


4 Answers

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;
}
like image 121
pradeep Avatar answered Nov 18 '22 07:11

pradeep


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

like image 35
LOG_TAG Avatar answered Nov 18 '22 07:11

LOG_TAG


Try to change

android.R.layout.simple_spinner_item 

to

android.R.layout.simple_list_item_1
like image 3
gattshjoty Avatar answered Nov 18 '22 08:11

gattshjoty


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.

like image 2
faylon Avatar answered Nov 18 '22 09:11

faylon