I have a ListView in my ListView show ImageButton.
I set focusalble "false" and focusableInTouchMode "false" to ImageButton.
I set ListView.OnItemClickListner. When I run my project It's show my ListView.
But When I click on Listview It's not working.
Then I remove ImageButton in layout and run my project again when i click ListView It' work
What wrong ?
You are not the only sufferer :) This behavior is often considered as a bug by Android developers Have a look at this link of their conversation.
To solve your problem- simply include android:descendantFocusability="blocksDescendants" attribute in your root layout.
If you are using custom Listview and in the custom Listview row item list if only Textview and Imageview, you should remove android:inputType="". It cause problem of focusability.
Actually nothing is wrong. What you are doing is ok. But i think you forgot one key factor here ImageButton has it's own OnClickListener. So when you embed your ImageButton into the listview row ListView.OnItemClickListner is not working because the click/touch is invoked by ImageButton, it's because of that ListView is not getting your click/touch event.
Checkout this link: How to fire onListItemClick in Listactivity with buttons in list?
I guess you are using customize list view Item just try to set
set focusable "false" and focusableInTouchMode "false" for all view in your custom_list_view_item.xml
Don't worry about your image button if you using click listener for image Button in adapter, It will also work fine. just do focusable "false" and focusableInTouchMode "false" for all view in your custom_list_view_item.xml
May be you have written onclick listener for the image button in adapter class
Example :
imageButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
}
});
If you set onclick listener for the listItem .It will automatically consume the action input
so the list item may not be clicked.
Related
I have a listView that uses a customAdapter and a has a custom layout. the listView Item contains an Image, Text and Button. I added an onClick attribute in the Xml android:onClick="onClicked". Then I made the method to respond to the Button
public void onClicked(View view){
......
}
but I don't know how to make it make a different code depending on the button position in the listView, if you know how to solve this problem please tell me, thanks
You can't do that, in your case you have to programatically set the onClickListener on your adapter code
You can create a custom adapter for your listview. You can program your button for click within that custom adapter ..
**I am using custom listview when i placing the button in custome view it is disabling the item touch or itemlongpress listener ...
How to use button click and listview touch simultaneously
The Problem here is , Listview having button as a child, Button will take the priority of onClick. Do the following thing
Set the property of Button to setFocusable(false);It will enable the OnClick on ListView.
For Click on Button - use onTouch instead of onClick. Which will allow you to click on button as well
try to add
android:descendantFocusability="blocksDescendants"
in the ListView tag where you define it in xml.
I got a ListView and I populate it with the view containing buttons. I set onClick listeners to the buttons from the adapted, but when I click the concrete item of the ListView - nothing happens. So, we can say only buttons onClick Listener is called, when I need listViewItem to be clicked.
What to do?
Set to your button in xml file: android:focusable="false"
This is because your Button takes focus. So now you can use both onClick for your buttons, and onItemClick for each row in ListView, by telling it that Button should not be focusable.
you need to set on item click like this listView.setOnItemClickListener();
It is better to do the onlcick on listview item only.You can set some flag and toggle between the functionalities.
This is an item of my ListView:
The star is a Button. I set a OnClickListener for it in getView() method of the Adapter. But I now face a problem - onItemClick doesn't fire after adding the Button. I want a Button to listen for its clicks and the rest part of a view to respond with firing onItemClick. Is there a solution?
I am not sure but Button is not working in list item , so plz use ImageView instead of Button.
You can also set onClick on ImageView same as Button in your getView() method
ImageView.setOnClickListener().....etc
To make the button not focusable (but still clickable):
android:focusable="false"
android:focusableInTouchMode="false"
If you put onClickListener() on an item in getView() of Adapter , then onItemClickListener() in that Activity doesn't work. For this you can put onClickListener() on your star button and put all other Items in a Layout and put onClickListener() in getView() to that layout.
I have a ListView containing a checkbox, an imageview, two textviews and a button. The problem is after adding the checkbox and the button the onitemclick event of the ListView is not responding. Will anybody suggest a work around for the problem?
You can set both android:focusable and android:focusableInTouchMode attributes of checkbox to false and onItemClick of the list will be called. But you'll have to call CheckBox#toggle() yourself in onItemClick.
Add an onlick listner to the view. or the checkbox and handle it manually.
When you use checkbox in your listview then it consumes your click action and your check action will be performed.
You can put click listener over textview, imageview or button. you also need to handle checkbox.