I’m experiencing a weird behaviour with one Button in Android.
I created a Custom Button (MyButton) and I put some methods on it to extend the deafult View, like adding a disable/enable method to change the button.
Here is MyButton’s code:
public class MyButton extends Button {
/** Enabled. */
private boolean enabled = true;
/** The default bg. */
private Drawable defaultBG = null;
/** The disabled drawable. */
private int disabledDrawable = 0;
/******************/
/** CONSTRUCTORS **/
/** [...] **/
/******************/
/**
* Disable.
*/
public void disable()
{
this.setClickable(false);
this.setFocusable(false);
Log.d("MY_BUTTON", "DISABLED!!");
if(!enabled || disabledDrawable == 0)
return;
defaultBG = this.getBackground();
this.setBackgroundResource(disabledDrawable);
enabled = false;
}
/**
* Enable.
*/
public void enable()
{
this.setClickable(true);
this.setFocusable(true);
Log.d("MY_BUTTON", "ENABLED!!");
if(enabled)
return;
this.setBackgroundDrawable(defaultBG);
enabled = true;
}
}
Here is my Button declaration in the layout:
<com.xxxx.library.View.MyButton
android:id="@+id/buttonVideo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="@string/upload_camera_video" />
and this is what I do in my Activity:
@Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.upload);
super.onCreate(savedInstanceState);
cameraVideo = (MyButton) findViewById(R.id.buttonVideo);
cameraVideo.setDisabledBG(R.drawable.disabled_buttons);
cameraVideo.diable();
cameraVideo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!v.isClickable())
{
log("NOT CLICKABLE!!!");
return;
}
log("CLICKABLE!!!");
}
});
}
And the result in logcat is this:
11-14 11:33:37.681: D/MY_BUTTON(6800): DISABLED!!
11-14 11:33:43.446: D/UploadActivity(6800): CLICKABLE!!!
The buttons seems disabled, but still accepts click events although it has the disabled background I assigned to it and it’s not focusable.
You need to use the method
setEnabled(false)to disable the button.