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 );
}
you are not using
params, I believe you intended to useinstead of
However this way
TextViews will overlap each other, so you might also want to.addRule()for vertical alignment inside your loop.edit
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.