Can I Make A Button Appear Disabled And Still Listen For Clicks?
Solution 1:
this is custom button which expose the event of touch when disabled it working for sure, and tested. designed specific to you
publicclassMyObservableButtonextendsButton
{
publicMyObservableButton(Context context, AttributeSet attrs)
{
super(context, attrs);
}
private IOnClickWhenEnabledListner mListner;
publicvoidsetOnClickWhenEnabledListener(IOnClickWhenEnabledListner listener) {
mListner = listener;
}
privateinterfaceIOnClickWhenEnabledListner {
publicvoidonClickWhenEnabled();
}
@OverridepublicbooleanonTouchEvent(MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (!isEnabled() && mListner != null) {
mListner.onClickWhenEnabled();
}
}
returnsuper.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
Solution 2:
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);
Solution 3:
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.
Solution 4:
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);
Solution 5:
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.
Post a Comment for "Can I Make A Button Appear Disabled And Still Listen For Clicks?"