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 a spinner I want to add two columns of checkboxes in the drop down of spinner.
I have tried this solution android spinner dropdown checkbox
but this does not let me show a hint "Please Select..." when no item is selected.
Here is what I want.
https://drive.google.com/open?id=11cr0iPgs9vwULeGeTAtllFR-3KW-6YvM
Unfortunately, standard spinner doesn't allow to show 'nothing selected' state after adapter is set. As soon as you set adapter first element become selected.
You should create custrom spinner for "nothing selected" position. See here http://stackoverflow.com/a/12221309
Seems, you should join solution from this link with link from your question
I am using a Spinner and ArrayAdapter to populate items in Spinner. I want to make the spinner multi-selectable i.e. user can select multiple items and should also be able to deselect to none. My code is as below,
spnAdapter = new ArrayAdapter<String>(this,android.R.layout.select_dialog_multichoice,items);
spnAdapter.setDropDownViewResource(android.R.layout.simple_list_item_multiple_choice);
spnProducts.setAdapter(spnAdapter);
Here the items is an array list.
This code gives me a spinner with check boxes but it does not allow multiple selection. What should I do to make it multiple selection?
Thanks in advance.
My app displays a Spinner in its actionbar. The text displayed in the spinner can be truncated when it's too long. The problem is that when I press on the spinner, the dropdown items are using the same width than the spinner. This means that some entries are truncated as well even though there's enough space on the right of the drop down item to display the truncated text...
Is there a way to use a wrap_content kind of width for the drop down items ?
For now I'm using the default android layout:
android.R.layout.simple_spinner_dropdown_item
I want to show indication at the bottom of spinner list. Suppose i have 10 values in array list when i click on the spinner then it shows first 5 rest shown after scroll the list. I want to show indication at bottom to scroll the list for more values.