Search code examples
androidbuttonvisibilityvisibleinvisible

Button visibility problems in Android


I'm creating a toolbar that toggles the visibility of buttons when you click a button from a toolbar. So, if the user clicks on the "Draw" button, invisible buttons "Pencil" and Pen" will become visible above the "Draw" button. If you click the "Draw" button again, the buttons "Pencil" and "Pen" will become invisible again.

Within my xml file I have set the visibilty of some buttons to be "invisible" so when I launch the activity they won't be seen. This part is straight forward.

.xml file of btnDrawLine - (Update @ 12:21)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent" >

<com.odhranlynch.testSection.UserInterface
    android:id="@+id/UserInterface"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true" />

<Button
    android:id="@+id/btnDraw"
    android:layout_width="80dip"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:text="Draw" />

<Button
    android:id="@+id/btnDrawLine"
    android:layout_width="80dip"
    android:layout_height="wrap_content"
    android:layout_above="@+id/btnDraw"
    android:layout_alignParentLeft="true"
    android:visibility="visible"
    android:text="Line" />

<Button
    android:id="@+id/btnDrawCurve"
    android:layout_width="80dip"
    android:layout_height="wrap_content"
    android:layout_above="@+id/btnDrawLine"
    android:layout_alignParentLeft="true"
    android:visibility="visible"
    android:text="Curve" />

<Button
    android:id="@+id/btnCutout"
    android:layout_width="80dip"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_toRightOf="@+id/btnDraw"
    android:text="Cutout" />

<Button
    android:id="@+id/btnCutInner"
    android:layout_width="80dip"
    android:layout_height="wrap_content"
    android:layout_above="@+id/btnDraw"
    android:layout_toRightOf="@+id/btnDraw"
    android:visibility="visible"
    android:text="Inner" />

<Button
    android:id="@+id/btnCutOutter"
    android:layout_width="80dip"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/btnDrawCurve"
    android:layout_alignBottom="@+id/btnDrawCurve"
    android:layout_toLeftOf="@+id/btnCancel"
    android:visibility="visible"
    android:text="Outter" />

<Button
    android:id="@+id/btnCancel"
    android:layout_width="80dip"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_toLeftOf="@+id/btnFinish"
    android:text="Cancel" />

<Button
    android:id="@+id/btnFinish"
    android:layout_width="80dip"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:text="Finish" />

</RelativeLayout>

Next, when the user clicks a button that is visible, I'd like the invisible buttons to appear.

Here's the thing, they won't reappear!lol I'm confused by it.

I would be very grateful if someone would be kind enough to shed so light onto this for me :)

testActivity.java

package com.odhranlynch.testSection;

import com.odhranlynch.testSection.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class testActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.new_product);

    // Find buttons and give them a name.
    final View btnDraw = findViewById(R.id.btnDraw);
    final View btnCutOut = findViewById(R.id.btnCutout);
    final View btnDrawLine = findViewById(R.id.btnDrawLine);
    final View btnDrawCurve = findViewById(R.id.btnDrawCurve);
    final View btnCutInner = findViewById(R.id.btnCutInner);
    final View btnCutOutter = findViewById(R.id.btnCutOutter);


    //Draw Button clicked (UI Toolbar).
    btnDraw.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            //Treat button as a toggle button
            //So if a sub-button (e.g. Draw Line) is visible, then we know the button has
            //been toggled to visible so lets now make it invisible.

            if (btnDrawLine.getVisibility()== View.VISIBLE) {
                //Its visible.
                btnDrawLine.setVisibility(View.INVISIBLE);

                btnDrawCurve.setVisibility(View.INVISIBLE);
                Log.d("TAG", "INVISIBLE");
            } else {
                // Either gone or invisible
                btnDrawLine.setVisibility(View.VISIBLE);
                btnDrawCurve.setVisibility(View.VISIBLE);
                Log.d("TAG", "VISIBLE");
            }
        }
    });     

}
}

As a further point to mention, if I set the visibilty of the buttons to be visible within the .xml file I can toggle the visibilty perfectly fine during runtime!

Again, I would be grateful of some help :)


Solution

  • Try replacing View.INVISIBLE to View.GONE.