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"?><RelativeLayout   Âandroid:layout_width="match_parent"   Âandroid:layout_height="match_parent">   Â<ImageView       Âandroid:id="@+id/image_view"       Âandroid:layout_width="100dp"       Âandroid:layout_height="100dp"       Âandroid:src="@drawable/gfg"/>   Â<TextView       Âandroid: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   Â@Override   ÂpublicView getView(intposition,@Nullable                                     ÂView convertView,@NonNullViewGroup parent)   Â{       ÂreturninitView(position, convertView, parent);   Â}   Â@Override   ÂpublicView getDropDownView(intposition,@Nullable                                             ÂView 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"?><RelativeLayout   Âandroid:layout_width="match_parent"   Âandroid:layout_height="match_parent"   Âtools:context=".MainActivity">   Â<TextView       Âandroid: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"       Â/>   Â<Spinner       Âandroid: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;   Â@Override   ÂprotectedvoidonCreate(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() {               Â@Override               ÂpublicvoidonItemSelected(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();               Â}               Â@Override               ÂpublicvoidonNothingSelected(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:



