views:

1452

answers:

1

Is it possible to set the width of a spinner dropdown list in code? I have a spinner populated with integers, and it does not look good with the list expanding to full width. Can I set the width to wrap the content somehow?

Spinner hSpinner = (Spinner) timerView.findViewById(R.id.timer_hour_spinner);
ArrayList<Integer> hList = new ArrayList<Integer>(21);

for (int i = 0; i <= 20; i++) { hList.add(i); }

ArrayAdapter hAdapter = new ArrayAdapter(RemindMe.this, android.R.layout.simple_spinner_item, hList);
hAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

hSpinner.setAdapter(hAdapter);

Thanks!

Linus

+2  A: 

You can change the width of anything in code by adjusting its LayoutParams. The details of that varies by container (LinearLayout vs. RelativeLayout vs. ...).

However, I'm confused why you want to change the width "in code". Why not just set the width to be wrap_content in the layout XML?

CommonsWare
I have set the width of the spinner to "wrap_content" in the layout file, but when I click on the spinner in the app the dropdown list expands to full width. That's why I figured it had to be resized in code if possible. I might have been unclear in my question, but it is the dropdown list that I want to set the width on, not the spinner object itself. Any ideas? And as always, thanks for helping!
aspartame
Ah, sorry, misunderstood. You could try a different layout for the drop-down view. I don't know if the width is being defined by the drop-down layout or is intrinsic to the dialog that is the "drop-down". If it is the latter, there probably is nothing you can do.
CommonsWare
OK, I figured it would be messy if at all possible. I actually skipped the spinner and went for a SeekBar instead. Looks good, so it worked out anyway! Thanks for your help!
aspartame