Custom Listview Click Issue on Items in Android

Custom ListView click issue on items in Android

The issue is that Android doesn't allow you to select list items that have elements on them that are focusable. I modified the checkbox on the list item to have an attribute like so:

android:focusable="false"

Now my list items that contain checkboxes (works for buttons too) are "selectable" in the traditional sense (they light up, you can click anywhere in the list item and the "onListItemClick" handler will fire, etc).

EDIT: As an update, a commenter mentioned "Just a note, after changing the visibility of the button I had to programmatically disable the focus again."

Click is not working on the Listitem Listview android

The first thing what you have to note here is, whenever there are Clickable elements like Buttons or ImageButtons present in your ListView element, they take the control of click events. And so your ListView won't get the chance to accept the click event.

What you simply have to do is, set the focusable attribute to false for the Button or ImageButton you have in your ListView. But still they will work without any problem and also your ListView's onListItemClick will also work.

Try this,

        <Button  android:id="@+id/textsize_increaser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/back_button"
android:focusable="false"
android:text=" A + "/>

Here I have added this android:focusable="false" and it works fine. try it.

Click on list item of a ListView doesn't respond

You should define on all of the child objects in the item listview (TextView, ImageView etc.):

android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"

And for the root item RelativeLayout / LinearLayout and so, define:

android:clickable="false"
android:descendantFocusability="blocksDescendants"
android:focusable="false"
android:focusableInTouchMode="false"

If you won't define those, they will "catch" the click event.
And if you have a Custom listView adapter, just check that you override:

@Override
public boolean isEnabled(int position)
{
return true;
}

Can't click the Listview items with custom adapter

You Can also use listview setOnItemnClickListener method for item click

Check Following Code :

listView1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {

String item = ((TextView)view).getText().toString();

Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();

}
});

Use this code in your xml and check. Hope this will work.

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">


<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior"></ListView>

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="56dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:layout_collapseMode="pin" />


<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary" />

</android.support.design.widget.AppBarLayout>


</android.support.design.widget.CoordinatorLayout>

Android listview item click is not working

I too faced this issue, I overcome from this issue by setting click listener to convertView of custom adapter. I don't know is this good approach but it solved my issue.

public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
KeepassDBGroupv1 rowItem = getItem(position);

LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.custom_list_row, null);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.textView_group_entry_name);
holder.imageView = (ImageView) convertView.findViewById(R.id.imgView_group_entry_icon);
holder.type = (TextView) convertView.findViewById(R.id.textView_group_entry_type);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();

holder.name.setText(rowItem.getGroupName());
holder.type.setText("Group");
Drawable d = context.getResources().getDrawable(context.getResources().getIdentifier("ic"+Integer.toString(rowItem.getImageId()), "drawable", context.getPackageName()));
holder.imageView.setImageDrawable(d);
//App.getDB().drawFactory.assignDrawableTo(holder.imageView, context.getResources(), rowItem.icon);
convertView.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Log.v("OpenDbListAdapter ","List View Clicked");
}
});
return convertView;
}

Android : How to set onClick event for Button in List item of ListView

You can set the onClick event in your custom adapter's getView method..

check the link http://androidforbeginners.blogspot.it/2010/03/clicking-buttons-in-listview-row.html



Related Topics



Leave a reply



Submit