Spinner in the action bar with a single icon but not selected

I am working on an application with an action bar for navigation. Now I'm trying to add a Spinner to an ActionBar .

I can get my custom icon on an ActionBar . But when I add entries to the counter, the icon is hidden behind the selection box with the first element selected.

I want to have only the icon in the ActionBar and drop-down-list/spinner if it is displayed on the icon.

This is my code:

 private void initializeMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.options, menu); View v = (View) menu.findItem(R.id.menuSort).getActionView(); Spinner spinner=(Spinner)v.findViewById(R.id.bookmarks_menu); Bookmark bookmark1 = new Bookmark(); Bookmark bookmark2 = new Bookmark(); List<Bookmark> list = new ArrayList<Bookmark>(); list.add(bookmark1); list.add(bookmark2); SpinAdapter adapter = new SpinAdapter(getApplicationContext(),0, list); spinner.setAdapter(adapter); } 
+6
source share
3 answers

The Spinner icon is only available in a few steps.

Step 1

Put the icon you want in xml:

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

Step 2

Then, in adapter Spinner , override getView() , for example:

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

Explanation

We need to understand that the getView() in the adapter is the view that will be used when Spinner does not open. We make the visibility gone because we don’t want to see it, so that the background Spinner from xml remains, which I already set to ic_sort_white_24dp in this example (step 1).

Do not mix with getDropDownView() , which is used for parameter strings that will be reset after clicking Spinner .

Bonus screenshot

This is how I look. Hope this helps!

screenshot

+6
source

In your adapter, you need to override getDropDownView (). This should provide the representation used in the counter lines. getView () should then return an ImageView for your icon.

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

+1
source

check its work.

enter image description here

 public class MainActivity extends Activity { final String[] choices = { "Android", "iOS", "RIM" }; final int[] choices_img = { R.drawable.ic_launcher, R.drawable.ios, R.drawable.black }; @SuppressLint("NewApi") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ActionBar actionBar = getActionBar(); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); ArrayAdapter<String> adapter = new ArrayAdapter<String>( MainActivity.this, android.R.layout.simple_dropdown_item_1line, choices); ListViewAdapter adapter1 = new ListViewAdapter(MainActivity.this); actionBar.setListNavigationCallbacks(adapter1, new OnNavigationListener() { @Override public boolean onNavigationItemSelected(int itemPosition, long itemId) { Toast.makeText(MainActivity.this, choices[itemPosition], Toast.LENGTH_SHORT) .show(); return false; } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } public class ListViewAdapter extends BaseAdapter { private LayoutInflater mInflater; public ListViewAdapter(Context con) { // TODO Auto-generated constructor stub mInflater = LayoutInflater.from(con); } public int getCount() { // TODO Auto-generated method stub return choices.length; } public Object getItem(int position) { // TODO Auto-generated method stub // return product_id1.size(); return position; } public long getItemId(int position) { // TODO Auto-generated method stub // return product_id1.get(position).hashCode(); return position; } @SuppressLint("NewApi") public View getView(final int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub final ListContent holder; View v = convertView; if (v == null) { v = mInflater.inflate(R.layout.scan_row1, null); holder = new ListContent(); 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; } } static class ListContent { ImageView img_p; TextView name; } } 

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

0
source

Source: https://habr.com/ru/post/943504/


All Articles