0

I tried to use below code, but the textviews do not change line after it reach the end of screen.

    RelativeLayout ll = (RelativeLayout)findViewById(R.id.viewObj);
    int lastid=0;
    for (int i=0; i<100; i++) {
        String teststr =  " hello ";
        TextView textView2 = new TextView(this); 
        textView2.setId(i+1);
        textView2.setTextSize(30);
        textView2.setBackgroundColor(Color.GREEN);
        textView2.setTextColor(Color.RED);
        textView2.setTypeface(Typeface.MONOSPACE);
        textView2.setText(teststr+String.valueOf(i));  
        if (i>0)
        {
            RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            lay.addRule(RelativeLayout.RIGHT_OF, lastid);
            ll.addView(textView2, lay);             
        } else {
            ll.addView(textView2);
        };
        lastid = textView2.getId();

    }

However, I dont know how to determine when to change line. I just keep on putting new textview to the right of the last one created. Can anyone tell me the correct logic to do the task? many thanks.

Umur Kontacı
  • 35,403
  • 8
  • 73
  • 96
manhon
  • 683
  • 7
  • 27

1 Answers1

1

Easy fix. Switch to a dynamically-created LinearLayout, and set its orientation to vertical:

lay.setOrientation(LinearLayout.VERTICAL);
Avram Score
  • 3,927
  • 2
  • 17
  • 12
  • thanks for reply, but after switching to LinearLayout, what i see is below:http://i.imgur.com/Y0URyQZ.png?1 – manhon Mar 28 '13 at 14:00
  • can anyone help? i still cannot get the layout i want. many thanks – manhon Mar 29 '13 at 02:50
  • Ah, I guess I didn't take a good look at your desired output. Well, if that's the case, why not simply append instances of the string to a stringbuilder, and modify the string shown within an EditText view? Lighter-weight that way, too. – Avram Score Apr 02 '13 at 04:16