ListView onItemClick not called - android

I have a ListView with a custom adapter and list items containing TextView(s) only. The list items have an OnItemClick method set in the onCreate callback method.
templatesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d(DEBUG_TAG, "templatesListView onClick()");
//item is selected from the cursor to get necessary data
Log.d(DEBUG_TAG, "ListView count: " + templatesListView.getCount());
Log.d(DEBUG_TAG, "messagesCursor count: " + messagesCursor.getCount());
if (position >= messagesCursor.getCount()) {
Log.d(DEBUG_TAG, "Unable to access element " + position + ", it does not exist in the messagesCursor. Cursor count: " + messagesCursor.getCount());
}
messagesCursor.moveToPosition(position);
final String selectedItemName = messagesCursor.getString(1);
AlertDialog.Builder builder = new AlertDialog.Builder(SendMessageActivity.this);
builder.setTitle(selectedItemName).setMessage("Do you want to use template: "+selectedItemName+"?");
//Use template onClick
builder.setPositiveButton("Use", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int x) {
messageEditText.setText(selectedItemName);
}
});
//Cancel onClick
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int x) {
}
});
builder.show();
}
});
The ListView in the activity layout file is defined as:
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/templatesListView"
android:layout_alignParentRight="true"
android:clickable="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/sendButton" />
The list item is defined in a separate layout file as:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/name_textView" />
The onClick method is called correctly when I run the app on Android 4.4.4, but when I run it on Android 5.1.1 it is not called at all.
The list item layout has been also created for v21+ separately, please find the code below:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Medium Text"
android:id="#+id/name_textView"
android:singleLine="true"
android:textColor="#color/foreground_material_light"
android:theme="#android:style/Widget.Material.Light.Button.Borderless" />
Do you guys know what should I change to make it work on API level 21+ ? Is that a matter of the xml file only (attributes?) or should I change the implementation? Cheers!

Just remove this,
android:clickable="true"
from your ListView. It is actually blocking the clicks from getting transferred to the individual rows.

SOLVED: I think the additional layout file was confusing for the application. It was named exactly the same as the original one, but with (v21) added at the end of it. After removing the second file it works fine.

Looking at this closely related problem and the selected answer, I think you should make some changes to your code.
Seeing that you have the LinearLayout which has a ListView as a child view - and this ListView has the LinearLayout as a child, which in turn has TextView as a child? In this case, perhaps the child views of your ListView should have the onClickListener. This means setting the following attributes to your name_textView:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
android:id="#+id/name_textView" />
Then keep the templatesListView as :
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/templatesListView"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/sendButton" />
And finally, change your list-item linear layout as follows:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:weightSum="1">
Give it a try and let me know if this helps.

Related

Not able to make Listview items clickable

I am not able to make my list view clickable.I have tried all the given solutions for same type of questions and had tried to change focusable and
focusableintouchmode values to both true and false.
Sometimes it works (i.e text in listview gets clicked) after i click the button 10/12 times.
Below are my xml and java files.
I have also attached the screenshot of logcat when my row number 9 got clicked after i pressed it more than 20 times.
enter image description here
Please help..
Activity_card.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/rlLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.hppc.business.Card">
<ListView
android:id="#+id/lvUsers"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:descendantFocusability="blocksDescendants">
</ListView>
</RelativeLayout>
entry1.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:orientation="horizontal"
android:padding="13dp">
<ImageView
android:layout_width="90dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="#drawable/image" />
<linearlayout>.....</linearlayout>
</linearLayout>
card.java
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent i = new Intent(Card.this, Manual.class);
Log.i("HelloListView", "You clicked Item: " + id + " at position:" + position);
Log.e("YESSSS",Integer.toString(position));
startActivity(i);
}
});
you have to make your list view focusable:
android:focusable="true"
android:focusableInTouchMode="true"

OnItemClickListener not called with custom layout row in ListView

