Search code examples
androidbuttoninvisible

How to set button invisible android


So I tried to make A button invisible, I searched first the forum for the answer and tryed it but it just doesn't work.

int level=0;
    try{
        String FILENAME = "TowerFile";
        FileInputStream fos = openFileInput(FILENAME);
        byte[] b = new byte[1];
        fos.read(b);
        fos.close();
        if(new String(b).equalsIgnoreCase("1")){
            level=1;
        }
        if(new String(b).equalsIgnoreCase("2")){
            level=2;
        }
        if(new String(b).equalsIgnoreCase("3")){
            level=3;
        }
        if(new String(b).equalsIgnoreCase("4")){
            level=4;
        }
        if(new String(b).equalsIgnoreCase("5")){
            level=5;
        }
    }catch (java.io.FileNotFoundException e) {
    } catch (IOException e) {
            e.printStackTrace();
    }

    Button button1 = (Button) findViewById(R.id.button1);
    Button button2 = (Button) findViewById(R.id.button2);
    Button button3 = (Button) findViewById(R.id.button3);
    Button button4 = (Button) findViewById(R.id.button4);
    Button button5 = (Button) findViewById(R.id.button5);



    if(level==1){
        button1.setVisibility(View.INVISIBLE);
    }

So I first get A variabel out of my file, and then I want to make A button invisable depending on the output. I get the wright number out of my file but it doesn't do anything, I also did 0 and gone but nothing works


Solution

  • buttonName.setVisibility(View.GONE);
    

    This is better than (View.INVISIBLE) cause the button doesn't get any space from the layout. If you want to make the button another time visible just use:

    buttonName.setVisibility(View.VISIBLE);