1

I want to add some textview to linear layout with a loop but I think I'm just adding one because the text of my text view is not displayed vertically.

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

int contador = 0;
while ((inputString = inputReader.readLine()) != null) {
    contador++;
    stringBuffer.append(inputString + "\n");
    TextView tvRutina = new TextView(this);
    tvRutina.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
    tvRutina.setPadding(0, 3, 0, 3);
    tvRutina.setId(contador);
    tvRutina.setText(inputString);

    llPrincipal.addView(tvRutina);
}

Xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >

2 Answers 2

1

just add below code in ur while loop :-

LinearLayout A = new LinearLayout(this);
A.setOrientation(LinearLayout.VERTICALLY);
llPrincipal .addView(A);

or

while ((inputString = inputReader.readLine()) != null) {
    LinearLayout A = new LinearLayout(this);
A.setOrientation(LinearLayout.VERTICALLY);
contador++;
stringBuffer.append(inputString + "\n");
TextView tvRutina = new TextView(this);
tvRutina.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
tvRutina.setPadding(0, 3, 0, 3);
tvRutina.setId(contador);
tvRutina.setText(inputString);

A.addView(tvRutina);
llPrincipal .addView(A);
}
Sign up to request clarification or add additional context in comments.

1 Comment

I'm sorry but that does not work also need to add other linear layout
0
LinearLayout MainLL= (LinearLayout) findViewById(R.id.myLayoutId); 
for(int i=0; i<5; i++){
 TextView text = new TextView(this);
 text.setText("The Value of i is :"+i); // <-- does it really compile without the + sign?
 text.setTextSize(12);  
 text.setGravity(Gravity.LEFT);
 text.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,          

 LayoutParams.WRAP_CONTENT));
 MainLL.addView(text);
}

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.