I have such a problem. I'm trying to get id of row in ListView. To do so I'm using setOnItemClickListener:
wyswietlenieZadan = (ListView)findViewById(R.id.ListView);
mAdapter = new ArrayAdapter<>(this, R.layout.zadania, R.id.opisZadania, listaZadan);
wyswietlenieZadan.setAdapter(mAdapter);
wyswietlenieZadan.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "Twoje id: " +id, Toast.LENGTH_SHORT).show();
}
});
Here is my zadania.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_gravity="center_vertical">
<TextView
android:id="#+id/opisZadania"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"
android:textSize="20sp"
android:layout_marginStart="32dp"
android:layout_alignParentStart="true"
android:layout_marginTop="12dp"
android:clickable="true"
android:contextClickable="true" />
<ImageButton
android:layout_width="wrap_content"
app:srcCompat="#android:drawable/ic_menu_close_clear_cancel"
android:id="#+id/task"
android:layout_marginEnd="19dp"
android:layout_height="wrap_content"
style="#android:style/Widget.ImageButton"
android:background="#android:drawable/ic_delete"
android:onClick="usunZadanie"
android:layout_alignTop="#+id/opisZadania"
android:layout_alignParentEnd="true"
android:focusable="false" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#android:drawable/ic_menu_edit"
android:id="#+id/imageView"
android:layout_toStartOf="#+id/task"
android:layout_alignTop="#+id/task"
android:onClick="edytujZadanie"
android:focusable="false" />
</RelativeLayout>
It doesnt work but when I use simple_list_item_1.xml as layout instead of zadania.xml, I can get id of each record. Anybody knows why it doesn't work with my custom layout?
Change:
Toast.makeText(getApplicationContext(), "Twoje id: " +id, Toast.LENGTH_SHORT).show();
to:
Toast.makeText(getApplicationContext(), "Twoje id: " +position, Toast.LENGTH_SHORT).show();
as the position variable gives you the position of the item you just clicked. If you clicked the first item it will show Twoje id:0 and if you clicked the second item it would show Twoje id:1 and so on...

setOnItemClickListener doesn't work when setOnLongClickListener is being used - Android

As the subject suggests, setOnItemClickListener doesn't work when setOnLongClickListener is being used, the layout I'm using in the base adapter is below,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:baselineAligned="false"
android:descendantFocusability="blocksDescendants"
android:paddingBottom="20dp"
android:paddingTop="5dp">
<com.mikhaellopez.circularimageview.CircularImageView
android:id="#+id/image1"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="2dp"
android:focusable="false"
app:border_color="#EEEEEE"
app:border_width="4dp"/>
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/contacts_grid_image"
android:layout_centerHorizontal="true"
android:focusable="false"
android:text="Name"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="11dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/name"
android:layout_centerHorizontal="true"
android:focusable="false"
android:text="Mobile number"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="9dp"/>
</RelativeLayout>
and I'm using this,
image.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
ClipData.Item item = new ClipData.Item((CharSequence) v.getTag());
String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN};
ClipData dragData = new ClipData("test", mimeTypes, item);
MyDragShadowBuilder myShadow = new MyDragShadowBuilder(holder.image);
if (groups.size() > holder.position) {
v.startDrag(dragData, myShadow, null, 0);
return true;
}
});
and this,
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Log.i(TAG, "clicked");
}
});
Now when I tap on the image the item click doesn't work, but when i click on the text views it works, what am i doing wrong?
Also note that when I remove the image.setOnLongClickListener() the whole gridview item becomes clickable again.
This should be the expected behaviour for GridView, because the child view is clickable, even though it is only handling the OnLongClick only.
As a workaround, you can
setOnClickListener for ImageView to perform the same action as you would in setOnItemClickListener
consider using setOnItemLongClickListener, but this would affect the whole RelativeLayout
use onTouchListener, but this would requires more works

LongClick with Custom TextView in ListVIew NOT WORKING

