Android Spinner Disable Item Dropdown Selecetd Item | Close Dropdown

You are reading Android Spinner Disable Item Dropdown example tutorial.

Learn to hide/close android spinner dropdown programmatically in this example.

Disable an item which is currently selected, from the dropdown menu or pop up.

Generally, we use spinner to allow user to select any one item among multiple options.

Sometimes, developer wants to disable the current selected item in the dropdown menu. In this scenario user will not be able to click on that item from dropdown.

There are some occasions when there is only one option in the spinner. Here, we do not need to show dropdown.

You will learn how to handle all these tricky things about spinner in today’s tutorial.

View Disable Item

Step 1. Closing or Hiding Dropdown Menu Programmatically

You need to close or hide the whole dropdown menu when you have only one item in your spinner.

Let us achieve this goal. First, create a new project in android studio.

Add the below source code in activity_main.xml file

  • I have taken one button and one spinner in above layout file.
  • Button is just saying that below spinner have single value so it has no dropdown menu.

Now write the following code in file

  • First of all, compiler will create one string array variable named “heaven”.
  • This variable have only one item.
  • Then an object of spinner is defined.
  • In onCreate() method, we will find the spinner from layout file using it’s unique ID.
  • After this, compiler will create the array adapter using string array variable. (heaven).
  • And then it will set the adapter to our spinner.

Now read the following code block.

  • Compiler will execute above code when the user touches the spinner.
  • I do not have set any code for this method. It simply means that we are telling the compiler to do nothing when the user touches the spinner.
  • Generally, when the user touches the spinner, system will open the dropdown. But in this case, we are overriding this general approach via above code structure.
  • So we have handle the scenario with only one item in the spinner.

Now let us disable the selected item in dropdown.

Step 2. Disable particular Item in Dropdown

First of all, add the below code in activity_main.xml file

  • Same as we have added earlier. One button and one spinner.
  • Button is telling that selected item will be disabled in dropdown of the below spinner.
  • To handle the complex scenario of disabling a particular item in dropdown, we need to create custom adapter.

Drawable files

  • Before adapter first create a XML file under res->drawable directory.
  • Name of this file should be color_main.xml 

Add the below code structure in color_main.xml file

  • Above will create the view for main layout of the spinner.
  • To change the background color of spinner or text color and size of spinner, you can change these properties in the above textview.
  • Make another XML file in res->drawable directory.
  • Give it a name as color_dropdown.xml

Code block for color_dropdown.xml is as the following

  • This code will generate user interface for dropdown menu.
  • Every item of dropdown will inflate their text color and size from the above textview.
  • We can change text color and size for every spinner item via adapter class.

So let us prepare an adapter class.

Adapter Class

Create a new JAVA class named “”

Write the following lines in “” file

  • Look at the method named hideSpinnerDropDown() method in above code.
  • This method will help us to hide the dropdown when user selects the item.
  • getView() method will create the view for the spinner’s main layout.
  • We will use color_main.xml file in getView() method.
  • getDropDownView() method will help us to make the layout of every single item of the dropdown.

Below code will change the color of the selected item

Following code will disable the current selected item.

Step 3. Ending Updates

Now copy the below code in

  • You should add the three lines before the onCreate() method.
  • integer variable called selectedColor will maintain the current selected position of the spinner.

Write down the following source code in side the onCreate() method.

  • First compiler will find the ID of the spinner.
  • Then it will create the object of ColorAdapter class and will set it to the adapter.
  • When the user selects the spinner item, compiler will update the value of the selectedColor variable.

Download Source Code For Android Spinner Disable Item Dropdown

[sociallocker]Download Source Code For Spinner_DisableItemDropdown[/sociallocker]

Hardik Parsania

I am mobile app developer. I am always passionate about solving complex and real world problems.I have developed many apps with great interest and superior logical skills.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *