50
String[] textArray={"one","two","asdasasdf asdf dsdaa"};
int length=textArray.length;
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(
        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
for(int i=0;i<length;i++){
    TextView tv=new TextView(getApplicationContext());
    tv.setText(textArray[i]);
    relativeParams.addRule(RelativeLayout.BELOW, tv.getId());
    layout.addView(tv, relativeParams);
}

I need to do something like that.. so it would display as

one
two
asdfasdfsomething

on the screen..

Onik
  • 19,396
  • 14
  • 68
  • 91
Lope Emano
  • 503
  • 1
  • 4
  • 6
  • 1
    Briefly, what's wrong with your code? What doesn't work? Btw, if this is an Activity, just use `this` instead of `getApplicationContext()`. – EboMike Dec 09 '10 at 02:33
  • your question title says "display it _below_ another textview" but your question states differently, with all texts in one row - please clarify or format the question text accordingly. – Mathias Conradt Dec 09 '10 at 02:34
  • The question wasn't formatted right - I fixed that. (It's probably not intuitive that newlines get swallowed up) – EboMike Dec 09 '10 at 02:40
  • use Linear Layout instead of RelativeLayout – Vishal Patoliya ツ Sep 15 '16 at 10:31

4 Answers4

77

If it's not important to use a RelativeLayout, you could use a LinearLayout, and do this:

LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);

Doing this allows you to avoid the addRule method you've tried. You can simply use addView() to add new TextViews.

Complete code:

String[] textArray = {"One", "Two", "Three", "Four"};
LinearLayout linearLayout = new LinearLayout(this);
setContentView(linearLayout);
linearLayout.setOrientation(LinearLayout.VERTICAL);        
for( int i = 0; i < textArray.length; i++ )
{
    TextView textView = new TextView(this);
    textView.setText(textArray[i]);
    linearLayout.addView(textView);
}
WeNeigh
  • 3,489
  • 3
  • 27
  • 40
23

Try this code:

final String[] str = {"one","two","three","asdfgf"};
final RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
final TextView[] tv = new TextView[10];

for (int i=0; i<str.length; i++)
{
    tv[i] = new TextView(this); 
    RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams
         ((int)LayoutParams.WRAP_CONTENT,(int)LayoutParams.WRAP_CONTENT);
    params.leftMargin = 50;
    params.topMargin  = i*50;
    tv[i].setText(str[i]);
    tv[i].setTextSize((float) 20);
    tv[i].setPadding(20, 50, 20, 50);
    tv[i].setLayoutParams(params);
    rl.addView(tv[i]);  
}
Onik
  • 19,396
  • 14
  • 68
  • 91
Balaji
  • 2,024
  • 17
  • 13
18
public View recentView;

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Create a relative layout and add a button
        relativeLayout = new RelativeLayout(this);
        btn = new Button(this);
        btn.setId((int)System.currentTimeMillis());
        recentView = btn;
        btn.setText("Click me");
        relativeLayout.addView(btn);


        setContentView(relativeLayout);

        btn.setOnClickListener(new View.OnClickListener() {

            @Overr ide
            public void onClick(View view) {

                //Create a textView, set a random ID and position it below the most recently added view
                textView = new TextView(ActivityName.this);
                textView.setId((int)System.currentTimeMillis());
                layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
                layoutParams.addRule(RelativeLayout.BELOW, recentView.getId());
                textView.setText("Time: "+System.currentTimeMillis());
                relativeLayout.addView(textView, layoutParams);
                recentView = textView;
            }
        });
    }

This can be modified to display each element of a String array in different TextViews.

WeNeigh
  • 3,489
  • 3
  • 27
  • 40
  • I had issues when using this logic with multiple views, as multiple items were assigned the same id, I changed the current time with a counter to solve the issue – Roberto Aug 19 '18 at 08:44
4

You're not assigning any id to the text view, but you're using tv.getId() to pass it to the addRule method as a parameter. Try to set a unique id via tv.setId(int).

You could also use the LinearLayout with vertical orientation, that might be easier actually. I prefer LinearLayout over RelativeLayouts if not necessary otherwise.

Mathias Conradt
  • 28,420
  • 21
  • 138
  • 192
  • How can you assign a unique id programmatically ? I mean how to be sure it will be unique, when you'll never know the values of the ids in xml, and the ids to come – Dany Y Jun 19 '13 at 16:08
  • You will know the values of the ids in xml, why wouldn't you? Make ids like `thisKindofElem001`, `thisKindofElem002` etc. – fiatjaf Jul 25 '13 at 23:52