Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically add view one below other in relative layout

Tags:

android

I want something like this programmatically:

view1 |  view2 view3 |  view4 ---------------- view1 |  view2 view3 |  view4 ---------------- view1 |  view2 view3 |  view4 --------------- ........... ...... which keeps repeating -------------- 

I don't want to use ListView.

like image 981
Pushan Avatar asked Mar 14 '11 09:03

Pushan


People also ask

What is the difference between relative layout and FrameLayout?

RelativeLayout : is a ViewGroup that displays child views in relative positions. AbsoluteLayout : allows us to specify the exact location of the child views and widgets. TableLayout : is a view that groups its child views into rows and columns. FrameLayout : is a placeholder on screen that is used to display a single ...

Which layout allows us to display items on the screen relative to each other?

RelativeLayout is a view group that displays child views in relative positions. The position of each view can be specified as relative to sibling elements (such as to the left-of or below another view) or in positions relative to the parent RelativeLayout area (such as aligned to the bottom, left or center).


1 Answers

Important: Remember to set the ID for each view.

RelativeLayout layout = new RelativeLayout(this); RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); layout.setLayoutParams(layoutParams);  RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); RelativeLayout.LayoutParams params4 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  TextView tv1 = new TextView(this); tv1.setId(1); tv1.setText("textView1");  TextView tv2 = new TextView(this); params2.addRule(RelativeLayout.RIGHT_OF, tv1.getId()); tv2.setId(2); tv2.setText("textView2");  TextView tv3 = new TextView(this); params3.addRule(RelativeLayout.BELOW, tv1.getId()); tv3.setId(3); tv3.setText("textView3");  TextView tv4 = new TextView(this); params4.addRule(RelativeLayout.RIGHT_OF, tv3.getId()); params4.addRule(RelativeLayout.ALIGN_BOTTOM, tv3.getId()); tv4.setId(4); tv4.setText("textView4");  layout.addView(tv1, params1); layout.addView(tv2, params2); layout.addView(tv3, params3); layout.addView(tv4, params4); 
like image 165
askilondz Avatar answered Sep 27 '22 18:09

askilondz