Relative Layout adding views programmatically one below other not aligning correctly
I was using the index of the for
loop as id for the textViews which I changed to make Android generate Id for it as below and it is working fine now as expected.
I changed the line
textView.setId(i)
to
textView.setId(ViewGroup.generateViewId())
Thanks.
Programmatically add view one below other in relative layout
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);
Adding a view between 2 views when one view is aligned to the bottom programmatically
Try this
RelativeLayout rlmain= (RelativeLayout) findViewById(R.id.rlMain);
RelativeLayout rA=new RelativeLayout(MainActivity.this);
RelativeLayout.LayoutParams layoutA = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
rA.setId(1);
rA.setBackgroundColor(getResources().getColor(R.color.blue));
layoutA.addRule(RelativeLayout.ALIGN_PARENT_TOP, rlmain.getId());
rA.setLayoutParams(layoutA);
TextView tvA=new TextView(MainActivity.this);
RelativeLayout.LayoutParams tvParamA = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
tvA.setText("This is Layout 1");
tvA.setLayoutParams(tvParamA);
rA.addView(tvA);
RelativeLayout rC=new RelativeLayout(MainActivity.this);
RelativeLayout.LayoutParams layoutC = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutC.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,rlmain.getId());
rC.setId(3);
rC.setLayoutParams(layoutC);
rC.setBackgroundColor(getResources().getColor(R.color.gray));
TextView tvC=new TextView(MainActivity.this);
RelativeLayout.LayoutParams tvParamC = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
tvC.setText("This is Layout 3");
tvC.setLayoutParams(tvParamC);
rC.addView(tvC);
RelativeLayout rB=new RelativeLayout(MainActivity.this);
RelativeLayout.LayoutParams layoutB = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
layoutB.addRule(RelativeLayout.BELOW, rA.getId());
layoutB.addRule(RelativeLayout.ABOVE, rC.getId());
rB.setId(2);
rB.setLayoutParams(layoutB);
rB.setBackgroundColor(getResources().getColor(R.color.orange));
ScrollView scroll=new ScrollView(MainActivity.this);
RelativeLayout.LayoutParams scrollB = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
scroll.setLayoutParams(scrollB);
rB.addView(scroll);
rlmain.addView(rA);
rlmain.addView(rB);
rlmain.addView(rC);
rlMain in above code is the RelativeLayout declared in xml file.
Hope this may help you!
How to add View outside RelativeLayout to RelativeLayout programmatically
RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams( RelativeLayout.WRAP_CONTENT,RelativeLayout.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW,R.id.rv_comment);
params.setMargin(0,16,0,0);
tv.setLayoutParams(params);
Related Topics
How to Start an Activity from Within a Fragment
How to Order My SQLite Database in Descending Order, for an Android App
Android: Notifydatasetchanged(); Not Working
Set Actionbar Tabs at Bottom in Actionbaractivity
Error Inflating Class and Android.Support.V7.Widget.Cardview
No Adapter Attached; Skipping Layout
First Letter Capitalization for Edittext
Reusing Fragments in a Fragmentpageradapter
Android Gradle: Buildtoolsversion VS Compilesdkversion
How to Change Toolbar Navigation and Overflow Menu Icons (Appcompat V7)
How to Reference an Asset in a Library Project
How to Share Image + Text Together Using Action_Send in Android
Downloading/Caching Google Maps for Offline Use
Google Maps API V2 'Failed to Load Map. Could Not Contact Google Servers'
Can the Android Emulator Record and Play Back Audio Using Pc Hardware
Android 4.3: How to Connect to Multiple Bluetooth Low Energy Devices