How to add Custom Spinner in android?

Spinner is a widget that is used to select an item from a list of items. When the user tap on a spinner a drop-down menu is visible to the user. In this article, we will learn how to add custom spinner in the app. If you want to know more about spinner in detail then click on this link.
Approach:
- Create a new file algorithm_spinner.xml and add the following code. Each item in spinner will have this layout, an image view and a textview.
algorithm_spinner.xml
<?xmlversion="1.0"encoding="utf-8"?><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"><ImageViewandroid:id="@+id/image_view"android:layout_width="100dp"android:layout_height="100dp"android:src="@drawable/gfg"/><TextViewandroid:id="@+id/text_view"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignBottom="@+id/image_view"android:layout_alignParentTop="true"android:layout_margin="20dp"android:layout_toEndOf="@+id/image_view"android:gravity="center"android:text="Quick Sort"android:textColor="@android:color/black"android:textSize="20sp"/></RelativeLayout> - Create a new file AlgorithmItem.java and add the below following code. This is the model class which is used to get the algorithm name when the user clicks on any item. Here we define a constructor and a getAlgorithmName method which returns the algorithm name of the object.
AlgorithmItem.java
packageorg.zambiatek.gfgcustomspinner;publicclassAlgorithmItem {privateString algorithmName;publicAlgorithmItem(String countryName){algorithmName = countryName;}publicString getAlgorithmName(){returnalgorithmName;}} - Create a new file AlgorithmAdapter.java and add the following code. Here we define our own Adapter class. It maps the item with its view, providing access to the item`s data in the list of spinner.
AlgorithmAdapter.java
packageorg.zambiatek.gfgcustomspinner;importandroid.content.Context;importandroid.view.LayoutInflater;importandroid.view.View;importandroid.view.ViewGroup;importandroid.widget.ArrayAdapter;importandroid.widget.TextView;importandroidx.annotation.NonNull;importandroidx.annotation.Nullable;importjava.util.ArrayList;publicclassAlgorithmAdapterextendsArrayAdapter<AlgorithmItem> {publicAlgorithmAdapter(Context context,ArrayList<AlgorithmItem> algorithmList){super(context,0, algorithmList);}@NonNull@OverridepublicView getView(intposition,@NullableView convertView,@NonNullViewGroup parent){returninitView(position, convertView, parent);}@OverridepublicView getDropDownView(intposition,@NullableView convertView,@NonNullViewGroup parent){returninitView(position, convertView, parent);}privateView initView(intposition, View convertView,ViewGroup parent){// It is used to set our custom view.if(convertView ==null) {convertView = LayoutInflater.from(getContext()).inflate(R.layout.algorithm_spinner, parent,false);}TextView textViewName = convertView.findViewById(R.id.text_view);AlgorithmItem currentItem = getItem(position);// It is used the name to the TextView when the// current item is not null.if(currentItem !=null) {textViewName.setText(currentItem.getAlgorithmName());}returnconvertView;}} - Add the following code in activity_main.xml file. Here we add our spinner on the layout. This will add a textview and a spinner.
activity_main.xml
<?xmlversion="1.0"encoding="utf-8"?><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="Learn Algorithms"android:textStyle="bold"android:textSize="18sp"android:layout_above="@+id/spinner_algorithm"android:layout_marginStart="10dp"android:layout_marginBottom="25dp"/><Spinnerandroid:layout_margin="5dp"android:id="@+id/spinner_algorithm"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:layout_marginTop="18dp"/></RelativeLayout> - Now add the following code in the MainActivity.java file. Here AlgorithmAdapter class object is made and it acts as an adapter for the spinner and add onItemSelectedListener() to our spinner. When the user tap on any item of the spinner, it gets invoked. It shows a toast with the name of the item that user selected from the list.
MainActivity.java
packageorg.zambiatek.gfgcustomspinner;importandroidx.appcompat.app.AppCompatActivity;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.AdapterView;importandroid.widget.Spinner;importandroid.widget.Toast;importjava.util.ArrayList;publicclassMainActivityextendsAppCompatActivity {ArrayList<AlgorithmItem> algorithmItems;AlgorithmAdapter adapter;@OverrideprotectedvoidonCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initList();Spinner spinner = findViewById(R.id.spinner_algorithm);// we pass our item list and context to our Adapter.adapter =newAlgorithmAdapter(this, algorithmItems);spinner.setAdapter(adapter);spinner.setOnItemSelectedListener(newAdapterView.OnItemSelectedListener() {@OverridepublicvoidonItemSelected(AdapterView<?> parent,View view,intposition,longid){// It returns the clicked item.AlgorithmItem clickedItem = (AlgorithmItem)parent.getItemAtPosition(position);String name = clickedItem.getAlgorithmName();Toast.makeText(MainActivity.this, name +" selected", Toast.LENGTH_SHORT).show();}@OverridepublicvoidonNothingSelected(AdapterView<?> parent){}});}// It is used to set the algorithm names to our array list.privatevoidinitList(){algorithmItems =newArrayList<>();algorithmItems.add(newAlgorithmItem("Quick Sort"));algorithmItems.add(newAlgorithmItem("Merge Sort"));algorithmItems.add(newAlgorithmItem("Heap Sort"));algorithmItems.add(newAlgorithmItem("Prims Algorithm"));algorithmItems.add(newAlgorithmItem("Kruskal Algorithm"));algorithmItems.add(newAlgorithmItem("Rabin Karp"));algorithmItems.add(newAlgorithmItem("Binary Search"));}}
Output:




