I have an android spinner that's populated by a list of strings using an ArrayAdapter and it operates fine, however because of the way the spinner is displayed I'm running into a display height problem with the list items.
At first glance, it would seem that the ArrayAdapter can use a single layout for displaying options which leads to the problem I'm having. When displaying the current item in the spinner (when the user is not selecting a new item from the list) the spinner pads the text so that the spinner is a reasonable size for clicking on. However, when the user taps on it and brings up the list to select a new item, the list items presented are way too small height-wise. If I use an item layout that presents the list items at a reasonable height, then the spinner itself becomes exorbitantly huge due to its own padding of the list item.
Any ideas on how I can manage the height of these two item display modes so that effectively they display with the same height value instead of the spinner height being larger than the list item display height?
I've run into this issue myself a while ago, and it turned out that I need to use different layouts for dropdown and display
I have this code:
adapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, cGroups,
new String[] {
"name", "_id"
}, new int[] {
android.R.id.text1
});
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Yes, the above answer is correct.
It took me forever to find this, because it's wrong in the sdk samples for 2.2 Android. And I had a hard time accepting that.
Here's a snippet from
samples/android-12/Spinner/src/com/android/example/spinner/SpinnerActivity.java:
this.mAdapter = ArrayAdapter.createFromResource(this, R.array.Planets,
android.R.layout.simple_spinner_dropdown_item);
while it should have android.R.layout.simple_spinner_item there instead and simple_spinner_dropdown_item should only be used for the dropdown items. Otherwise the spinner arrow get streched and it draws dropdown selection circle to the display, too.
Related
I am designing a mobile app for jewellery shop. In Android, for the jewellery product description page, I have a Spinner with some ring sizes with their text 5 to 25 set by using a custom array list adapter.
I have a Radio Group of 18 carat or 22 carat that work along with the Spinner, and if different Radio Button for carat is selected, then the spinner text should change along with it only for a few ring sizes, for example 17 - Ships in 24 hours if the ring size is ready to ship and 18 if the ring size is to be newly made based upon customer's order.
To achieve this, if a different size Radio Button is selected, I create a new array list, add all the new text again for spinner and then use array adapter again.
ArrayAdapter adapter = new ArrayAdapter(Description.this, android.R.layout.simple_spinner_item, sizes);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s1.setAdapter(adapter);
In doing so, the Spinner automatically defaults to the first item 5 always, even if previously the spinner item selected was, for example, 17 size for a different carat Radio Button.
And also, the other details corresponding to other Text Views in the same activity, for example price, discount, making charge, gold weight, delivery date, whether product is ready to ship, etc. automatically default to the ring size 5.
So, is it possible to change the text of a specific Spinner item programmatically if a different carat Radio Button is selected? Is it possible to achieve that without creating a new array list, add all the new text to it and making use of array adapter again? And if so how?
If sizes is an ArrayList, to change a particular item in the spinner try using
sizes.set(index,newItem)
If you want to change the 3rd item to 10,
sizes.set(2,10) //I am assuming the sizes arraylist is integer arraylist. if not change to required data type
then execute
adapter.notifyDataSetChanged() // This will refresh you spinner.
instead of re-setting the adapter on the spinner. And then if you want, execute
s1.setSelection(2)
The s1.setAdapter(adapter); should only be executed once(when the spinner is initialised).
public void refreshSpinner(int index,int newValue){
sizes.set(index,newValue);
adapter.notifyDataSetChanged();
//Only if you want to select the new value
s1.setSelection(index);
}
Try below code,
Select the Spinner element at index position 2
(It will select third element/item from Spinner)
s1.setSelection(2);
I have set a dynamic value in a spinner . I am using following code for the same.
spinner_generalbooks.setAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, new String[]{"Author","ISBN","Keyword","Title"}));
It is working fine, but I have a problem with the view of the spinner . If we select simple_spinner_item, it is ok in normal state, but when we want to change it, the rows are very narrow and not CheckedTextView whereas in normal spinner options comes with CheckedTextView. If we select simple_spinner_dropdown_item, the options come with CheckedTextView, but in normal state, it looks different as in the pic
(First one is default spinner and second one is using simple_spinner_dropdown_item).
I want to show the spinner just like as default spinner. How to make it?
Android will take the layout specified in the adapter and use it for the control and the items unless you specify the view-resource separately. The way through this is to set the layout to simple_item in the ArrayAdapter constructor and then set the layout dropdown_item separately in a call to setDropDownViewResource().
ArrayAdapter newAdapter = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_spinner_item, new String[]{"Author","ISBN","Keyword","Title"});
newAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_generalbooks.setAdapter(newAdapter);
If you whant to customize the visual of your Spinner, it'll be more simple to create your own component. A spinner is just a Layout that contains text, image and that display a list in a popup. Create a custom layout for your item and use a new BaseAdapter object to bind your datas.
http://thinkandroid.wordpress.com/2010/01/13/custom-baseadapters/
hope i could help
I have a spinner with data content, but the spinner is very small. How can i put the spinner diferrent?
I guess you're talking about the "drop down" item list (in the other case you would just have to change the size of the Spinner in the xml file).
You have differents way to set the Adapter actually, for example, when you declared your ArrayAdapter (or another Adapter), you can set a layout to put data in a View.
See Android.R.layout
For example : "simple_spinner_item" is really small so you'd rather use another one like "simple_dropdown_item_1line"
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item,spinnerArray);
Can you show us some code if this is not what you are looking for ? I'm not sure of what you are talking (if it's the Spinner element or the list)
I'm running into a problem: when I use a non-trivial type of Spinner item, the Spinner displays the drop-down list someplace other than on the Spinner.
(Note: most of this description is identical to https://stackoverflow.com/questions/4188443/android-doesnt-honor-selection-change-with-custom-spinner-items, but the problem I'm reporting here is slightly different. I've split these up so it's clear where to direct different replies to)
My goal was to have a slightly more fancy display for each item in the spinner, and so I started by creating a layout that contains several items, one of which is the target TextView (lbl2, in this case)
I then attempt to set up the Spinner (my eventual goal is to populate the spinner programmatically, so I'm not using resources to set this up) using:
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(
this, R.layout.spinner_fancy, R.id.lbl2);
adapter.add("Item #1");
adapter.add("Item #2");
adapter.add("Item #3");
spinner.setAdapter(adapter);
When I run the program it looks (mostly) good - the Spinner is, in fact, rendering the goofy-looking multi-color, vertical layout of three textviews for each item, and it's correctly substituting Item #1, Item #2, and Item #3 for lbl2. I can even click on the spinner & bring up the drop-down list of choices.
This problem is that the items aren't displayed over the spinner. Instead they're just kind of floating over the activity, a bit further down. Hopefully this picture will help clarify: Floating Spinner Elements http://www.freeimagehosting.net/uploads/bf9f584156.png
EDIT: Thanks for the vote up - I've fixed up the image so it's now inline!
Android Spinners work in that way... The list of items is shown in "dialog mode".
You can add a title to the list using this in the XML layout (in the spinner section):
android:prompt="Select a fancy item..."
My spinner control is displaying fine no problem. When someone is in create mode they are able to selected a value which I am able to record and store in my database.
But I have a problem when someone is in edit mode and I want to display the list but have one of the items (say the third) selected. I can't see how to do that. All the tutorials talk about how to populate the spinner but not how to preset one as being selected.
this.ddlCategory = (Spinner)this.findViewById(R.id.add_edit_place_ddlCategory);
ArrayAdapter adpt = new ArrayAdapter(this, android.R.layout.simple_spinner_item, categories);
adpt.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
this.ddlCategory.setAdapter(adpt);
Please help
Cheryl
You can use one of the setSelection methods to programatically select a value.