1

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 :)

6
  • Is it printing the log messages as expected? Visible, Invsible, Visible.. etc on each button clock? Feb 7, 2012 at 12:04
  • Yeah it is, so I know for sure the toggle button works.
    – Odhran
    Feb 7, 2012 at 12:05
  • Hmm, have you tried replacing btnDrawCurve.setVisibility(View.INVISIBLE) with View.GONE? Just thinking out loud. Feb 7, 2012 at 12:14
  • Haven't tried that, I'll give it a go now!
    – Odhran
    Feb 7, 2012 at 12:22
  • Thinking out loud was a winner lol Changing from View.INVISIBLE to View.GONE worked! Many thanks Raoul :) Raoul, if you answer the question I'll be able to tick that it has been answered
    – Odhran
    Feb 7, 2012 at 12:31

3 Answers 3

0

Try replacing View.INVISIBLE to View.GONE.

0

Your code is working fine..

The XML file

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello" />

<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="invisible"
android:text="Line" />

<Button
    android:id="@+id/draw"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="draw" />

</LinearLayout>

The activity

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class DrawCanvasActivity extends Activity {
private static final String Number1 = "9686801147";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final View btnDrawLine = findViewById(R.id.btnDrawLine);


    Button btnDraw = (Button) findViewById(R.id.draw);
    btnDraw.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            if (btnDrawLine.getVisibility()== View.VISIBLE) {
                btnDrawLine.setVisibility(View.INVISIBLE);
                Log.d("TAG", "INVISIBLE");
            } else {
                btnDrawLine.setVisibility(View.VISIBLE);
                Log.d("TAG", "VISIBLE");
            }
        }
    });     
}
}

The button line appears when the draw button is clicked.Guess there may be problem in the view formatting in your code.

3
  • Hmm thats strange. Would a SurfaceView have any interference with this? I create one which is used in my code. I'll update my .xml snippet...
    – Odhran
    Feb 7, 2012 at 12:16
  • 1
    Try setting fixed width and height for surface view. Also draw button is below surfaceview and drawline is above draw. So there are chances that the surfaceview overlaps the drawline button.. Try fixing it below the draw button. Does this work?
    – user936414
    Feb 7, 2012 at 12:26
  • Many thanks user936414 :) I've got it working now. Although, I will certainly have a look at you're suggestions :)
    – Odhran
    Feb 7, 2012 at 12:33
0

If you want ot use gone/visible, just add a LinearLayout around your buttons, which you want to hide. LinearLayout will have a layout_width=wrap_content; and you referer position of other element to this Layout.

After you free to change visibility to gone/visible of your buttons.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.