15

I'd like to gray out a button so it appears disabled to the user, but still listen for clicks so that I can display a message to the user that explains why the button isn't applicable.

I'd like to ensure that the Android API is the one configuring whatever is the appropriate standard disabled appearance as opposed to manually setting the button color to gray, etc. What's the best way to do this?

Related: Android - Listen to a disabled button

Community
  • 1
  • 1
Jeff Axelrod
  • 27,676
  • 31
  • 147
  • 246

6 Answers6

11

this is custom button which expose the event of touch when disabled it working for sure, and tested. designed specific to you

public class MyObservableButton extends Button
{
public MyObservableButton(Context context, AttributeSet attrs)
{
    super(context, attrs);
}

private IOnClickWhenEnabledListner mListner;

public void setOnClickWhenEnabledListener(IOnClickWhenEnabledListner listener) {
    mListner = listener;
}

private interface IOnClickWhenEnabledListner {
    public void onClickWhenEnabled();
}

@Override
public boolean onTouchEvent(MotionEvent event)
{       
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        if (!isEnabled() && mListner != null) {
            mListner.onClickWhenEnabled();
        }
    }

    return super.onTouchEvent(event);
}

}

this is the right way to accomplish what you want from my point of view as android developer.

there is no problem extanding all android's views, and use them on the xml files, and source instead..

good luck

Tal Kanel
  • 10,475
  • 10
  • 60
  • 98
6

You could also manually set the background of your Button to the default one for disabled. But leave the button enabled and handle the click events in the normal fashion

something like this should do it:

mBtn.setBackgroundResource(android.R.drawable.btn_default_normal_disabled);
FoamyGuy
  • 46,603
  • 18
  • 125
  • 156
0

I am not an Android expert, so there could be better ways, but what about disabling the button and overlaying a transparent view that will catch the events?

You could have the view always there laying below the button so you just need to change a z-index, or create it dynamically when needed.

garph0
  • 1,700
  • 1
  • 13
  • 16
  • garph, that's an interesting idea--I bet it would work. I'm first going to try a couple of the other solutions though. – Jeff Axelrod Jun 01 '12 at 19:28
0

If your button is on the newer types that controls color via backgroundTint rather then background, this is what you should do:

if (enabled) {
    ViewCompat.setBackgroundTintList(btn, getResources().getColorStateList(R.color.button_states)); // either use a single color, or a state_list color resource
} else {
    ViewCompat.setBackgroundTintList(btn, ColorStateList.valueOf(Color.GRAY));
}
// make sure we're always clickable
btn.setEnabled(true);
btn.setClickable(true);
marmor
  • 27,641
  • 11
  • 107
  • 150
-2

You could also set the Button's style to look grayed out (for both pressed and non-pressed states), then setOnClickListener() as normal, but have the onClick() method give the message that it isn't clickable.

jmhend
  • 537
  • 1
  • 6
  • 16
-2

Since button extends textview. You may use the methods in textview such as .setTextColor() or .setBackgroundColor() .

Now for the the display you have 2 options:

  • AlertDialog - displays an alert but doesn't self close unless specified.
  • Toast - displays a text over a given time and self closes.

take your pick.

Juju
  • 70
  • 8