4

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

4 Answers 4

3

Just use:

setDropDownWidth(desiredWidth);
Sign up to request clarification or add additional context in comments.

Comments

1

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?

3 Comments

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

It is straightforward to control the spinner dropdown appearance. Try this:

//Step 1. create the drop down list
static List<String> special_Spinner_Display_List = new ArrayList<String>();  
// add your values to the list...(this is best done using a for loop)
special_Spinner_Display_List.add(item1);
special_Spinner_Display_List.add(item2);  //etc., etc.

//Step 2. build the spinner
ArrayAdapter arrayAdapter_special = new ArrayAdapter(this, 
      R.layout.your_special_spinner, special_Spinner_Display_List);
arrayAdapter_special.setDropDownViewResource(R.layout.special_spinner_dropdown);
specialSpinner.setAdapter(arrayAdapter_special);

//Step 3. create an XML layout file called special_spinner_dropdown where you can 
//style to your heart's content.  Here's an example:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/SpinnerDropdown"
style="?android:attr/spinnerDropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#D5ECED"
android:ellipsize="marquee"
android:singleLine="true"
android:textSize="24sp" 
android:textStyle="bold"
android:textColor="@color/black" />

That's it. Lemme know how it works!

1 Comment

do you know how to set the width of the drop down resource? it doesnt seem to be working.
0

Set dropdown width in xml file of spinner using the tag

android:dropDownWidth="@dimen/desired_width"

Explaination :

mDropDownWidth = pa.getLayoutDimension(R.styleable.Spinner_dropDownWidth,
        ViewGroup.LayoutParams.WRAP_CONTENT);

this field is used for dropdown width when spinner item is initialized in Spinner class

for programatically changing spinner dropdown width above api level 16, use

mSpinner.setDropDownWidth(size_in_pixel);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.