11

I am triying to use dynamic layout for comment part of my project but when i settext of textview dynamicly the output only appears in top of the screen. And it puts the output over the other outputs

RelativeLayout ll=(RelativeLayout) findViewById(R.id.rl);
        for(int i = 0; i < 20; i++) {
        TextView cb = new TextView(this);
        cb.setText("YORUMLAR"+yorum[0]+i);

         cb.setTextSize(30);
          ll.addView(cb); 

        }

So how can i put the output on the bottom of the screen linearly.

igde_
  • 261
  • 3
  • 6
  • 15
  • 1
    ...by setting proper relative atributes to your textViews; for now, you are just setting the text, with no position, and default it's on top of the screen. – Adinia Jul 05 '11 at 13:06

3 Answers3

22

You should use LinearLayout to automatically add one TextView after another.


Assuming you can't live without RelativeLayout, you'll need to dynamically generate ids for all TextView you create in order to put one view under another. Here is example:

public class HelloWorld extends Activity
{       
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {       
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity);

        RelativeLayout layout = (RelativeLayout)findViewById(R.id.layout);

        Random rnd = new Random();
        int prevTextViewId = 0;     
        for(int i = 0; i < 10; i++)
        {                       
            final TextView textView = new TextView(this);
            textView.setText("Text "+i);     
            textView.setTextColor(rnd.nextInt() | 0xff000000);            

            int curTextViewId = prevTextViewId + 1;
            textView.setId(curTextViewId);
            final RelativeLayout.LayoutParams params = 
                new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, 
                                                RelativeLayout.LayoutParams.WRAP_CONTENT);

            params.addRule(RelativeLayout.BELOW, prevTextViewId);
            textView.setLayoutParams(params);

            prevTextViewId = curTextViewId;
            layout.addView(textView, params);
        }              
    }    
}

enter image description here

Andro Selva
  • 53,910
  • 52
  • 193
  • 240
inazaruk
  • 74,247
  • 24
  • 188
  • 156
  • Thanks a million! I have a cursor that can have null values. People hated seeing the blank stuff so I created a dynamic textview using "if not null, do this" statements then used your example to make the dynamic textview IDs for each one that needs to be viewed. The key was to set the curTextViewId = prevTextViewID + 1 in each if statement then at the end of each one be sure to set prevTextViewId = curTextViewId so the next if statement gets the new prevTextViewId. Thanks! – Opy Feb 01 '12 at 17:44
2

You've to provide the location of your newly added view. As @Adinia said, with no position, it will be aligned to the top by default. So you can use the following code to do it with RelativeLayout;

RelativeLayout containerLayout = (RelativeLayout) findViewById(R.id.container);

for (int i = 0; i < 20; i++) {

    TextView dynaText = new TextView(this);

    dynaText.setText("Some text " + i);
    dynaText.setTextSize(30);

    // Set the location of your textView.
    dynaText.setPadding(0, (i * 30), 0, 0);

    containerLayout.addView(dynaText);
}

If you want to show multiple textviews one after the other, then you should go with LinearLayout.

Mudassir
  • 13,031
  • 8
  • 59
  • 87
1

You may also add Dynamic textview to relative layout. Here with i have attached some code this may help you.

    RelativeLayout ll=(RelativeLayout) findViewById(R.id.rl);
    for(int i = 0; i < 20; i++) 
    {
       TextView cb = new TextView(this);
       cb.setText("YORUMLAR"+yorum[0]+i);
       cb.setTextSize(30);
       cb.setId(2000+i);
       RelativeLayout.LayoutParams TextViewLayoutParams = new RelativeLayout.LayoutParams(100,100);
       if (i != 0 )DispViewLayoutParams.addRule(RelativeLayout.BELOW, 2000 - (i-1));
       ll.addView(cb,TextViewLayoutParams); 
    }
Prasaathviki
  • 1,147
  • 2
  • 11
  • 22