Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Reduce the width of a spinner's drop-down box

I have looked at most of the questions on this subject and there does not seem to be a definitive answer. I have three spinners in different rows of a table view. Using the weight attribute, the cells in the table and,consequently, the spinners and text views all have the same width.

However, when a spinner is clicked, the drop-down list appears at full width and covers the whole table. There must be a way of restricting the width of the list. It only holds a number with a maximum of 4 digits after all.

I have seen a note on R.attr.dropDownWidth which looks as though it may help but I have no idea how to implement this method, let alone if it will actually work.

Any help on this or another method that will restrict the width of the drop-down to about 100 dip will be really appreciated - especially if sample code is included.

Thanks,

Alan ...

like image 506
Alan McTavish Avatar asked Oct 09 '11 11:10

Alan McTavish


1 Answers

However, when a spinner is clicked, the drop-down list appears at full width and covers the whole table.

That is because it is really a pop-up dialog.

There must be a way of restricting the width of the list.

This is difficult, as you are not the one creating the dialog. I do not even see how you could accomplish this by extending Spinner with your own subclass, given the way Spinner is written. You might have to clone the Spinner source code, move it to another package (or class name), and alter performClick() to change the size of the AlertDialog using the techniques outlined in How to control the width and height of the default Alert Dialog in Android?.

And, bear in mind that the behavior of Spinner is significantly different on Honeycomb, and it remains to be seen how it looks on Ice Cream Sandwich. We do not have the Honeycomb source code, so my clone-the-class solution is going to give you problems on newer devices.

I have seen a note on R.attr.dropDownWidth which looks as though it may help but I have no idea how to implement this method, let alone if it will actually work.

It is not a method, but rather an attribute you could set in a style. However, based on the source code, it appears that this is only used for AutoCompleteTextView's drop-down.

like image 134
CommonsWare Avatar answered Sep 27 '22 17:09

CommonsWare