I implement a spinner with country name and flag using Array Adapter. But when i select the item it set both values on spinner i.e Name and flag, but I want to set only Flag on spinner look like
Asked
Active
Viewed 1,436 times
1

AskNilesh
- 67,701
- 16
- 123
- 163
-
1try this https://stackoverflow.com/questions/24422236/how-to-dynamically-populate-android-spinner-with-text-image – Gursewak Singh Jul 21 '18 at 09:12
-
Do you have any coding done already? Or just that image? – Ridcully Jul 22 '18 at 08:36
2 Answers
1
As described in the accepted answer of the question, @GursewakSingh referred to, you need to create a custom adapter and implement getView()
and getDropdownView()
to return different views.
For your case, you will want getView()
to return a view that just shows the flag image, and getDropdownView()
to return a view that shows the flag and the name of the country.
The spinner view will use getView()
to show the spinner's normal view and getDropdownView()
to render the items of the dropdown list.

Ridcully
- 23,362
- 7
- 71
- 86
-
thanks for answer but can you please give me example code, I want to display only image on spinner – Jul 21 '18 at 09:40
1
Use this hopefully its work fine
// array of data icons from source
private static Integer[] imageIcons = { R.drawable.a, R.drawable.b,
R.drawable.c };
// call adapter from main class
SimpleImageArrayAdapter adapter = new SimpleImageArrayAdapter(context, imageIcons );
spinner.setAdapter(adapter);
// adapter class
public class SimpleImageArrayAdapter extends ArrayAdapter<Integer> {
private Integer[] images;
SimpleImageArrayAdapter(Context context, Integer[] images) {
super(context, android.R.layout.simple_spinner_item, images);
this.images = images;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return getImageForPosition(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getImageForPosition(position);
}
private View getImageForPosition(int position) {
ImageView imageView = new ImageView(getContext());
imageView.setBackgroundResource(images[position]);
imageView.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
return imageView;
}
}
}

Babul
- 1,076
- 7
- 9
-
-
follow same procedure use string array with country name. use another adapter – Babul Jul 22 '18 at 08:55