How to lay out Views in RelativeLayout programmatically?

From what I’ve been able to piece together, you have to add the view using LayoutParams. LinearLayout linearLayout = new LinearLayout(this); RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); relativeParams.addRule(RelativeLayout.ALIGN_PARENT_TOP); parentView.addView(linearLayout, relativeParams); All credit to sechastain, to relatively position your items programmatically you have to assign ids to them. TextView tv1 = new TextView(this); tv1.setId(1); TextView … Read more

Percentage width in a RelativeLayout

You are looking for the android:layout_weight attribute. It will allow you to use percentages to define your layout. In the following example, the left button uses 70% of the space, and the right button 30%. <LinearLayout android:layout_width=”match_parent” android:layout_height=”wrap_content” android:orientation=”horizontal”> <Button android:text=”left” android:layout_width=”0dp” android:layout_height=”wrap_content” android:layout_weight=”.70″ /> <Button android:text=”right” android:layout_width=”0dp” android:layout_height=”wrap_content” android:layout_weight=”.30″ /> </LinearLayout> It works the … Read more