Can't click on ListView row with imagebutton
Unfortunately,
android:focusable="false"
android:focusableInTouchMode="false"
doesn't work for ImageButton
.
I finally found the solution here. In your layout xml for those items, add
android:descendantFocusability="blocksDescendants"
to the root view.
It works perfectly for a ListView
that has ImageButton
s. According to official reference, blocksDescendants
means that the ViewGroup
will block its descendants from receiving focus.
ImageButton within row of ListView android not working
You need to create a custom adapter which accepts a list of objects that you can use to build your list.
You need to create a class which holds the information needed in your list. From what you've said something like this:
public class RowItem {
private String _columnName;
private Drawable _drawable;
public RowItem(String columnName, Drawable drawable) {
_columnName = columnName;
_drawable = drawable;
}
public String getColumnName() {
return _columnName;
}
public Drawable getDrawable() {
return _drawable;
}
}
Then create these objects from the items you want in your list
ArrayList<RowItem> rowItems = new ArrayList<>();
//Create a number of row items and add them to your list
RowItem myItem1 = new RowItem("String I want for column name", myDrawable);
rowItems.add(myItem1);
After creating your list of RowItem objects you create your custom adapter like this:
CustomListViewAdapter customAdapter = new CustomListViewAdapter(this, R.layout.todo_row, rowItems);
Then you can pass your list of RowItem objects into your custom adapter via it's contructor (like you were trying to do previously):
public class CustomListViewAdapter extends ArrayAdapter<RowItem> {
Context context;
ArrayList<RowItem> _rowItems;
public CustomListViewAdapter(Context context, int resourceId,
ArrayList<RowItem> rowItems) {
super(context, resourceId);
this.context = context;
_rowItems = rowItems;
System.out.println("I am in the custom Adapter class "+ context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
System.out.println("This is the get view");
View row = convertView;
RowItem item = _rowItems.get(position);
// you can now get your string and drawable from the item
// which you can use however you want in your list
String columnName = rowItem.getColumnName();
Drawable drawable = rowItem.getDrawable();
if (row == null) {
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = mInflater.inflate(R.layout.todo_row, parent, false);
}
ImageButton chkDone = (ImageButton) row.findViewById(R.id.chkDone);
chkDone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View parentRow = (View) v.getParent();
ListView listView = (ListView) parentRow.getParent();
final int position = listView.getPositionForView(parentRow);
System.out.println("I am in position "+ position);
}
});
return row;
}
Listview with ImageButton, want to call Listview Click in ImageButton click Listener
no idea if this is really a good practice, but it works like this:
imageButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
ExpandableListView myList = (ExpandableListView) parent.findViewById(R.id.expandableList);// get reference to the ExpandableListView
myList.performItemClick(view, myList.getPositionForView(view), getGroupId(groupPosition));
}
});
ImageButton click event works, but ListView click event does not
ImageButton
takes focus when you click on list item
Add the below
android:descendantFocusability="blocksDescendants"
to the root element in custome.xml
Android custom ListView with ImageButton is not getting focus
I have to get focus on all clickable items so that user knows where the focus is. But the problem is I can get focus on list row if I add
View getting hang up due to multiple views inside row file, to get click You have to add one more property to your main layout of row xml.
android:descendantFocusability="blocksDescendants
But when I write above property then I'm not able to focus ImageButton which is in my row file.
I just tried one way to acheive to get foucus on ImageButton
as well as on listview
.
To get focus on ImageButton:
- Create one selector file.
- Applied on Imagebutton.
- Added
android:focusableInTouchMode="true"
to ImageButton.
To get focus on Listview:
- Add
android:descendantFocusability="blocksDescendants
to main-layout within row xml.
Now when you click on your ImageButton it will focus with your desire color even it will focus on listview click too.
Selecotor.xml
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android"
>
<item
android:state_pressed="true"
android:drawable="@android:color/transparent"
/> <!-- pressed -->
<item
android:state_focused="true"
android:drawable="@android:color/darker_gray"
/> <!-- focused -->
<item
android:drawable="@android:drawable/btn_star"
/> <!-- default -->
</selector>
Tested in S3 emulator, works well.
ImageButton within an Android ListView causes problems
use
android:descendantFocusability="blocksDescendants"
android:focusable="false"
hope it helps
Android onClick for ImageButton inside ListView and get position
You are using setTag()
but never doing anything with it. Change
final int position = getView(position, view, parent).get(position);
to
final int position = (Integer)view.getTag();
right now it is probably giving you the position
of the last item that was iterated through getView()
.
Edit
Your Toast
needs a String
so you will need to change it to
Toast.makeText(getContext(), "" + position, Toast.LENGTH_SHORT).show();
or convert it into a String
before.
Related Topics
Pick Any Kind of File via an Intent in Android
Mediaplayer Stutters at Start of Mp3 Playback
Read & Writing Arrays of Parcelable Objects
The Android Emulator Is Not Starting, Showing "Invalid Command-Line Parameter"
Failed to Find Build Tools Revision 23.0.1
How to Debug Android Application Line by Line Using Eclipse
Rotating Image on a Canvas in Android
How to Block a Mobile Number Call and Message Receiving in Android Application Development
How to Set Custom Font for a Whole Application in Android
Cursor Adapter and SQLite Example
How to Add Items to a Spinner in Android
Encryption Error on Android 4.2
How to Check If Url Is Valid in Android
Android Studio: How to Attach Android Sdk Sources
Multi Selection Spinner in Android Without Alertdialog
How to Fix Android.Os.Deadobjectexception Android X
Android - How to Get the Processname or Packagename by Using Pid