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.

Leave a Reply

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