Reputation: 11
How do we set edittext toRightof textView during run time layout Design on Android?
We want to make a layout design during run time like
TextView1 EditText1
TextView2 EditText2
SubmitButon
Upvotes: 1
Views: 514
Reputation: 987
put this code in Your onCreate()
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
TextView tv = new TextView(this);
tv.setText("Dynamic layouts ftw!");
ll.addView(tv);
EditText et = new EditText(this);
et.setText("weeeeeeeeeee~!");
ll.addView(et);
this.setContentView(sv);
Upvotes: 3
Reputation: 3497
Use the following code in layout-xml file:
<LinearLayout android:orientation="horizontal" >
<LinearLayout android:orientation="vertical">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText layout_width="0" layout_weight="1" />
</LinearLayout>
<LinearLayout android:orientation="horizontal" >
<EditText android:layout_width="200"
android:layout_height="wrap_content" />
<EditText android:layout_width="200"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout android:layout_gravity="center_horizontal">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
Upvotes: 0
Reputation: 34236
There are a lot of ways to do this. Personally, I would nest some layouts like so (Note- not real code, just to get the point across):
<LinearLayout orientation:vertical >
<LinearLayout orientation:horizontal>
<TextView layout_width="0" layout_weight="1" />
<EditText layout_width="0" layout_weight="1" />
</LinearLayout>
<LinearLayout orientation:horizontal>
<TextView layout_width="0" layout_weight="1" />
<EditText layout_width="0" layout_weight="1" />
</LinearLayout>
<LinearLayout>
<Button layout_gravity="center_horizontal" />
</LinearLayout>
</LinearLayout>
Set all of the LinearLayouts to fill the screen width-wise and wrap content or set your own spacing vertically.
Upvotes: 1
Reputation: 29670
First Create a TableLayout and put TextView1, EditText1, TextView2 & EditText2 in it. After TableLayout use normal Relative Layout to add Submit Button & make sure to change the Button's Gravity to Center Horizontal.
Upvotes: 1