I have a ListView which has a Custom TextView which i got from here: FlowTextView
The problem is if i use a normal default TextView everything works perfectly fine,But if i use this custom text view all the Click Events (LongClick and OnClick) dont work if clicked inside the text view i.e. the click event only work at places where this Custom TextView doesn't reach to (The edges of my List Row).
I have tried
android:descendantFocusability="blocksDescendants"
android:focusable="false"
android:focusableInTouchMode="true"
But none of it worked, or maybe i did it in the wrong way...if you think any of these methods work please elaborate on how to use this
My implementation in the FlowTextView
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity"
android:background="#ff71ff34">
<uk.co.deanwild.flowtextview.FlowTextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/textView2"
android:text="Description"
android:textSize="24sp"
android:background="#1b1b1f"
android:textColor="#android:color/white">
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#ffffbb52"/>
</uk.co.deanwild.flowtextview.FlowTextView>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CodeLearn 1"
android:background="#1b1b1f"
android:textColor="#android:color/white"/></RelativeLayout>
inside OnCreate
ListView mainList = (ListView)findViewById(R.id.listView1);
new Connect(mainList,this).execute(aa);
mainList.setClickable(true);
mainList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener()
{
public boolean onItemLongClick(AdapterView <?> arg0, View arg1,
int pos, long id) {
// TODO Auto-generated method stub
// Toast.makeText(this, "Wrong Username/Password! TRY AGAIN!", Toast.LENGTH_LONG).show();
Log.d("long clicked","pos: " + pos);
return true;
}
});
activity_main.xml which contains the root listview
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"></ListView>
As said by #Vikram in comments
Check line 88 of FlowTextView.java: Link. That should answer your
question.
so basically the solution was to setOnTouchListener for that object to null
setOnTouchListener(null)

ListView Delete Icon

I am trying to fire a event when a user click the delete icon in the list , But I could not able to do it.I am using a simple cursor adapter with setviewbinder to display items in the list.In the setviewBinder method I set the textview layout clicklistener to be null that's why, view is not clicked when a user click in the textview.When a user is swipe in the list immediately delete icon is appeared.I need to fire a click event in the deleteIcon.Currently, when a user is clicked the delete icon, the whole view is clickable.I need to achieve when a user is clicked the delete icon, need to fired the event, only in the delete icon.
public boolean setViewValue(final View view, final Cursor data, int columnIndex) {
LinearLayout layoutTaskView=(LinearLayout)view.findViewById(R.id.layoutTaskView),
layoutTaskDelete = (LinearLayout) view.findViewById(R.id.LayoutDeleteTask);
boolean crossed = Boolean.valueOf(data.getString(columnIndex));
if(crossed){
icon.setImageState(new int[] { android.R.attr.state_checked }, true);
text1.setPaintFlags(text1.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
text2.setPaintFlags(text2.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
layoutTaskView.setOnClickListener(null);
}
}
Layout Xml File
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/layoutTaskView"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:padding="15dp" >
<TextView
android:id="#+id/taskTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="#+id/taskName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:paddingLeft="10dp"
android:text="TextView" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/LayoutDeleteTask"
android:clickable="false">
<ImageView
android:id="#android:id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:padding="15dp"
android:src="#drawable/checkmark" />
</LinearLayout>
</LinearLayout>
ListView xml
<com.myexample.CrossView
android:id="#+id/crossview"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</com.myexample.CrossView>
Adapter Code
String[] from = new String []
{
DatabaseHelper.COLUMN_CATEGORY,DatabaseHelper.COLUMN_TASKNAME,
DatabaseHelper.COLUMN_CROSSED};
int[] to = new int[] {R.id.taskTime,R.id.taskName, android.R.id.content};
adapter = new SimpleCursorAdapter(context, R.layout.layout_list_item, data, from, to, 0);
adapter.setViewBinder(new CrossBinder());
lv.setAdapter(adapter);
public void onItemClick(AdapterView<?> parent, View v, final int position, final long id) {
ImageView icon = (ImageView)v.findViewById(android.R.id.icon);
icon.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(v.getId() == android.R.id.icon){
}
}
}
You need to use android:descendantFocusability = "afterDescendants" parameter to root layout of list item. In your case, the root LinearLayout.
For more information check this link.
EDIT : remove android:clickable="false" from your LinearLayout containing ImageView. Put android:clickable="true" in ImageView. Moreover, you can use ImageButton instead ImageView.
P.S. I have suggested to use android:descendantFocusability in main parent LinearLayout.
Create custom adapter like that answer Custom adapter for a list of items that have multiple child items?
This answer give tutorial link to create custom dapters for listview.

Categories

Resources