1

I have an EditText where the user enters a name and I want this name to be assigned to a string variable for later use.

EditText et=(EditText)findViewById(R.id.editText1);
        final String value=et.getText().toString();
        Button b=(Button)findViewById(R.id.button1);
        b.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                TextView tv=(TextView)findViewById(R.id.textView2);
                tv.setText(value);
            }

        });

This is the code I am using. I enter some text in editText and when I click the button, the textView should display content in edittext. But when I click the button the textview becomes invisible i.e. it's having null value.

gunar
  • 14,660
  • 7
  • 56
  • 87
user2648852
  • 711
  • 1
  • 11
  • 13

3 Answers3

2

change your String value initialization inside the onClick, currently you have it initialized on as soon you get editText reference ... so it does not update to the text you have entered during runtime

New Code

final EditText et=(EditText)findViewById(R.id.editText1);
        Button b=(Button)findViewById(R.id.button1);
        b.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                String value=et.getText().toString();
                TextView tv=(TextView)findViewById(R.id.textView2);
                tv.setText(value);
            }

        });
Shubhank
  • 21,721
  • 8
  • 65
  • 83
  • this is working. but if i want the text in edittext to be displayed in a different activity how does this work? my app is like, i enter name and age and submit it. when submitted it will redirect the page to another page where i need to display the contents of edittext – user2648852 Oct 11 '13 at 14:21
  • save it in sharedPreferences so that it persists during app session or directly pass to other activity as intent extra. – Shubhank Oct 11 '13 at 14:35
0

Try something like this

final EditText et=(EditText)findViewById(R.id.editText1);
final EditText tv=(EditText)findViewById(R.id.editText2);
Button b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener(){

    @Override
    public void onClick(View v) {
        tv.setText(et.getText().toString());
    }

});
guikk
  • 195
  • 5
  • this is working. but if i want the text in edittext to be displayed in a different activity how does this work? my app is like, i enter name and age and submit it. when submitted it will redirect the page to another page where i need to display the contents of edittext – user2648852 Oct 11 '13 at 14:22
  • You can pass the value of the edittext as a parameter for the new activity – guikk Oct 11 '13 at 14:28
  • Have a look on this thread, maybe it will help you for pass parameter : http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android – guikk Oct 11 '13 at 14:28
  • i tried the solution posted by user914425 and edited by Darshan Computing. it dint work. in the next activity the textboxes again go invisible – user2648852 Oct 11 '13 at 14:48
  • sorry. my bad. i put the parameter in wrong format. it works now. – user2648852 Oct 11 '13 at 15:34
0

Try like this

String value;

OnCreate(){

EditText et=(EditText)findViewById(R.id.editText1);
TextView tv=(TextView)findViewById(R.id.textView2);
Button b=(Button)findViewById(R.id.button1);

        b.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                value=et.getText().toString();
                tv.setText(value);
            }

        });


}
Mukesh Kumar Singh
  • 4,512
  • 2
  • 22
  • 30