setClickable() thwarted by onClickListener - android

In my main activity I have a button which I make clickable or non clickable according to a given state. However I discover that the onClickListener automatically makes it clickable again! So how to disable a button?

If you want to make your view non-clickable:
view.setClickable(false);
If you want to disable your view:
view.setEnabled(false);

Restore clickable after setting listener, for instance:
boolean isClickable = mView.isClickable();
mView.setOnClickListener(this);
mView.setClickable(isClickable);

Related

Listview itemclick not work

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.

ListView Touch is disabled with button

**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.

ListView onitemclick is not working

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.

ToggleButton - How Do I Manually Call Its onClick Listener?

I have created a custom View which is a descendent of ToggleButton. I want my custom view to have two states i.e. on and off just like ToggleButton but I want to MANUALLY change its state once my custom view has been clicked.
The way I have done this so far is to override the performClick() method but in my performClick() method I want to be able to call the assigned onClick listener, so my question is how do I call my custom view's onClick listener? (I bet there's an obvious answer!)
I realised I just override toggle().

How to disable edittext pop up when EditText is LongClicked?

Hello All I have used onLongClickListener() to my EditText view but whenever I click on view for long time so a popup appears that doesn't let me do my task in onLongClick(View v).
The pop up contains some options like Select All,Select Text,Cut All ,Copy All etc.
I want to disable that (that should not appear on clicking long on EditText view).
How can I do that please Help Me
You can return true in your onLongClick listener onLongClick method that means you have consumed the event and it won't be passed further along.
I also got that popup when I overrode onTouchEvent() and returned false. Suppress it by returning true.

Categories

Resources