Pow
Pow

Reputation: 1367

Viewing Dynamic text in Android

I am trying to print Hello + written text in text box (As I posted earlier). Here's the code I have done. But instead of printing Hello+written text, its only printing Hello after clicking the button.

Any suggestion will be appreciated. Thanks.

public class myActivity1 extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

      /* // testing add button
         setContentView(R.layout.content_layout_id);
      */

        final TextView nameText= (TextView) findViewById(R.id.entry);  
        //final TextView tv1 = new TextView(this);
        final TextView tv2 = new TextView(this);

        tv2.setText("Hello"+ nameText.getText().toString());

        final Button button = (Button) findViewById(R.id.Button01);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                setContentView(tv2);


            }
        });

    }
}

Upvotes: 0

Views: 2071

Answers (2)

CommonsWare
CommonsWare

Reputation: 1006604

It is not printing anything after "Hello", because nameText is empty.

Now, perhaps nameText is really an EditText, and you are trying to fill it in at runtime. If so, you need to check the contents of nameText in the OnClickListener. Right now, you are checking it in onCreate(), before the UI is even presented to the user.

Upvotes: 1

Frank
Frank

Reputation: 3296

Should your setContentView(tv2) line be:

tv2.setText("Hello"+ nameText.getText().toString());

Instead?

Upvotes: 0

Related Questions