Button.setClickable(false) is not working
Asked Answered
O

13

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);
Oil answered 16/9, 2013 at 10:26 Comment(1)
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?Ceremonious
C
59

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.

Ceremonious answered 16/9, 2013 at 10:33 Comment(2)
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.Ulphi
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 eyesRealism
B
53

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

Brightman answered 21/11, 2013 at 5:40 Comment(3)
@ADT I have edited the answer. Do try and let me know the outcome :)Brightman
@jozze using both setClickable and setEnabled worked for meSusi
using setEnable(false) if there is background for view , it disappearsCristalcristate
M
11

Put setClickable after setOnClickListener

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

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

Malinin answered 27/9, 2019 at 1:22 Comment(2)
This looks pretty good answer, but I wonder why it is not still upvotedLaguna
@Laguna because the same answer was already given 6 years before this answer. https://mcmap.net/q/592635/-button-setclickable-false-is-not-workingRubious
F
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.

Familial answered 16/9, 2013 at 10:39 Comment(0)
R
3

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

Rosy answered 8/1, 2018 at 8:40 Comment(0)
A
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.

Alkyne answered 11/10, 2019 at 17:53 Comment(0)
C
1

on xml

android:enabled="false"

android:alpha="0.5"

dynamically

yourButtonId.alpha = 0.5f

yourButtonId.isEnabled = false

Coriecorilla answered 1/7, 2021 at 13:19 Comment(1)
Yes, it will workLegnica
G
0

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

Glossematics answered 2/8, 2017 at 8:48 Comment(0)
S
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).

Simp answered 13/8, 2017 at 18:0 Comment(0)
W
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;
}
Wary answered 12/11, 2019 at 14:11 Comment(0)
C
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.

Counterblow answered 22/7, 2020 at 19:53 Comment(0)
C
0

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

spinner.setOnTouchListener { v, event ->
                return@setOnTouchListener true
            }
Comminute answered 19/12, 2020 at 19:24 Comment(0)
S
0

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

Scansorial answered 30/1, 2021 at 10:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.