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
-
Possible duplicate of How to add a button dynamically in Android?Tim Biegeleisen– Tim Biegeleisen2017-03-21 07:58:12 +00:00Commented Mar 21, 2017 at 7:58
-
1button is hidden but it take space in xml file Not really.John Joe– John Joe2017-03-21 07:58:13 +00:00Commented 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.Tim Biegeleisen– Tim Biegeleisen2017-03-21 07:59:18 +00:00Commented Mar 21, 2017 at 7:59
-
Does this answer your question? How can I remove a button or make it invisible in Android?Josh Correia– Josh Correia2020-08-14 03:06:39 +00:00Commented Aug 14, 2020 at 3:06
Add a comment
|
3 Answers
- 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);
Comments
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
Rucha
ok,means using layout position we can also set controls peoper in xml File