user1260584
user1260584

Reputation: 159

How to display a something from an EditText?

I thought making thing part of the app would be easy, however I was wrong. I wish to have a textView display whatever the user wrote in the editText. This is what I tried.

add.setOnClickListener(new View.OnClickListener() {
    public void onClick(View arg0) {
    // TODO Auto-generated method stub
        myTextView.setText(myEditText.getText().toString());
    // of course I would use variables in place of the
        // myTextView and myEditText
    }
});

This is another way I tried to get this done.

add.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
    // TODO Auto-generated method stub
    //num1 is my String variable
        num1 = myEditText.getText().toString();
    myTextView.setText(num1);
    }
}); 

Both times the textView comes up with nothing in it. Thank you for any help!

Upvotes: 0

Views: 186

Answers (6)

Stelios M
Stelios M

Reputation: 237

From your code i understood that there is a button here too so try this should work:

public class Activity1 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            Button btn = (Button)findViewById(R.id.mybutton);
            btn.setOnClickListener(btncall);
}

  private OnClickListener btncall = new OnClickListener()
  {
      public void onClick(View v)
      {
      TextView mytextView = (TextView) findViewById(R.id.MytextView);   
      EditText myeditText = (EditText) findViewById(R.id.MyeditText);
      mytextView.setText(myeditText.getText().toString());
      }
  };

}

Upvotes: 0

andro-girl
andro-girl

Reputation: 8129

have you check the visibility of textview ?before clicking add button it is invisible rite?then u have to set the visibility on add button click.

Upvotes: 0

Tprice88
Tprice88

Reputation: 661

I usually use GetDlgItemText.

char Buffer[120];
GetDlgItemText(hwndDlg, (control), buffer, sizeof(buffer));

This will read it and store it in buffer.

Upvotes: 1

Phil
Phil

Reputation: 36299

onClickListener merely responds to user clicks. You need to implement a TextWatcher on your EditText. The most straightforward way of doing this is to implement TextWatcher in your class, then make a call to myEditText.addTextChangedListener(this).

I recommend adding something like the following to your onTextChanged method:

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
    myTextView.setText(myTextView.getText()+s);//or something like this...
}

Upvotes: 1

Shubhayu
Shubhayu

Reputation: 13562

First of all check whether the control is coming to your setOnClickListener(). Put in a Log to find that out.

Next make sure that "add" is the button or item that u r using to initiate the copy process.

This statement of yours is correct.

myTextView.setText(myEditText.getText().toString());

Though you do not require the toString(). Doesnt really make a difference. I suggest you check that your textview and edittext is fine.

Upvotes: 0

Kevin
Kevin

Reputation: 532

In the EditText the getText call should you return the String, I don't believe you need to call the ToString method on it. The way you are using it in the onClickListener implies you have a button that should be calling a function to set the text into the textview. If you want it dynamically you should be able to use onTextChanged to fill in the data.

Upvotes: 0

Related Questions