Android - Listen to a disabled button
Asked Answered
R

11

28

How can I respond to an event based on clicking a disabled Button. I have a requirement that I have to present Dialog, when a disabled Button is clicked but the listener I have assigned does not fire even when I setClickable(false)

Am an android noob, sorry.

Rack answered 26/1, 2012 at 9:24 Comment(0)
K
8

A disabled button cannot listen to any event, but you can customize your own button by extending Button class to make your own definition of disabling

Kovno answered 26/1, 2012 at 9:33 Comment(1)
Even though this is the accepted answer, it isn't particularly useful. :-(Kadiyevka
B
62

You can for example use #setActivated() method instead. Disabling a view will ignore all events. https://developer.android.com/reference/android/view/View.html#setActivated(boolean). Then you can customize text and background styles with android:state_activate attribute if you need:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_activated="false"
      android:color="@color/a_color" />
    <item android:state_activated="true"
      android:color="@color/another_color" />
</selector>
Borer answered 26/11, 2018 at 12:47 Comment(1)
That's a pretty good alternative. Should be higher up, or even the accepted answer, imho.Variant
K
8

A disabled button cannot listen to any event, but you can customize your own button by extending Button class to make your own definition of disabling

Kovno answered 26/1, 2012 at 9:33 Comment(1)
Even though this is the accepted answer, it isn't particularly useful. :-(Kadiyevka
A
8

You can override onTouchEvent and create a listener like this :

class MyButton @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = R.attr.materialButtonStyle) : MaterialButton(context, attrs, defStyleAttr) {

    private var onDisableClickListener: OnClickListener? = null

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        if (!isEnabled && event?.action == MotionEvent.ACTION_DOWN) {
            onDisableClickListener?.onClick(this)
        }
        return super.onTouchEvent(event)
    }

    fun setOnDisableClickListener(l: OnClickListener?) {
        onDisableClickListener = l
    }
}

In your activity :

button.setOnDisableClickListener {
            Toast.makeText(this), "The button is disabled", Toast.LENGTH_SHORT).show()
}
button.setOnClickListener {
            Toast.makeText(this), "The button is enabled", Toast.LENGTH_SHORT).show()
}
Aerodonetics answered 30/10, 2020 at 13:28 Comment(0)
I
4

Instead of disabling it, keep it enabled but use a flag to control your "inner state"

Ivy answered 26/1, 2012 at 9:50 Comment(2)
Could you (or someone) expand on this please?Explosion
no need to add a new flag when you can use buttons very own setActivated() method and then check with isActivated()Hack
C
2

I solved this issue by using a flag to keep my button's state.

private boolean isMyButtonEnabled = false;

public void onMyButtonClick(View v) {
   if(isMyButtonEnabled){
      ..
   }
}
Callie answered 25/4, 2014 at 0:15 Comment(0)
H
2

you can add android:allowClickWhenDisabled attribute to your button in xml like this:

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:allowClickWhenDisabled="true"/>
Hallowmas answered 21/3, 2022 at 13:31 Comment(1)
Please note that this is only available for API >= 31: developer.android.google.cn/reference/android/view/…Mojica
A
0

I looked for it but got nothing to listen the EditText block. So I find another way to activate it. If there is a near button or area that you already listen, you can enable SetOnLongClickListener to activate the block. It will be a secret but you can tell the users.

button.setOnLongClickListener(new OnLongClickListener() { 
        @Override
        public boolean onLongClick(View v) {
            // TODO Auto-generated method stub
            editText.setEnabled(true)
            return true;
        }
    });

enter image description here

Anyone answered 21/12, 2014 at 14:26 Comment(0)
L
0

I'm about to tackle this by using the selected state, which is generally available for use in widgets, and can be used in state list drawables. A simple search for usage of isSelected turns up results in ListView, GridView, TextView and TabLayout. And the documentation states

Views are typically * selected in the context of an AdapterView like ListView or GridView; * the selected view is the view that is highlighted.

Longdistance answered 25/4, 2017 at 4:38 Comment(0)
D
0

You should use activated state to enable or disable button . It is clickable or as someone point use selected or checked state. Each of these state has a different meaning so use it carefully

Deaton answered 28/6, 2018 at 19:33 Comment(0)
B
0

create in res/color/color_state.xml

  <selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:color="#767C7F" android:state_activated="true" />
     <item android:color="#CBCBCB" android:state_activated="false" />
     <item android:color="#CBCBCB" />
  </selector>

set textColor by:

   android:textColor="@color/color_state"

set event click to change state color:

   binding.format1.setOnClickListener {
        binding.format1.isActivated = true
        binding.format2.isActivated = false
        binding.format3.isActivated = false
    }
Bailar answered 17/10, 2022 at 5:46 Comment(0)
E
0

After API level 31 , you can use android:allowClickWhenDisabled="true"

Effloresce answered 8/6 at 12:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.