**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.
Related
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.
My custom Listview has more than one button, how can I identify which button has been clicked ? I want to do different things on action of different buttons.
In custom adapder of your ListView you must be creating objects of each button in its getView() method.
Just set the onClickListener on the buttons and perform the action on each buttons in its onClick().
Create local variables for buttons inside getview method and set onClickListener for each button in a row.
Also have a look at this -:
2 Buttons in ListView in android
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.
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.
i have a listview and button on the list how can i use both button.setonclicklistener and onListItemClick in listview
I had the same issue.But while adding button's onClick event to adapter,it was not able to listen to the list item's click event.so I used image instead of a button and then registered its click event in adapter,and that was working nicely.
Set the button.setOnCLickListener in your getView() method of the adapter.