John
John

Reputation: 105

handling programatically created layout from xml: ids challenge

I need to be able to add a layout to my main layout from xml file many times programatically.

The problem is handling assigning ids of the new layout's views. See the code:

MainFragment.java

private int belowOfWhat = R.id.layout_header;
...

 onActivityResult:

LayoutInflater myInflater = LayoutInflater.from(getContext());
RelativeLayout layout = (RelativeLayout) myInflater.inflate(R.layout.assignment_layout, null, false);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
//here problems start
//i want to position the new layout below the previously created layout
params.addRule(RelativeLayout.BELOW, belowOfWhat);
layout.setLayoutParams(params);
mRelativeLayout = rootView.findViewById(R.id.to_do_layout);
mRelativeLayout.addView(layout);
belowOfWhat = generateViewId();
idsOfToDos.add(belowOfWhat);
CheckBox assignmentCheckbox = (CheckBox) 
//assignment_checkbox is an id of checkbox in the xml layout I add
rootView.findViewById(R.id.assignment_checkbox);
assignmentCheckbox.setId(belowOfWhat);
assignmentCheckbox.setText(mToDoInfo);

I don't know where the problem is, so here is how the app works now: I add a new layout, it gets positioned correctly below layout_header.

But when I add the second or more layouts they overlap each other on top of the app instead of being positioned one below the other.

I'd appreciate if you guided me to the solution of the problem.

Upvotes: 0

Views: 36

Answers (2)

Muazzam A.
Muazzam A.

Reputation: 645

You have to use Relative Layout and use the following properties with that to align the views.

  • RelativeLayout.BELOW
  • RelativeLayout.RIGHT_OF
  • RelativeLayout.ALIGN_BOTTOM

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);

hope so it gives you an idea how to align views pragmatically.

Upvotes: 0

firegloves
firegloves

Reputation: 5709

If you don't need views'ids for other purposes you could solve more simple.

If I've understood what you are trying to do, what you need is a vertical LinearyLayout instead of a RelativeLayout, then subviews will be added one below each other

Upvotes: 1

Related Questions