user5560712
user5560712

Reputation: 51

saving/printing text Android

I have 2 TextView, and 1 Button. I want to be able to click the button and to save the text on the first TextView and show the value on the second TextView.

I've tried this code:

    public void buttonOnClick(){
    Button submit = (Button) findViewById(R.id.submit);
    editName = (EditText) findViewById(R.id.name);
    editEmail = (EditText) findViewById(R.id.email);
    textout = (TextView) findViewById(R.id.outputText);

    submit.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
             textout.setText(editName.getText()+"\n"+editEmail.getText());
        }
    });


}  

But I get an error on 'textout'. When I clock the red light buble, it says 'create a local variable', field text'.

Upvotes: 0

Views: 41

Answers (2)

royB
royB

Reputation: 12977

Your problem stimulates from the fact that EditText getText() returns Editable Object. It's not a String and you can't concat 2 Editable Objects.

You have 2 Options:

textout.setText(editName.getText().toString() + "\n" + editEmail.getText().toString());

And Second you can use SpanableStringBuilder

SpannableStringBuilder sb = new SpannableStringBuilder(editName.getText());
    sb.append("\n").append(editEmail.getText());

The Second options allows you also to decorate the Text and save you the Need to build a String which is (maybe) better.

Upvotes: 0

Collins Abitekaniza
Collins Abitekaniza

Reputation: 4578

Try this

public void onClick(View v) {
         textout.setText(editName.getText().toString()+"\n"+editEmail.getText().toString());
    }

Upvotes: 1

Related Questions