How to make an invisible button in runtime

Posted on 2011-10-11
Last Modified: 2012-05-12
Hi Experts,

I am trying to make a button invisible when the application starts (android), but my code does not work.

      Button b3;      
    public void onCreate(Bundle icicle) {
        login = (EditText) findViewById(;
        senha = (EditText) findViewById(;
        b3 = (Button) findViewById(;
        b3.setVisibility(View.GONE);   <-- code Crash here

How to do this code works ?

Question by:alpires
    LVL 24

    Expert Comment

    set it to "gone" in xml resource.

    besides, are you sure your b3 object is not null?
    LVL 1

    Accepted Solution

    There are three visibility states for Views, controls, etc - anything subclassed from View:

    VISIBLE which means just that; the item is visible, takes up space in the layout, etc

    INVISIBLE which means the item takes up space in the layout, but the item itself is not visible.  (think of three buttons in a row; the middle one is INVISIBLE, so it won't be drawn, but there will be space for it between the other two buttons)

    GONE which means the item is not drawn, and doesn't take up space in the layout.  if the middle button of the three-button example were GONE, then the other two buttons would be positioned as if the middle button wasn't even defined at all; they would likely be close together.

    Just offhand, try checking that b3 isn't null:

        b3 = (Button) findViewById(;
        if (b3 != null) {
            b3.setVisibility(View.GONE);   <-- code Crash here
        } else {
            Log.i("b3 is null");

    Open in new window

    You can also set a breakpoint at the setVisibility line and use the debugger.

    Check that whatever is named 'button3' really is a button.  (there must be something otherwise the compiler would complain that doesn't exist.  it's interesting that it does crash; anything subclassed from View has setVisibility() so I wouldn't expect it to crash...)

    To make it invisible right from the start, set the "android:visibility" property to GONE right in the XML file:

    	android:text="Clear Message"

    Open in new window


    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    As the title indicates, I have done this before. It chills me everytime I update the OS on my phone, ( because one time I did this and I essentially had a bricked …
    A short article about a problem I had getting the GPS LocationListener working.
    Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now