Looking Forward
Looking Forward

Reputation: 3585

Add multiple text views programmatically in Android

Here I have to add text view programmatically based on array list size. Text views should be appear in row like continues pattern... eg. tv1, tv2, tv3 and so on till the size of array list.

But here I am getting text views which are appearing on each other. I can't read the text on them. Here is my code:

ArrayList<String> languageNames = new ArrayList<String>();
RelativeLayout rl = (RelativeLayout)findViewById(R.id.rl);
if(languageNames.size()>0)
{
    int size = languageNames.size();
    TextView[] tv = new TextView[size];
    RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    p.addRule(RelativeLayout.BELOW, tvLocation.getId());

    for(int i=0; i<size; i++)
    {
        tv[i] = new TextView(getBaseContext());
        tv[i].setText(languageNames.get(i).toString());
        tv[i].setLayoutParams(p);
        tv[i].setPadding(50, 50, 0, 0);
        tv[i].setTextColor(Color.parseColor("#000000"));
        rl.addView(tv[i]);
    }
}
else
{

}

what needs to be done so that I can get text views in appropriate manner?

Upvotes: 0

Views: 2578

Answers (2)

ADT
ADT

Reputation: 255

Try this code:

LinearLayout rl = (LinearLayout)findViewById(R.id.mainLayout);

    TextView[] tv = new TextView[10];
    for(int i=0; i<10; i++)
    {
        tv[i] = new TextView(getBaseContext());
        tv[i].setText("TextView "+ i);

        tv[i].setPadding(50, 50, 0, 0);
        tv[i].setTextColor(Color.parseColor("#000000"));
        rl.addView(tv[i]);
    }

Hope this will help you

Upvotes: 0

Tarun
Tarun

Reputation: 13808

Add buttons inside a LinearLayout and add this LinearLayout in the RelativeLayout.

RelativeLayout r1 = (RelativeLayout) findViewById(R.id.r1);
 RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.BELOW, tvLocation.getId());
LinearLayout LL = new LinearLayout(getBaseContext());
LL.setOrientation(LinearLayout.VERTICAL);

   for (int i=0;i< size;i++) {
     tv[i] = new TextView(getBaseContext());
    tv[i].setText(languageNames.get(i).toString());

    tv[i].setPadding(50, 50, 0, 0);
    tv[i].setTextColor(Color.parseColor("#000000"));
     LL.addView(tv);   
 }
r1.addview(LL, p);

Upvotes: 1

Related Questions