tags:

views:

73

answers:

2

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);


        }
    });

}

}

A: 

Should your setContentView(tv2) line be:

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

Instead?

Frank
A: 

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.

CommonsWare
Thanks a lot ... It worked! Actually I need to go through the basic documentation first!
zeb