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);
}
});
}
}
It is not printing anything after “Hello”, because
nameTextis empty.Now, perhaps
nameTextis really anEditText, and you are trying to fill it in at runtime. If so, you need to check the contents ofnameTextin theOnClickListener. Right now, you are checking it inonCreate(), before the UI is even presented to the user.