FET
FET

Reputation: 942

Creating multiple TextViews programmatically

I've just entered the Android World, I'm using Android Studio and a Book to get started, so after have read some chapters I want to make some practice of what I've just read.

I'd like to create a simple app that asks you for a word and a number and after clicking a button you get a brand new activity with the word you submitted, displayed the exact amount you said before.

Example: Hello, 4 = Hello Hello Hello Hello (vertically)

So I did create this method in the main activity:

public void submit(){

    EditText Edtword = (EditText) findViewById(R.id.text);
    EditText Edtnum = (EditText) findViewById(R.id.number);

    String word = Edtword.getText().toString();
    int num = Integer.parseInt(Edtnum.getText().toString());

    Intent intent = new Intent(this, display.class);
    intent.putExtra(display.EXTRA_MESSAGE, word);
    intent.putExtra("number", (int)num);
    startActivity(intent);
}

And this second Activity launched by a button:

public class display extends AppCompatActivity {

public static final String EXTRA_MESSAGE = "word";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display);
    Intent intent = getIntent();

    String word = intent.getStringExtra(EXTRA_MESSAGE);
    int num = intent.getIntExtra("number", 0);


}

What should I add in the 2nd activity in order to create programmatically those TextViews? I tried with loops but couldn't go successful.

Thanks

Upvotes: 2

Views: 11425

Answers (3)

Shruti
Shruti

Reputation: 311

You can use for loop like this,

for(i=1;i<=num;i++){
    txtView.append(word+"\n");
}

Upvotes: 0

user4571931
user4571931

Reputation:

In your 2nd activity, 1st you need a LinearLayout with attribute android:orientation="vertical" defined in AndroidManifest file. i.e:

<LinearLayout 
        android:id="@+id/llMain"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">
</LinearLayout>

Then you can write the code as shown below in the java file:

LinearLayout m_ll = (LinearLayout) findViewById(R.id.llMain);
        for(int i=0;i<num;i++)
        {
            TextView text = new TextView(this);
            text.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            text.setText(""+i);
            m_ll.addView(text);
        }

I still believe that the approach suggested by Frank D. would be optimal, but this is just for your reference, Hope it helps. :)

Upvotes: 4

Frank D.
Frank D.

Reputation: 1306

I wouldn't add TextViews programmatically in your case, it's too complex for your goal. A single TextView (just define it in your layout XML) can hold multiple lines of text.

TextView yourTextView = (TextView) findViewById(R.id.textView); //however your textview is defined in your xml
String word = "Hello";
int num = 5; //or whatever value

String multiLineText = ""; //empty at first
for(int i = 0, i < num; i++){ 
   multiLineText = multiLineText + word + "\n";
}

yourTextView.setText(multiLineText);

Upvotes: 2

Related Questions