How to enable and disable buttons in Android?
I am trying to enable and disable 4 UI buttons programmatically. And I am using Unity3D, but I cannot get it to work. What am I missing? My current attempt looks like this:
My LinearLayout
xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/overlay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right"
android:orientation="vertical" >
<com.BoostAR.Generic.TintedImageButton
android:id="@+id/helpButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/overlayButtonMargin"
android:src="@drawable/help"
android:visibility="visible" />
<com.BoostAR.Generic.TintedImageButton
android:id="@+id/refreshButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/overlayButtonMargin"
android:src="@drawable/refresh" />
<com.BoostAR.Generic.TintedImageButton
android:id="@+id/screenshotButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/overlayButtonMargin"
android:src="@drawable/photo" />
<com.BoostAR.Generic.LockButton
android:id="@+id/lockButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/overlayButtonMargin"
android:src="@drawable/unlocked" />
</LinearLayout>
What I did in code:
private static final int[] AUGMENTED_UI_IDS = {
R.id.refreshButton, R.id.screenshotButton, R.id.lockButton
};
private void updateAugmentedUiVisibility()
{
final int visibility =
(mShouldShowAugmentedUI ? View.VISIBLE : View.INVISIBLE);
runOnUiThread(new Runnable() {
@Override
public void run() {
for (int id : AUGMENTED_UI_IDS) {
final View view = findViewById(id);
if (view == null) {
Log.e(LOG_TAG, "Failed to find view with ID: " + id);
} else {
Log.e(LOG_TAG, "Visibility: " + visibility);
view.setVisibility(visibility);
}
}
}
});
}
}
RESULT:
Statement
Log.e(LOG_TAG, "Failed to find view with ID: " + id);
is called. When I cross-reference the ID numbers that seem to be good.
A quick explanation that can add some order to things when you set attributes through code is good to keep this in mind:
view.setVisibility(View.INVISIBLE); // the opposite is obvious
will make the view invisible, but will still take up space (you just won't see it)
view.setVisibility(View.GONE);
will cause the view to change, making it invisible, and rearrange the views around it in a way that takes up space as if it never existed.
view.setEnabled(false); // the opposite is again obvious
will make the view insensitive but visually understandable, for example let's say you are using a switch and after switching it you would like it to become incompatible, then this would be an example:
Switch MySwitch = (Switch) someParentView.findViewById(R.id.my_switch);
MySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if (isChecked)
{
MySwitch.setEnabled(false);
}
}
}
this, by the way, also applies to layouts (to some extent).
Hope it helps.