Mohamad Ghanem
Mohamad Ghanem

Reputation: 599

Add Dynamic button in android

I have a Linear Layout

and I want to add to this Linear Layout a button (from 3 Buttons) Dynamic in Run Time Depending on an integer value that returns from a function

I know how to add a view to a Layout using AddView , and how to remove views form layout using removeView ...

but my question is how to check if the view Exists in the layout before call the method reomveView

Upvotes: 0

Views: 553

Answers (1)

Francis Fuerte
Francis Fuerte

Reputation: 254

I suggest you create your buttons beforehand and just set the visibility to View.GONE

when you trigger some event you can set other buttons visibility to true while the others are false..

this will work with no problems and you dont have to add them dynamically.. it will just "seem" dynamic :)

You can do that with this code:

button1.setVisibility(View.GONE);
button2.setVisibility(View.VISIBLE);

Given the buttons are "button1" and "button2"

View.GONE will "remove" the view from the screen, however it is still "there" it just doesn't appear for the user, and it doesn't take up any space

View.INVISIBLE will "remove" the view from the screen, how ever the space it took up is still used by it.

View.VISIBLE will show the view as it would usually.

Upvotes: 3

Related Questions