Reputation: 565
I am adding textview on runtime and I want textview to be added on the last line or next line if it cannot fit on the last line. I am trying to use RelativeLayout . I am trying to specify params like ALIGN_PARENT_LEFT But this is not working. So, I wanted to ask which layout should I use ?
RelativeLayout ll = (RelativeLayout) findViewById(R.id.categorylayout);
// ll.setGravity(Gravity.LEFT );
RelativeLayout.LayoutParams params = (android.widget.RelativeLayout.LayoutParams) ll.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
for(int i =0 ; i<tags.size();i++){
TextView tt = new TextView(this);
tt.setText(tags.get(i));
tt.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tt.setBackgroundColor(Color.RED);
ll.addView(tt );
}
Upvotes: 1
Views: 1893
Reputation: 9743
you are not using params
, I believe you intended to use
tt.setLayoutParams(params);
instead of
tt.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
However this way TextView
s will overlap each other, so you might also want to .addRule()
for vertical alignment inside your loop.
edit
for(int i =0 ; i<tags.size();i++){
TextView tt = new TextView(this);
tt.setText(tags.get(i));
tt.setId(i+1);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
if (i == 0)
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
else
params.addRule(RelativeLayout.BELOW, i);
tt.setLayoutParams(params));
tt.setBackgroundColor(Color.RED);
ll.addView(tt);
}
brief exlpanation: you set ids for each TextView
(as explained here, RelativeLayout resolves only ids for each children), and so next one will be below previous. First one is aligned with parent's top. I haven't tested it, but at least it is starting point.
Upvotes: 2
Reputation: 18489
If you know how many widgets in your xml file,then using LinearLayou**t with **vertical_orientaion you can add it at any specified position like this:
ll.addView(tt,your_position);
Hope this will help. :)
Upvotes: 0