Android : onItemClickListener not responding –|– Clickable row/item of Custom ListView

It happens that sometimes you are making an interactive app and not using buit-in classes but your own custom classes to have better and prettier interface. For that you often end up creating a custom list view that inflates layout (for a single row) from a separate XML file. But what if you want to make each row as well as items with in that row clickable separately? Many of new developers and beginners follow general approach by applying onItemClickListener  onto the listView  but do not get any response when the touch or click on a certain row of that List.

Follow this approach  to get rid of hassle.

Open the XML file where you have designed the layout for single row of the custom ListView and include the following lines into your layout’s parent in that file.

android:descendantFocusability="blocksDescendants"

Basically when you have some elements and sub-child elements in a single row, They get focus and your actual row is un-selectable or un- clickable. All you need is to remove focus from those elements.  The good thing about this parameter is that your descendants or child elements (for example button and event listener on it) will continue to work as normal.

For better understanding, here is the layout that I made according to my app’s UI need.

<?xml version="1.0" encoding="utf-8"?>
 <FrameLayout  xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_height="wrap_content"
     android:layout_width="fill_parent"
     android:id="@+id/listrow"
     android:descendantFocusability="blocksDescendants">
    
     <ImageView 
         android:layout_width="fill_parent" 
         android:layout_height="wrap_content"
         android:adjustViewBounds="true"
         android:background="@drawable/note_add"
         android:contentDescription="@string/txt_title" android:src="@null" />    
         
     <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:orientation="vertical"
         android:layout_gravity="center_horizontal">
     
             <TextView android:id="@+id/txt_datetime" android:layout_width="wrap_content"
                 android:layout_height="wrap_content" android:contentDescription="@string/txt_title"
                 android:text="@string/txt_title" android:layout_marginRight="30dp"
                 android:layout_marginTop="23dp" android:textColor="#000000"
                 android:textSize="15sp" android:layout_gravity="right|top" />
     
             <TextView android:id="@+id/txt_note" android:layout_width="200dp" android:textColor="#000000"
                 android:layout_height="wrap_content" android:contentDescription="@string/txt_title"
                 android:text="@string/txt_title" android:layout_marginLeft="70dp" android:paddingRight="20dp"
                 android:textSize="15sp" android:layout_gravity="left|center" 
                 android:maxLines="2"/>
     
             <ImageButton android:id="@+id/btn_del_note"
                 android:layout_width="wrap_content" android:layout_height="wrap_content"
                 android:background="@null" android:src="@android:drawable/ic_delete"
                 android:contentDescription="@string/btn_delete"
                 android:layout_gravity="right|bottom" android:adjustViewBounds="true"
                 android:layout_marginRight="30dp"  android:layout_marginTop="5dp"/>
     
     </LinearLayout>
 </FrameLayout>

Here is the main XML where my ListView resides:

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent" android:layout_height="match_parent"
     android:background="@drawable/bg" android:orientation="vertical">
    <ImageView android:id="@+id/addnotebtn"
             android:layout_width="wrap_content" android:layout_height="wrap_content"
             android:layout_margin="0dp" android:paddingRight="20dp"
             android:paddingTop="10dp" android:contentDescription="@string/txt_title"
             android:src="@drawable/note_btn_add" android:layout_gravity="right" />
         
         <ListView
             android:id="@+id/list"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_gravity="center_horizontal"
             android:layout_marginBottom="0dp"
             android:layout_marginTop="0dp"
             android:background="@null"
             android:cacheColorHint="#00000000"
             android:clipChildren="true"
             android:divider="@null"
             android:focusable="false"
             android:focusableInTouchMode="false"
             android:footerDividersEnabled="false"
             android:headerDividersEnabled="false"
             android:paddingBottom="10dp"
             android:paddingTop="20dp"
             android:scrollbars="none"
             android:smoothScrollbar="true" />
    </RelativeLayout>

———————————————————————————————————–

If you look closely you will find the following parameter in the parent layout item (which is <FrameLayout> in my case).

 android:descendantFocusability="blocksDescendants" 

This is the real thing that does the work so don’t forget to add it, if you want to make your custom listview selectable/clickable!

Open the activity class file where you want to handle the display of this list and make a list using custom adapter like this:

ListView list = (ListView) findViewById(R.id.list);
         try{
             // get all the notes from database
             DBHelper dbhelper = new DBHelper(this);
             dbhelper.opendb();
             List<Note> notes = dbhelper.getAllNotes();
             dbhelper.closedb();
             if(!notes.equals(null)){
                 list.setAdapter(new CustomAdapter(this, notes));
                 list.setOnItemClickListener(new OnItemClickListener() {
                         public void onItemClick(AdapterView<?> parentAdapter, View row,    int position, long rowid)
 {
                         // TODO Auto-generated method stub
                         Intent intent  = new Intent(ViewNotes.this, EditNotes.class);
                         intent.putExtra("noteid", Long.valueOf(parentAdapter.getChildAt(position).findViewById(R.id.btn_del_note).getTag().toString()));
                         startActivity(intent);
                         finish();
                     }
                 });
             }
             else{
                 notes.clear();
                 notes = null;
             }
         }catch (Exception e) {
             // TODO: handle exception
             Toast.makeText(this, "The drectory is empty. Add new Notes", Toast.LENGTH_SHORT ).show();
         }

I have populated my custom list with the data that I fetched from the database (It’s totally up to your choice how you populate your list). Next I set an Item click listener on to my list. With in that listener, I have overridden the onItemClick() method where I have started a new activity based on the values of the elements in that row. Note the parameters of this method

AdapterView<?> parentAdapter === The Parent Adapter or the list that contains the rows
View row === A single row that has been clicked 
int position === The position of the row, that has been clicked, in the ListView
long rowid === Id of the row (the id that is maintained in your R file) 

Based on the nature of your app you can use these parameters to achieve app specific tasks. Hope that this post helps you. If you have any query please feel free to comment. And do mention, if you find something wrong or if you have a better approach.

Advertisements

2 thoughts on “Android : onItemClickListener not responding –|– Clickable row/item of Custom ListView

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s