Guizinhobeback
Guizinhobeback

Reputation: 86

TextView does not show the message

So, Im trying to show how many characters has in the string that I'm receving for one entrance.

This is string is going to message. But the problem is, if I put this line

setContentView(R.layout.activity_display_message); 

, It doesnt show the string and it shows the already set string. If I remove this line, the code works but only shows one textView, the second doesn't work.

Here is my code:

    Intent intent = getIntent();

    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

    TextView textView = new TextView(this);
    textView.setTextSize(40);

    textView.setText(message);

    setContentView(textView);
    setContentView(R.layout.activity_display_message);
    //until here everything is working

    TextView myTextView = (TextView) findViewById(R.id.mytextview);
    myTextView.setText("My double value is ");

I have the ID mytextview on the xml file.

Upvotes: 0

Views: 172

Answers (2)

Raghunandan
Raghunandan

Reputation: 133570

You only need

String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
setContentView(textView);

You can use textView.append(value); textView.append("\n") instead of inflating a layout

OR You only need

setContentView(R.layout.activity_display_message);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView myTextView = (TextView) findViewById(R.id.mytextview);
myTextView2.append("My double value is ");
myTextView2.append("\n"); // new line
myTextView.append(message);

Assuming activity_display_message.xml has a textview with id mytextView

If you need another textview

TextView myTextView2 = (TextView) findViewById(R.id.mytextview2);
// need to have another textview with id mytextview2 in activity_display_message.xml
myTextView2.setText("My double value is ");

But instead you can use append with a single textview

Upvotes: 1

Dimmerg
Dimmerg

Reputation: 2138

You should paste both of your text views into layout xml:

  Intent intent = getIntent();

    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

    setContentView(R.layout.activity_display_message);
    //until here everything is working

    TextView myTextView1 = (TextView) findViewById(R.id.mytextview1);
    myTextView1.setText(message);

    TextView myTextView2 = (TextView) findViewById(R.id.mytextview2);
    myTextView2.setText("My double value is ");

Upvotes: 0

Related Questions