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
Just use:
setDropDownWidth(desiredWidth);
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?
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!
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);
Related
My requirement is to have Spinner dropdown width similar to the Spinner View.
So that when drop down is expanded, it can completely overlap with Spinner view.
But i got into a problem.
I have created 2 spinners to explain my situation.
Dimension
form_screen_equipment_combo_box_dropdown_vertical_offset = -50dp
One from XML layout. (Gives me expected result)
<Spinner
android:id="#+id/equipment_selector"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/combo_box_bg"
android:dropDownVerticalOffset="#dimen/form_screen_equipment_combo_box_dropdown_vertical_offset"
android:dropDownWidth="match_parent"
android:popupBackground="#drawable/combo_box_bg"
android:spinnerMode="dropdown" />
Another dynamically from java code. (Problematic Spinner)
Spinner view = new Spinner(context, Spinner.MODE_DROPDOWN);
**"view.setPadding(0, 0, 0, 0);" This line solved my problem.**
view.setLayoutParams(new LayoutParams(android.view.ViewGroup.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
CustomSpinnerAdapter equipmentAdapter = new CustomSpinnerAdapter(context, 0, Arrays.asList(values));
view.setAdapter(equipmentAdapter);
view.setPrompt(title);
view.setPopupBackgroundDrawable(context.getResources().getDrawable(R.drawable.combo_box_bg));
view.setDropDownWidth(android.view.ViewGroup.LayoutParams.MATCH_PARENT);
view.setDropDownVerticalOffset(context.getResources().getDimensionPixelOffset(R.dimen.form_screen_equipment_combo_box_dropdown_vertical_offset));
view.setBackground(context.getResources().getDrawable(R.drawable.combo_box_bg));
In second spinner i have unexpected space, i checked through UIAutomator and it seems dropdown view's width is lesser than the Spinner view.
Attached screenshot of second spinner for reference.
Can you please suggest required code changes to fix this problem ?
Thanks in advance.
I have edited my answer above. It seems Spinner widget add padding by default when created programmatically. Had to reset that 0.
I have spinner who collect data from DB and display on real time. The problem I am facing is that when the length of an item is quite long then the spinner Stretch itself and goes out of the screen/view. What I want is to have a fix length of my spinner and it should not stretch itself. Here is the view of my Spinner:
And below is the code:
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Java:
Spinner spinnerRub = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, lables);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Instead of using wrap content for the width , specify the width (say 120dp or..)based on your requirement.
Hi I have a spinner, which contains radio button elements. My spinner loads the elements, but everything is invisible. I think you can understand my situation from the image. an I get any suggestions.
This is the code I am using,
String[] reminder={"5 minutes","10 minutes","15 minutes","20 minutes","25 minutes","30 minutes","35 minutes","45 minutes"};
spinnerAdapter = new ArrayAdapter<String>(mContext,
android.R.layout.simple_spinner_item, reminder);
spinnerAdapter
.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
reminder_spinner.setPrompt("Reminders");
reminder_spinner.setAdapter(spinnerAdapter);
reminder_spinner.setOnItemSelectedListener(new ItemSelect());
The fact that the item text shows up when it is selected leads me to believe it could be a styling issue, ie white font on white background. Here is the layout for simple_spinner_dropdown_item:
<CheckedTextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"/>
I would define another xml layout using the above as a template, and pass that to setDropDownViewResource instead.
add this
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);`
in your code and remove
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);`
I've a spinner pointing on a CursorAdapter.
I set this adapter with a specific view.
I would like to fix the spinner's width, I tried a lot of stuff unsucessfully :
layout_width="100dp" // to force spinner with a specific size KO
reduce the layout width to 100dp // KO
use an ellipsize on a textview contained by the adapter // KO
I would like to block the spinner's width. Do you have an idea ?
You could try adding layout_padding to the left and right of it.
You can try creating a custom textview layout for your spinner to fix some properties.
For example, you can fix the width at 100 dp :
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerItemStyle"
android:singleLine="true"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:ellipsize="end" />
Then you can declare it like this:
mSpinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapterSpinner = ArrayAdapter.createFromResource(this, R.array.spinner_vals, R.layout.custom_simple_spinner_dropdown_item);
I have three main things I want to change with the appearance of a ListView.
1) get rid of horizontal dividers that print between items
2) change font size of test displayed in listview
3) change the padding around each item displayed
It looks like after reading for a while that 2) and 3) should be controlled by changing these properties for the TextView used by the ListView, but I don't yet understand how to do that. Can someone answer these with a little more detail?
1) Set divider height to 0 --- setDividerHeight(0) and set divider color to transparent --- setDivider(new ColorDrawable(0x00FFFFFF))
2) If you're using a list of text views then you can continue using a simple adapter like ArrayAdapter but you will need to create a custom text view. You can add something like this to res/layout
test_text.xml
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize=<SIZE IN SP>
android:textColor="#FFF" />
3) Add padding to your textview above
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text"
android:padding="5dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize=<SIZE IN SP>
android:textColor="#FFF" />
Use your new layout with your ArrayAdapter
eg.
ArrayAdapter(Context context, int resource, int textViewResourceId, List<T> objects)
ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(this, R.layout.test_text, R.id.text, myData);
All of those can be controlled via styles. The docs on styles explain everything you need to do.
In short, you create a style resource - derived from whatever you want to use as your base - where you override all the relevant items, like the divider, the padding the font size.
Then, you either apply that style to your ListView in the XML file, or you create a theme to it and call setTheme() BEFORE you call setContentView().
EDIT: I should mention the obvious answer that kcoppock pointed out - if this is specific to just one element in your layout, just modify that particular element in your layout file.