I want to populate a Spinner with items which have a main text and a sub text, just like Android Studio shows when building the view on the "Designer" tab.
So far I was able to fill it with the main text only.
I am doing it via code. Using a SimpleAdapter.
I tried the following but with no success, it just gives me same result (only main text):
Spinner spinner = (Spinner) findViewById(R.id.mySpinner);
List<Map<String, String>> itens = new ArrayList<>();
Map<String, String> item = new HashMap<>(2);
item.put("text", "MAIN TEXT");
item.put("subText", "SUB TEXT");
itens.add(item);
SimpleAdapter adapter = new SimpleAdapter(spinner.getContext(), itens,
android.R.layout.simple_spinner_dropdown_item,
new String[]{"text", "subText"},
new int[]{android.R.id.text1, android.R.id.text2}
);
// i am not sure what this does
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
I had the same problem and have used the OP's code as a base to create this solution:
final Spinner spinner = (Spinner)fragmentView.findViewById(R.id.spinner);
List<Map<String, String>> items = new ArrayList<Map<String, String>>();
Map<String, String> item0 = new HashMap<String, String>(2);
item0.put("text", "Browse aisles...");
item0.put("subText", "(Upgrade required)");
items.add(item0);
Map<String, String> item1 = new HashMap<String, String>(2);
item1.put("text", "Option 1");
item1.put("subText", "(sub text 1)");
items.add(item1);
Map<String, String> item2 = new HashMap<String, String>(2);
item2.put("text", "Option 2");
item2.put("subText", "(sub text 2)");
items.add(item2);
SimpleAdapter adapter = new SimpleAdapter(getActivity(), items,
android.R.layout.simple_spinner_item, // This is the layout that will be used for the standard/static part of the spinner. (You can use android.R.layout.simple_list_item_2 if you want the subText to also be shown here.)
new String[] {"text", "subText"},
new int[] {android.R.id.text1, android.R.id.text2}
);
// This sets the layout that will be used when the dropdown views are shown. I'm using android.R.layout.simple_list_item_2 so the subtext will also be shown.
adapter.setDropDownViewResource(android.R.layout.simple_list_item_2);
spinner.setAdapter(adapter);
You can also substitute android.R.layout.simple_spinner_item
and/or android.R.layout.simple_list_item_2
with your own custom views (that would typically reside in your layout
folder).
This is a much better solution than PhoneGap!! :D
You will have to create a custom ArrayAdapter that creates a custom view for your Spinner dropdown. This link gives a good example: How to customize the Spinner dropdown view
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With