In this post, you learn how to create a custom spinner in android.
Steps to create a custom Spinner
- Create a Spinner Element in your XML file.
- Create the custom layout for Spinner element.
- Create the Getter/Setter method for Spinner element.
- Create Adapter for the Spinner.
- Create Item List for the spinner.
- Implement OnItemSelectedListener for Spinner.
- Full Source Code & Output.
Create a Spinner element.
Add spinner element in your layout.xml file.write the following code in your activity_main.xml file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Spinner | |
android:id="@+id/customSpinner" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_margin="10dp" /> | |
Custom Layout for Spinner
create a new layout file name custom_spinner_layout.xml and design spinner layout, here we use ImageView and TextView for Spinner Item.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="horizontal"> | |
<ImageView | |
android:id="@+id/ivCustomSpinner" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:padding="10dp" | |
android:src="@drawable/ic_android_black_24dp" /> | |
<TextView | |
android:id="@+id/tvCustomSpinner" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:padding="10dp" | |
android:text="Androchunk" | |
android:textColor="@android:color/black" | |
android:textSize="20sp" /> | |
</LinearLayout> | |
Getter/Setter method for Spinner element
create a class name CustomItems.java and add a getter/setter method for each variable of the custom item.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CustomItems { | |
private String spinnerText; | |
private int spinnerImage; | |
public CustomItems(String spinnerText, int spinnerImage) { | |
this.spinnerText = spinnerText; | |
this.spinnerImage = spinnerImage; | |
} | |
public String getSpinnerText() { | |
return spinnerText; | |
} | |
public int getSpinnerImage() { | |
return spinnerImage; | |
} | |
} | |
Adapter for the Spinner
Create Adapter class to render data into the spinner. Create class name CustomAdapter.java and extends it to ArrayAdapter<CustomItems>. Override two methods,getView: return view of spinner with selected item position.
getDropDownView: return view of dropdown list of the spinner
here we have same view for both methods so we create new method customView and use it for both methods.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CustomAdapter extends ArrayAdapter<CustomItems> { | |
public CustomAdapter(@NonNull Context context, ArrayList<CustomItems> customList) { | |
super(context, 0, customList); | |
} | |
@NonNull | |
@Override | |
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { | |
return customView(position, convertView, parent); | |
} | |
@Override | |
public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { | |
return customView(position, convertView, parent); | |
} | |
public View customView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { | |
if (convertView == null) { | |
convertView = LayoutInflater.from(getContext()).inflate(R.layout.custom_spinner_layout, parent, false); | |
} | |
CustomItems items = getItem(position); | |
ImageView spinnerImage = convertView.findViewById(R.id.ivCustomSpinner); | |
TextView spinnerName = convertView.findViewById(R.id.tvCustomSpinner); | |
if (items != null) { | |
spinnerImage.setImageResource(items.getSpinnerImage()); | |
spinnerName.setText(items.getSpinnerText()); | |
} | |
return convertView; | |
} | |
} | |
Item List for the spinner
this list contains items which display into the spinner.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ArrayList<CustomItems> customList = new ArrayList<>(); | |
customList.add(new CustomItems("Android", R.drawable.ic_android_black_24dp)); | |
customList.add(new CustomItems("School", R.drawable.ic_school_black_24dp)); | |
customList.add(new CustomItems("satisfied", R.drawable.ic_sentiment_very_satisfied_black_24dp)); | |
customList.add(new CustomItems("shopping_cart", R.drawable.ic_shopping_cart_black_24dp)); | |
customList.add(new CustomItems("whatshot", R.drawable.ic_whatshot_black_24dp)); | |
Implement OnItemSelectedListener for Spinner
To handle the item selection of Spinner attaches onItemSelectedListener which implement two methods.onItemSelected: this method called when item selection is changed.
onNothingSelected: this method call when no item is selected for ex: remove the selected item from the list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { | |
CustomItems items = (CustomItems) adapterView.getSelectedItem(); | |
Toast.makeText(this, items.getSpinnerText(), Toast.LENGTH_SHORT).show(); | |
} | |
@Override | |
public void onNothingSelected(AdapterView<?> adapterView) { | |
} |
Full Source Code & Output.
activity_main.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:gravity="center" | |
android:orientation="vertical" | |
tools:context=".MainActivity"> | |
<Spinner | |
android:id="@+id/customSpinner" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_margin="10dp" /> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="horizontal"> | |
<ImageView | |
android:id="@+id/ivCustomSpinner" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:padding="10dp" | |
android:src="@drawable/ic_android_black_24dp" /> | |
<TextView | |
android:id="@+id/tvCustomSpinner" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:padding="10dp" | |
android:text="Androchunk" | |
android:textColor="@android:color/black" | |
android:textSize="20sp" /> | |
</LinearLayout> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener { | |
private Spinner customSpinner; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
customSpinner = findViewById(R.id.customSpinner); | |
// create spinneritemlist for spinner | |
ArrayList<CustomItems> customList = new ArrayList<>(); | |
customList.add(new CustomItems("Android", R.drawable.ic_android_black_24dp)); | |
customList.add(new CustomItems("School", R.drawable.ic_school_black_24dp)); | |
customList.add(new CustomItems("satisfied", R.drawable.ic_sentiment_very_satisfied_black_24dp)); | |
customList.add(new CustomItems("shopping_cart", R.drawable.ic_shopping_cart_black_24dp)); | |
customList.add(new CustomItems("whatshot", R.drawable.ic_whatshot_black_24dp)); | |
// create Adapter for spinner | |
CustomAdapter customAdapter = new CustomAdapter(this, customList); | |
if (customSpinner != null) { | |
customSpinner.setAdapter(customAdapter); | |
customSpinner.setOnItemSelectedListener(this); | |
} | |
} | |
@Override | |
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { | |
CustomItems items = (CustomItems) adapterView.getSelectedItem(); | |
Toast.makeText(this, items.getSpinnerText(), Toast.LENGTH_SHORT).show(); | |
} | |
@Override | |
public void onNothingSelected(AdapterView<?> adapterView) { | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CustomItems { | |
private String spinnerText; | |
private int spinnerImage; | |
public CustomItems(String spinnerText, int spinnerImage) { | |
this.spinnerText = spinnerText; | |
this.spinnerImage = spinnerImage; | |
} | |
public String getSpinnerText() { | |
return spinnerText; | |
} | |
public int getSpinnerImage() { | |
return spinnerImage; | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CustomAdapter extends ArrayAdapter<CustomItems> { | |
public CustomAdapter(@NonNull Context context, ArrayList<CustomItems> customList) { | |
super(context, 0, customList); | |
} | |
@NonNull | |
@Override | |
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { | |
return customView(position, convertView, parent); | |
} | |
@Override | |
public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { | |
return customView(position, convertView, parent); | |
} | |
public View customView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { | |
if (convertView == null) { | |
convertView = LayoutInflater.from(getContext()).inflate(R.layout.custom_spinner_layout, parent, false); | |
} | |
CustomItems items = getItem(position); | |
ImageView spinnerImage = convertView.findViewById(R.id.ivCustomSpinner); | |
TextView spinnerName = convertView.findViewById(R.id.tvCustomSpinner); | |
if (items != null) { | |
spinnerImage.setImageResource(items.getSpinnerImage()); | |
spinnerName.setText(items.getSpinnerText()); | |
} | |
return convertView; | |
} | |
} | |
If you have any questions or suggestions please leave them in the comments.
Full Source Code : https://github.com/Androchunk/CustomSpinner
Full Source Code : https://github.com/Androchunk/CustomSpinner
Question of the Day
Change Icon of Spinner like this,
Solution :
Official documentation:
AdapterView.OnItemSelectedListener:https://developer.android.com/reference/android/widget/AdapterView.OnItemSelectedListener
1 Comments
Good work sir,
ReplyDeletepublic CustomAdapter(@NonNull Context context, ArrayListcustomList) {
super(context,customList); } // I get a red line under the customList