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
  • 942
  • 4
  • 13
  • 35
  • 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. :)

  • I think this should work, but when I add the lines on my 2nd Activity, I need to make some imports with multiple choices, which one should I get? – FET Sep 10 '15 at 14:09
  • You mean to say that you have a list of some data and you want to display it in those textviews? –  Sep 10 '15 at 14:11
  • Done, @AndroidWeblineindia – FET Sep 10 '15 at 14:25
  • ohk... it is import android.view.ViewGroup.LayoutParams; –  Sep 10 '15 at 14:38
  • I'll try, by the way I guess I have another issue with the intent, let you know in a minute – FET Sep 10 '15 at 14:43
  • yes I think I have a problem with my intent, I'm leaving you my code, maybe you'll figure out the issue? – FET Sep 10 '15 at 14:54
  • I will mark this as the Correct Answer, as soon as my app will work eheh ;) – FET Sep 10 '15 at 14:58
  • any idea about which might be the issue? – FET Sep 10 '15 at 17:16
2

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
  • 391
  • 1
  • 5
  • 21