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

4
  • Possible duplicate of How to add a button dynamically in Android? Commented Mar 21, 2017 at 7:58
  • 1
    button is hidden but it take space in xml file Not really. Commented Mar 21, 2017 at 7: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. Commented Mar 21, 2017 at 7:59
  • Does this answer your question? How can I remove a button or make it invisible in Android? Commented Aug 14, 2020 at 3:06

3 Answers 3

5
  • 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);
Sign up to request clarification or add additional context in comments.

Comments

0

you should use View.GONE instead of View.INVISIBLE

Comments

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?

1 Comment

ok,means using layout position we can also set controls peoper in xml File

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.