-1

I want to hide a layout when a Button is pressed.

enter image description here

How do I do so?

Community
  • 1
  • 1
Brahimce
  • 23
  • 2
  • 10

3 Answers3

0

You should be able to call

view.setVisibility(View.GONE);

in your onClick() method where view is the variable name for the View that you are trying to make disappear.

codeMagic
  • 44,549
  • 13
  • 77
  • 93
Austin Musice
  • 543
  • 4
  • 8
0

Try toggling the visibility to the view you want to hide or remove

Button button = (Button) findViewById(R.id.button);
final LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(layout.getVisibility() == View.VISIBLE){
                layout.setVisibility(View.GONE);
            } else {
                layout.setVisibility(View.VISIBLE);
            }
        }
});
Chad Bingham
  • 32,650
  • 19
  • 86
  • 115
0

If your second layout's name is view2, and your first buttons name is buttonOne:

buttonOne.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                     if(view2.getVisibility()==View.VISIBLE){
                         view2.setVisibility(View.GONE);
                     }else{
                         view2.setVisibility(View.VISIBLE);
                     }
                }
            });
Jemshit
  • 9,501
  • 5
  • 69
  • 106