Skip to content Skip to sidebar Skip to footer

Spinner In Action Bar With Only Icon But Not The Selected Option

I am working on a app with action bar for navigation. Now I am trying to add a Spinner to the ActionBar. I am able to get my custom icon on the ActionBar. But when I add entries in

Solution 1:

Icon-only Spinner is achievable through a few steps.

Step 1

Put the icon you want in the xml:

<Spinner...android:background="@drawable/ic_sort_white_24dp" />

Step 2

Then, in the adapter of the Spinner, override getView(), like this:

ArrayAdapter<String> dataAdapter = newArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list){
        @OverridepublicViewgetView(int position, View convertView, ViewGroup parent) {
            // this part is needed for hiding the original viewView view = super.getView(position, convertView, parent);
            view.setVisibility(View.GONE);

            return view;
        }
};

Explanation

We need to understand that getView() in the adapter is the view that will be used when the Spinner is not opened. We make the visibility gone because we don't want to see it, so that what is left is the background of Spinner from the xml, which I already set to ic_sort_white_24dp in this example (Step 1).

Do not mix up with getDropDownView() which is used for the rows of options that will drop downed after the Spinner is clicked.

Bonus Screenshot!

This is how mine look like. Hope it helps!

screenshot

Solution 2:

In your adapter, you need to override getDropDownView(). That should provide the view used in the rows of the spinner. getView() should then return an ImageView for your icon.

See these 2 answers on similar questions for more details and examples:

Solution 3:

check this its working..

enter image description here

publicclassMainActivityextendsActivity {

    final String[] choices = { "Android", "iOS", "RIM" };
    finalint[] choices_img = { R.drawable.ic_launcher, R.drawable.ios,
            R.drawable.black };

    @SuppressLint("NewApi")@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ActionBaractionBar= getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

        ArrayAdapter<String> adapter = newArrayAdapter<String>(
                MainActivity.this, android.R.layout.simple_dropdown_item_1line,
                choices);
        ListViewAdapteradapter1=newListViewAdapter(MainActivity.this);
        actionBar.setListNavigationCallbacks(adapter1,
                newOnNavigationListener() {
                    @OverridepublicbooleanonNavigationItemSelected(int itemPosition,
                            long itemId) {
                        Toast.makeText(MainActivity.this,
                                choices[itemPosition], Toast.LENGTH_SHORT)
                                .show();
                        returnfalse;
                    }
                });
    }

    @OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        returntrue;
    }

    publicclassListViewAdapterextendsBaseAdapter {

        private LayoutInflater mInflater;

        publicListViewAdapter(Context con) {
            // TODO Auto-generated constructor stub
            mInflater = LayoutInflater.from(con);
        }

        publicintgetCount() {
            // TODO Auto-generated method stubreturn choices.length;
        }

        public Object getItem(int position) {
            // TODO Auto-generated method stub// return product_id1.size();return position;
        }

        publiclonggetItemId(int position) {
            // TODO Auto-generated method stub// return product_id1.get(position).hashCode();return position;
        }

        @SuppressLint("NewApi")public View getView(finalint position, View convertView,
                ViewGroup parent) {
            // TODO Auto-generated method stubfinal ListContent holder;
            Viewv= convertView;
            if (v == null) {
                v = mInflater.inflate(R.layout.scan_row1, null);
                holder = newListContent();

                holder.name = (TextView) v.findViewById(R.id.textView1);

                holder.img_p = (ImageView) v.findViewById(R.id.imageView1);

                // holder.total_rate.setOnClickListener(mOnTitleClickListener1);

                v.setTag(holder);
            } else {

                holder = (ListContent) v.getTag();
            }

            holder.img_p.setBackground(getResources().getDrawable(
                    choices_img[position]));
            holder.name.setText("" + choices[position]);

            return v;
        }
    }

    staticclassListContent {

        ImageView img_p;
        TextView name;

    }

}

if you want only icon then just use image view in scan_row1.xml

Post a Comment for "Spinner In Action Bar With Only Icon But Not The Selected Option"