By default in actionbar list navigation width of selected item is as wide as widest item. I would like to "wrap_content" in selected item as it's in google+,gmail,maps android apps.
Does someone know hot to do it?
I tried to override getView of navigation adapter but it doesn't work. It looks that this view is wrapped with another view which has width of widest item. I also tried actionbar.setCustom view with spinner but spinner behaviour is same. It uses widest item width.
Thanks for all suggestions, links and help.
This worked for me. Important part is this. Put the below above code is your adapter and use selectedItemPosition for selecting text from objects array.
Example is given below.
If you need an explanation follow this link: http://coding-thoughts.blogspot.in/2013/11/help-my-spinner-is-too-wide.html
I've met the same problem. It seems Android will call getView() method for all the items, and finally set the width to the widest item's width.
So here's my solution:
Store the current selected section(e.g. section in your example), say selectedSection, in your code. Actually you have to do it in the onNavigationItemSelected() method of NavigationListener.
Override the getView() method of your SpinnerAdapter, always set it's content to selectedSection.
In the onNavigationItemSelected() method of NavigationListener, try to call the notifyDataSetChanged() method of your spinnerAdapter after you set the current mode to selectedSection.
Here's the sample code:
You need to override the getView() method in your adapter and set the layout params into the textview to wrap content. The spinner will resize automatically for the selected item.
I created a custom adapter extends from ArrayAdapter:
After that I override the getView() method changing the LayoutParams of the current view:
In your activity, create adapter instance and set you spinner with it:
When you choose something in the list, the spinner will recreate the view, reorder the selected item to the first position, and resize him self to the content's size.
Create a spinner and add textviews to that. On the spinner set the maxWidth field to whatever value you want and that would handle this. Another option would be to set the
This would mean that the spinner would occupy only 1/3rd of the scrren width.
On the textview you would set :