lomza
lomza

Reputation: 9706

Add Button programmatically with parameters?

Basically, I have this layout structure: <RelativeLayout> <RelativeLayout> <TextView /> </RelativeLayout> <ScrollView> <RelativeLayout> ... </RelativeLayout> </ScrollView> </RelativeLayout> and I want to add a button programmatically. This button should be inside the <RelativeLayout>, which is inside the <ScrollView>. Also, I need it to be align to the bottom and CENTER_VERTICAL. I would really appreciate any hints/examples ;) Thanks! P.S. Although, there are many similar questions on stackoverflow, none of the answers helped me...

Upvotes: 1

Views: 4308

Answers (1)

Thommy
Thommy

Reputation: 5407

First you need to give your relative Layout in XML an ID: android:id="@+id/myLayout". Then in Java code:

Button b = new Button(this);
RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
rl.addRule(RelativeLayout.ALIGN_BOTTOM);
b.setLayoutParams(rl);
((RelativeLayout) findViewById(R.id.myLayout)).addView(b);

Upvotes: 8

Related Questions