How to set button invisible android

11,692

Solution 1

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

Solution 2

try to do like this

button1.setVisibility(Button.INVISIBLE);

Solution 3

The code you written for making button invisible is correct. Check whether the variable level is 1 or not.

Share:
11,692
stevedc
Author by

stevedc

Updated on June 04, 2022

Comments

  • stevedc
    stevedc about 2 years

    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

    • Shankar Agarwal
      Shankar Agarwal about 12 years
      can you paste your xml file too
  • vipin
    vipin about 12 years
    try that code without if block if and let me know what happen
  • stevedc
    stevedc about 12 years
    Yes without the if statement it doesn't appear
  • vipin
    vipin about 12 years
    oh it shows there is problem in you logic that level is not get assigned by the correct value please check your code new String(b).equalsIgnoreCase("1") assigning level or not
  • stevedc
    stevedc about 12 years
    Yes I found it to but I am using the same method for my languege and that works
  • vipin
    vipin about 12 years
    means your proble get resolved or not if resolved then set type of question close
  • vipin
    vipin about 12 years
    or is there any other issue raised