Create a new TextView programmatically then display it below another TextView

JavaAndroidTextviewAndroid Relativelayout

Java Problem Overview


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..

Java Solutions


Solution 1 - Java

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);
}

Solution 2 - Java

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]);	
}

Solution 3 - Java

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.

Solution 4 - Java

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLope EmanoView Question on Stackoverflow
Solution 1 - JavaWeNeighView Answer on Stackoverflow
Solution 2 - JavaBalajiView Answer on Stackoverflow
Solution 3 - JavaWeNeighView Answer on Stackoverflow
Solution 4 - JavaMathias ConradtView Answer on Stackoverflow