3

I am trying to enable and disable 4 UI buttons programmatically. And I am using Unity3D, but I can't seem to make it 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 the 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);
                    }
                }
            }
        });
    }
}

OUTCOME:

The statement

Log.e(LOG_TAG, "Failed to find view with ID: " + id);

gets called. When I did cross reference the id numbers which seem to be good.

TommySM
  • 3,793
  • 3
  • 24
  • 36
Filip
  • 155
  • 2
  • 14
  • You should paste the relevant code here. What exactly do you mean by " turn on and off these 4 UI buttons"? You want to disable/enable them? – codeMagic Jun 05 '15 at 15:24
  • I did add to the question. Thanks and yes I would like to disable and enable them. That is all I want to do. – Filip Jun 05 '15 at 16:24
  • Edited to try and improve your formatting and your grammer. I also removed extraneous introductory material and the *thank you*. Also, I imported the xml from pastebin (please don't link part of your question to 3rd party sites). – Elliott Frisch Jun 07 '15 at 17:03
  • Do you at any point *before* calling `updateAugmentedUiVisibility` call `setContentView(that-layout-id)` or inflate said layout any other way into the view hierarchy so the `findViewById` may find it? – Eugen Pechanec Jun 07 '15 at 22:03
  • possible duplicate of [How to disable an Android button?](http://stackoverflow.com/questions/4384890/how-to-disable-an-android-button) – Jared Burrows Jun 09 '15 at 07:41

1 Answers1

2

A quick explanation that might add some order to things, when you set attributes through the code, it's good to remember these:

view.setVisibility(View.INVISIBLE); // the opposite is obvious

will make the view invisible but will still occupy the space (you just won't see it)

view.setVisibility(View.GONE);

will collapse the view, making it both invisible and will rearrange the views around it in a manner that will occupy the space as if it was never there.

view.setEnabled(false); // the opposite is again obvious

will make the view non responsive but in a visually understandable manner, e.g, let's say you use a Switch, and after you switch it, you would like it to become unchangable, 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 is relevant for layouts as well (to some extent).

Hope this Helps.

TommySM
  • 3,793
  • 3
  • 24
  • 36