theostorm
theostorm

Reputation: 43

Android Add Buttons to ViewGroup Programmatically

I have a ViewGroup. In this ViewGroup I would like to add three buttons that take up the whole size of the ViewGroup, each evenly taking up the same amount of space.

Where do I add these buttons? I've tried doing it in the constructor, in onDraw, in onLayout, and none of them seem to display anything.

Upvotes: 0

Views: 1222

Answers (1)

Sweeper
Sweeper

Reputation: 271040

First you should create a button.

Button button = new Button (this);
//And you want to set some properties of the view
button.setLayoutParams (new RelativeLayout.LayoutParams (....));//Here you should use the corresponding layout params for different ViewGroups, here I used RelativeLayout.
//Maybe you want to set other properties...

ViewGroup viewGroup = //Here get your view group
viewGroup.addView (button);

And that's it! The most important part is setting the layout params. Remember to use the right params or an exception will be thrown.

Upvotes: 1

Related Questions