Disable particular item in listview android programmatically

How to disable particular listview item non-clickable,non-selectable in android app.

In this tutorial we are creating an listview with multiple items. Now after that we are disabling any particular item inside listview. This makes the item non-clickable so app user can see the icon but dose not have functionality to select it. So here is the complete step by step tutorial for Disable particular item in listview android programmatically.

android-project-download-code-button

How to Disable particular item in listview android programmatically.

Code for MainActivity.java file.

 package com.disableitemlistview_android_examples.com;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class MainActivity extends Activity {

 ListView listview;
 String[] subjects = new String[] {
 "Android",
 "PHP",
 "Blogger",
 "SEO",
 "WordPress",
 "Android Studio"
 };
 ArrayAdapter<String> arrayadapter;
 
 Button button;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 listview = (ListView)findViewById(R.id.listView1);
 button = (Button)findViewById(R.id.button1);
 
 arrayadapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, subjects);
 
 listview.setAdapter(arrayadapter);
 
 
 button.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 //Disable the second item of list view. 
 listview.getChildAt(1).setEnabled(false);
 
 }
 });
 
 }

}

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.disableitemlistview_android_examples.com.MainActivity" >

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:text="Disable 2nd Item from ListView" />

 <ListView
 android:id="@+id/listView1"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_alignParentBottom="true"
 android:layout_centerHorizontal="true"
 android:layout_below="@+id/button1" >
 </ListView>

</RelativeLayout>

Screenshots:

disable-php

Disable particular item in listview android programmatically

Click here to download Disable particular item in listview android programmatically project with source code.

Leave a Reply

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