21

I have set mButton.setClickable(false); in my code but still this button is invoked by global button.setOnClickListener of my code.

EDIT: sorry for the delayed update. Below is the details view where I face the issue.
inside my listview customAdapter class getView method

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    View row = convertView;
    YourWrapper wrapper = null;
    HashMap<String, Object> cTa= new HashMap<String, Object>();
    cTa= d.getPosition(position)
    Button mButton = (Button)convertView.findViewById(R.id.mBtn);
    if (row == null)
    {
        row = inflater.inflate(R.layout.layout, parent, false);
        wrapper = new YourWrapper (row);
        row.setTag(wrapper);
    }
    else
        wrapper = (YourWrapper) row.getTag();

     if(success)
        {
                    // section-1
            mButton.setClickable(true);
        }
        else{
                   // section-2
            mButton.setClickable(false);
            mButton.setFocusable(false);
        }
    wrapper.getButton().setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            //operation
        }
    });

    return row;
}

Above is the current code which working,and on section-2 it makes the mButton clickable- false, and focusable - false but still it's listen the below wrapper.getButton().setOnClickListener() and perform the operation. Please suggest me. Sorry for delayed update. Thanks!

UPDATE: I have made below hot-fixes that solve the problem for now.

// section-2
mButton.setVisibility(View.GONE);
mButton.setClickable(false);
mButton.setFocusable(false);
AdamHurwitz
  • 9,758
  • 10
  • 72
  • 134
Pradip
  • 3,189
  • 3
  • 22
  • 27
  • Is this your whole code? Is `mButton` the same as `wrapper.getButton()`? If so, why don't you put the part with `setOnClickListener` in your section-1? – Jan Sep 16 '13 at 18:06

13 Answers13

57

That seems to be by design. This is from the documentation of the View.setOnClickListener method:

Register a callback to be invoked when this view is clicked. If this view is not clickable, it becomes clickable.

Jan
  • 1,394
  • 10
  • 12
  • 19
    srsly..."If this view is not clickable, it becomes clickable." is a huge design flaw in my opinion. Thanks for the answer, this should be accepted. – JacksOnF1re Dec 12 '14 at 16:42
  • 1
    I agree with @JacksOnF1re. Android is a seriously rushed job with some of the dumbest design decisions. The same with button `setAlpha(0);` - the button is still clickable even when alpha is 0. Who in their sane mind would make a button invisible and still let it be clickable, under what circumstance would one want that in their app? Some silly guessing game with invisible buttons? Guess where I am! *rolls eyes* – Zhang Jun 23 '17 at 04:52
50

Instead of using setClickable(false) use setEnabled(false)

joecizac
  • 1,077
  • 2
  • 13
  • 14
11

Put setClickable after setOnClickListener

mBtn.setOnClickListener(this);
mBtn.setClickable(false);

if you put setClickable(false) before setOnClickListener(this), it doesn't work.

Aaron
  • 139
  • 1
  • 3
4

Instead of using setClickable(false) use following

button.setFocusableInTouchMode(false);

I had the same problem in my app where i needed to set my button not to clickable in certain conditions. this worked for me. Hope this helps.

mananjani
  • 185
  • 5
3

Use View.setOnClickListener() before View.setClickable() ,or the method setOnclickLisnter() will set the flag true.

Sarabjit Singh
  • 1,814
  • 1
  • 17
  • 28
1

I'm not sure if you're still looking for the answer, but for some weird reason

mBtn.setClickable(true);

stops the view from getting clicked and

mBtn.setClickable(false);

makes it clickable again.

Saad Waseem
  • 13
  • 2
  • 4
1

on xml

android:enabled="false"

android:alpha="0.5"

dynamically

yourButtonId.alpha = 0.5f

yourButtonId.isEnabled = false

Emine Sa
  • 31
  • 3
0

You can check like if(!view.isClickable()) return;

Qamar
  • 4,959
  • 1
  • 30
  • 49
0

This will work in case of Imageview as well as the button.

 private OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
        if (imageview.isEnabled()){
            //I have wrapped all code inside onClick() in this if condition
            //Your onClick() code will only execute if the imageview is enabled
            //Now we can use setEnabled() instead of setClickable() everywhere
        }}
    };

Inside onCreate(), you can do setEnabled(false) which will be equivalent to setClickable(false).

We are able to use setEnabled() as tag because it's state remains uneffected on invocation of click (unlike setClickable() whose state changes).

Sachin Aggarwal
  • 1,095
  • 8
  • 17
0

Like Other friends said, setOnClickListener will override the flag to true.
So the Workaround is to setOnTouchEvent return true whenever you want to disable clicks and set it to retrun false when you want to enable click events.
This is because onTouchEvent is called before every clickListener you define for a view, so returning true will say to all listeners that :

"Ok, I received this event here, nobody else can receive it".

So your solution may be something like this:

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    View row = convertView;
    YourWrapper wrapper = null;
    HashMap<String, Object> cTa= new HashMap<String, Object>();
    cTa= d.getPosition(position)
    Button mButton = (Button)convertView.findViewById(R.id.mBtn);
    if (row == null)
    {
        row = inflater.inflate(R.layout.layout, parent, false);
        wrapper = new YourWrapper (row);
        row.setTag(wrapper);
    }
    else
        wrapper = (YourWrapper) row.getTag();

     if(success)
        {
                    // section-1
            mButton.setOnTouchListener((v, event) -> false);
        }
        else{
                   // section-2
            mButton.setOnTouchListener((v, event) -> true);
        }
    wrapper.getButton().setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            //operation
        }
    });
    return row;
}
Reza
  • 845
  • 13
  • 18
0

Set the click listener to null

someView.setOnClickListener(null)

As @Jan notes, the setOnClickListener enables the click listener automatically. Therefore, a null click listener can be set to disable future clicks. After setting the view to a null click listener, there are no adverse effects to future clicks on that view.

AdamHurwitz
  • 9,758
  • 10
  • 72
  • 134
0

I wanted to do it on Spinner, and only this one worked for me:

spinner.setOnTouchListener { v, event ->
                return@setOnTouchListener true
            }
M. Usman Khan
  • 3,689
  • 1
  • 59
  • 69
0

I just checked setClickable(true) and setClickable(false) on Android 4.1.1 and it seems to be working now.

Rocky666
  • 91
  • 1
  • 5