Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I make a button appear disabled and still listen for clicks?

Tags:

android

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

like image 625
Jeff Axelrod Avatar asked Jun 01 '12 15:06

Jeff Axelrod


2 Answers

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

like image 122
Tal Kanel Avatar answered Sep 21 '22 22:09

Tal Kanel


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);
like image 26
FoamyGuy Avatar answered Sep 21 '22 22:09

FoamyGuy