Disable single item inside spinner in android programmatically

How to make only single spinner item not selectable in android dynamically.

In this tutorial we are creating an spinner with multiple choices but instead we are disabling an single item inside spinner so that makes the item non-selectable. If user selected the disable item then a toast message appears on screen and says that this item is temporary out of list, So please select another one. So here is the complete step by step tutorial for Disable single item inside spinner in android programmatically.

android-project-download-code-button

How to Disable single item inside spinner in android programmatically.

Code for MainActivity.java file.

 package com.disableitemspinner_android_examples.com;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.view.ViewGroup;

public class MainActivity extends Activity {

 Spinner spinner;
 String[] spinnerItems = new String[]{
 "ANDROID",
 "PHP",
 "BLOGGER",
 "WORDPRESS",
 "SEO"
 };
 List<String> spinnerlist;
 ArrayAdapter<String> arrayadapter;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 spinner = (Spinner) findViewById(R.id.spinner1);

 spinnerlist = new ArrayList<>(Arrays.asList(spinnerItems));
 
 arrayadapter = new ArrayAdapter<String>(MainActivity.this,R.layout.spinner_item_textview,spinnerlist){
 @Override
 public boolean isEnabled(int position){
 if(position == 2)
 {
 //Disable the third item of spinner.
 return false;
 }
 else
 {
 return true;
 }
 }

 @Override
 public View getDropDownView(int position, View convertView,ViewGroup parent) 
 {
 View spinnerview = super.getDropDownView(position, convertView, parent);
 
 TextView spinnertextview = (TextView) spinnerview;
 
 if(position == 2) {
 
 //Set the disable spinner item color fade .
 spinnertextview.setTextColor(Color.parseColor("#bcbcbb"));
 }
 else {
 
 spinnertextview.setTextColor(Color.WHITE);
 
 }
 return spinnerview;
 }
 };

 arrayadapter.setDropDownViewResource(R.layout.spinner_item_textview);
 
 spinner.setAdapter(arrayadapter);
 
 
 }
}

Code for activity_main.xml layout file.

 <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.disableitemspinner_android_examples.com.MainActivity" >

 <Spinner
 android:id="@+id/spinner1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true" />

</RelativeLayout>

Code for spinner_item_textview.xml file.

 <?xml version="1.0" encoding="utf-8"?>
<TextView
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/spinnerItem"
 android:layout_width="250dp"
 android:layout_height="wrap_content"
 android:textColor="#fefcfe"
 android:padding="10dp"
 android:textSize="18dp"
 android:background="#c67f04" />

Screenshot:

Disable single item inside spinner in android programmatically

Click here to download Disable single item inside spinner in android programmatically project with source code.

3 Comments

  1. I have tried this code and it is not working on my end. Tried implementing it the same way but it didn’t work.

  2. hello, Thanks for the above code, although i am having trouble in changing the colour of already selected values , i have taken positions of all previously selected options into a list, can you please check whether there is any error in code below
    @Override
    public View getDropDownView(int position, View convertView,
    ViewGroup parent) {
    View view = super.getDropDownView(position, convertView, parent);
    TextView tv = (TextView) view;
    for (int i=0;i<checklist.size();i++){
    if(position==(Integer)checklist.get(i)) {
    // Set the disable item text color
    //tv.setTextColor(Color.GRAY);
    tv.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    Toast.makeText(Scheduler.this,"Already Selected Device, Please Choose Another", Toast.LENGTH_SHORT).show();
    }
    });
    }
    else {
    tv.setTextColor(Color.BLACK);
    }

    }
    return view;
    }

Leave a Reply

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