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.

THelper
  • 15,333
  • 6
  • 64
  • 104
Justin
  • 3,255
  • 3
  • 22
  • 20

10 Answers10

60

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>
Ricard
  • 1,223
  • 1
  • 13
  • 17
  • 9
    That's a pretty good alternative. Should be higher up, or even the accepted answer, imho. – Levite Mar 15 '19 at 11:44
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()
}
AndrazP
  • 217
  • 4
  • 11
Benjamin Ledet
  • 942
  • 1
  • 9
  • 19
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

Pete Houston
  • 14,931
  • 6
  • 47
  • 60
4

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

Luis Filipe
  • 8,488
  • 7
  • 48
  • 76
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){
      ..
   }
}
Selman Kahya
  • 175
  • 2
  • 2
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"/>
Peter Wauyo
  • 827
  • 1
  • 8
  • 11
  • 1
    Please note that this is only available for API >= 31: https://developer.android.google.cn/reference/android/view/View#setAllowClickWhenDisabled(boolean) – James Mar 22 '22 at 16:09
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

Sabri Meviş
  • 2,231
  • 1
  • 32
  • 38
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.

androidguy
  • 3,005
  • 2
  • 27
  • 38
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

rana
  • 1,824
  • 3
  • 21
  • 36
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
    }