Note that there are some explanatory texts on larger screens.

plurals
  1. POControlling an ImageView using a Button click
    text
    copied!<p>I have here four sets of buttons and an empty <code>RelativeLayout</code> under them, whenever I click one of these another layout will pop-out in the empty <code>RelativeLayout</code> containing another TWO sets of buttons namely <kbd>ON</kbd> and <kbd>OFF</kbd>. my problem occurs in the <kbd>ON</kbd> and <kbd>OFF</kbd> buttons because I want an <code>ImageView</code> to appear whenever I click the <kbd>ON</kbd> button &amp; disappear whenever I click the <kbd>OFF</kbd> button. Here is my layout and the code:</p> <p>This is the layout for the 4 buttons:</p> <pre><code> &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/RelativeLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/green_bg" tools:context=".AleccMainActivity" &gt; &lt;TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="@string/please_choose" android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="#CAFFD8" /&gt; &lt;LinearLayout android:id="@+id/linearLayout" android:layout_width="fill_parent" android:layout_height="70dp" android:layout_alignParentLeft="true" android:layout_below="@+id/textView1" android:background="#95FF4F" &gt; &lt;Button android:id="@+id/btnAcu" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/tab_acu" android:onClick="gotoACU" /&gt; &lt;Button android:id="@+id/btnFan" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/tab_fan" android:onClick="gotoFan" /&gt; &lt;Button android:id="@+id/btnLight" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/tab_light" android:onClick="gotoLight" /&gt; &lt;Button android:id="@+id/btnHallway" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:background="@drawable/tab_hallway" android:onClick="gotoHallway" /&gt; &lt;/LinearLayout&gt; &lt;RelativeLayout android:id="@+id/rl" android:layout_width="fill_parent" android:layout_height="200dp" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_below="@+id/linearLayout" &gt; &lt;/RelativeLayout&gt; &lt;/RelativeLayout&gt; </code></pre> <p>And this is the code for one of the layout that will be appearing when I click one of those FOUR (they have the same buttons inside but different images to show):</p> <pre><code> &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/RelativeLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/green_bg" android:orientation="vertical" &gt; &lt;LinearLayout android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="50dp" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginTop="16dp" &gt; &lt;Button android:id="@+id/button1" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:onClick="offFAN" android:text="@string/off" /&gt; &lt;Button android:id="@+id/button2" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:onClick="onFAN" android:text="@string/on" /&gt; &lt;/LinearLayout&gt; &lt;ImageView android:id="@+id/fanOn" android:layout_below="@+id/linearLayout1" android:layout_alignParentLeft="true" android:layout_width="wrap_content" android:layout_height="100dp" android:contentDescription="@string/acu" android:visibility="invisible" android:src="@drawable/fan_on" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p>This one is the function when calling the layout with <kbd>ON</kbd> and <kbd>OFF</kbd> buttons:</p> <pre><code>public void gotoFan(View v){ // Do something in response to button RelativeLayout rlFAN = (RelativeLayout) findViewById(R.id.rl); getLayoutInflater().inflate(R.layout.alecc_fan, rlFAN, true); } </code></pre> <p>And this is where I think my problem exists:</p> <pre><code>public void offFAN (View view) { String messageToSend = "FANS OFF"; SmsManager.getDefault().sendTextMessage(number, null, messageToSend, null,null); Toast.makeText(AleccMainActivity.this,"Electric Fans turned OFF", Toast.LENGTH_SHORT).show(); ImageView fanON = (ImageView) findViewById(R.id.fanOn); fanON.setVisibility(View.GONE); } public void onLIGHT (View view) { String messageToSend = "LIGHTS ON"; SmsManager.getDefault().sendTextMessage(number, null, messageToSend, null,null); Toast.makeText(AleccMainActivity.this,"Flourescent Lights turned ON", Toast.LENGTH_SHORT).show(); ImageView lightON = (ImageView) findViewById(R.id.lightOn); lightON.setVisibility(ImageView.VISIBLE); } </code></pre> <p>I can't get it working. When I first click the one of the 4 buttons then click <kbd>ON</kbd> / <kbd>OFF</kbd> it seems fine but, whenever I click another the image no longer becomes visible.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload