Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic TextView in Relative layout

Tags:

android

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.

like image 888
igde_ Avatar asked Jul 05 '11 12:07

igde_


3 Answers

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

like image 152
inazaruk Avatar answered Oct 23 '22 20:10

inazaruk


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.

like image 20
Mudassir Avatar answered Oct 23 '22 20:10

Mudassir


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); 
    }
like image 1
Prasaathviki Avatar answered Oct 23 '22 19:10

Prasaathviki