2

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

FET
  • 892
  • 4
  • 10
  • 33
  • 1
    you don't need 4 textviews to display 4 lines of text. You just need to add "\n" in between each word to get a new line and it will fit in a single TextView – Frank D. Sep 10 '15 at 13:51
  • Alright, thanks! But now the issue of creating a TextView programmatically persists – FET Sep 10 '15 at 13:53
  • It's way more simple to define the TextView in your XML if you only need one. If you really have to do it programmatically, check this thread: http://stackoverflow.com/questions/3204852/android-add-a-textview-to-linear-layout-programmatically – Frank D. Sep 10 '15 at 13:55
  • Ok, well, if I add the Text View via the XML, then how do I add the lines in it? – FET Sep 10 '15 at 13:57
  • Should I use a for loop? How then? – FET Sep 10 '15 at 13:57
  • I added an example below – Frank D. Sep 10 '15 at 14:01
  • Have a look at this http://stackoverflow.com/questions/4394293/create-a-new-textview-programmatically-then-display-it-below-another-textview hope this helps. – PunK _l_ RuLz Sep 10 '15 at 14:03

3 Answers3

4

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

Mobile Team ADR-Flutter
  • 12,062
  • 2
  • 29
  • 49
3

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);
Frank D.
  • 1,306
  • 1
  • 13
  • 23
1

You can use for loop like this,

for(i=1;i<=num;i++){
    txtView.append(word+"\n");
}
Shruti
  • 381
  • 6
  • 21