Reputation: 1169
I am trying to create views to add in to a Relative Layout and have views inside a Relative Layout to then be displayed on the main layout. This will then look like container boxes with elements inside and I want to be able to add a layout with the same elements inside below the previous one. Here is the code that I have for it so far, but I get an onClick error, because I am testing it by button click to create the new layout. Bare in mind that this is in a Fragment class and I am using ActionBarSherlock to do this. Please can anyone help me or give me any tips.
Here is the code:
RelativeLayout layout = new RelativeLayout(getSherlockActivity());
RelativeLayout layout2 = new RelativeLayout(getSherlockActivity());
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);
RelativeLayout.LayoutParams params5 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams params6 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams containerParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//Main layout
tv1 = new TextView(getSherlockActivity());
params1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
params1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
tv1.setId(1);
tv1.setText("textView1");
tv2 = new TextView(getSherlockActivity());
params2.addRule(RelativeLayout.BELOW, tv1.getId());
params2.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params2.setMargins(0, 0, 0, 16);
tv2.setId(2);
tv2.setText("textView2");
//Container layout
layout2.setBackgroundResource(R.color.display_panels);
layout2.setPadding(10, 0, 10, 10);
containerParams.addRule(RelativeLayout.ALIGN_LEFT, tv2.getId());
containerParams.addRule(RelativeLayout.ALIGN_RIGHT, tv1.getId());
containerParams.addRule(RelativeLayout.BELOW, tv2.getId());
containerParams.setMargins(0, 0, 0, 16);
tv3 = new TextView(getSherlockActivity());
params3.addRule(RelativeLayout.ABOVE, tv4.getId());
params3.addRule(RelativeLayout.ALIGN_LEFT, pb1.getId());
tv3.setId(3);
tv3.setText("textView3");
tv4 = new TextView(getSherlockActivity());
params4.addRule(RelativeLayout.CENTER_HORIZONTAL);
params4.addRule(RelativeLayout.ABOVE, pb1.getId());
tv4.setId(4);
tv4.setText("textview4");
pb1 = new ProgressBar(getSherlockActivity());
params5.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
params5.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params5.addRule(RelativeLayout.LEFT_OF, ib1.getId());
pb1.setProgress(40);
pb1.setMax(100);
ib1 = new ImageButton(getSherlockActivity());
params6.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params6.addRule(RelativeLayout.ALIGN_TOP, pb1.getId());
//ib1.setImageResource(R.drawable.ic_green_ok);
layout.addView(tv1, params1);
layout.addView(tv2, params2);
layout2.addView(tv3, params3);
layout2.addView(tv4, params4);
layout2.addView(pb1, params5);
layout2.addView(ib1, params6);
//layout.addView(layout2, containerParams);
getSherlockActivity().setContentView(layout);
The commented out code is what I had in there before and it didn't work with them in, but i thought that this should work:
layout.addView(layout2, containerParams);
Upvotes: 0
Views: 2343
Reputation: 91
You can also do this
final TextView titleView = (TextView) itemLayout
.findViewById(R.id.titleView);
Rather than creating all those additional parameters this will take care of the position in the layout
Upvotes: 1