5

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 ...

Alan McTavish
  • 121
  • 1
  • 8

1 Answers1

5

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.

Community
  • 1
  • 1
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Hi CommonsWare, Yes, I had come to the same conclusion and am going to leave things as they are. As you say, this situation may be different in later versions but, for now at least, I need to stay backwardly compatible. Thanks anyway. Alan ... – Alan McTavish Oct 10 '11 at 18:59
  • Having uploaded onto a different device I notice that the elements of the spinner and the list are quite difficult to select. I had set the textview in the row xml as 'wrap-content' and it meant that, if there were only 1 or 2 characters in the tv, you had to actually hit one of them to select the row. By changing to 'fill_parent' the tv stretches right across the row and makes selection easier. I thought this might help others. Best wishes, Alan ... – Alan McTavish Oct 10 '11 at 19:04