0

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?

5
  • You LinearLayout instead of RelativeLayout and set orientation as VERTICAL Commented Sep 10, 2013 at 7:39
  • set id for the TextViews. Commented Sep 10, 2013 at 7:41
  • p.addRule(RelativeLayout.BELOW, tvLocation.getId()); will make all the textviews stack below tvLocation. Commented Sep 10, 2013 at 7:41
  • @Tarun... I need all textviews below tvLocation.. Commented Sep 10, 2013 at 7:43
  • User LinearLayout and set its orientation:Vertical it will set below textview Commented Sep 10, 2013 at 7:53

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

3 Comments

@Tarun...thanks for your answer. Here I am getting data in vertical manner... I want them in series like in horizontal mode...
You can change LL.setOrientation(LinearLayout.VERTICAL); to LL.setOrientation(LinearLayout.HORIZONTAL);
@Tarun...I am getting output but here text views' size are different than the default text views. how can I set them as default size?
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.