0

In My Android Application, i have to hide button based on some condition,if condition is true than button is hide,otherwise it appear as it is, so for hide facility i'm using button's buttonID.setVisibility(View.INVISIBLE),so that button is hidden but it take space in xml file,so please suggest me to button is hidden and don't take space in xml file

John Joe
  • 12,412
  • 16
  • 70
  • 135
Rucha
  • 17
  • 1
  • 6
  • Possible duplicate of [How to add a button dynamically in Android?](http://stackoverflow.com/questions/1851633/how-to-add-a-button-dynamically-in-android) – Tim Biegeleisen Mar 21 '17 at 07:58
  • 1
    _button is hidden but it take space in xml file_ Not really. – John Joe Mar 21 '17 at 07:58
  • There is nothing wrong with having a button hard-coded in your layout XML file. In fact, I would generally prefer that, assuming the button is showing regularly, because it makes it easier to see what your layout is doing. That being said, if you follow the duplicate link, you will see how to dynamically add a button to a layout. This will make the button "don't take space in xml file," if that's what you really want. – Tim Biegeleisen Mar 21 '17 at 07:59
  • Does this answer your question? [How can I remove a button or make it invisible in Android?](https://stackoverflow.com/questions/4127725/how-can-i-remove-a-button-or-make-it-invisible-in-android) – Josh Correia Aug 14 '20 at 03:06

3 Answers3

4
  • visible - This means visible and it takes space.
  • invisible- This means invisible and it takes space.
  • gone- This means invisible and it DOESN'T takes space.

Use it as follows:

Visible tag in XML

android:visibility="visible"

Visible code in java

view.setVisibility(View.VISIBLE);

Invisible tag in XML

android:visibility="invisible"

Invisible code in java

view.setVisibility(View.INVISIBLE);

Gone tag in XML

android:visibility="gone"

Gone tag in java

view.setVisibility(View.GONE);
Lokesh Saini
  • 106
  • 1
  • 6
0

you should use View.GONE instead of View.INVISIBLE

justqb
  • 751
  • 7
  • 6
0

As what @Tim suggested,you can always change the layout parameters for the elements not just set it's visibility to VISIBLE or GONE

Here the sample. Assume checkBox is clicked

checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked){  
                buttonID.setVisibility(View.GONE);
             }
            else{
                buttonID.setVisibility(View.VISIBLE); 
                // now settings the new parameters
                AbsoluteLayout.LayoutParams params = ((AbsoluteLayout.LayoutParams) buttonID.getLayoutParams());
                params.x = 100; // the new value
                params.y = 100; // the new value
                buttonID.setLayoutParams(params);
            }
    });

Source : Android: how to change layout_x, layout_y in an AbsoluteLayout dynamically?

Community
  • 1
  • 1
John Joe
  • 12,412
  • 16
  • 70
  • 135