Arshad Ali
Arshad Ali

Reputation: 3274

How to set text of TextView ?

I am facing a problem of setting the text to TextView in android my code is :

public class Main extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button button = (Button) findViewById(R.id.button1);
        final TextView text = (TextView) findViewById(R.id.textView1);
        final EditText input = (EditText) findViewById(R.id.editText1);
        final String string = input.getText();
        button.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                text.setText(string);                   
            }
        });
    }
}

if I write

    final Editable string = input.getText();

then it works.....!!!!

Now I want to send data of EditText to next Activity like this :

public class Main extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button button = (Button) findViewById(R.id.button1);
        final TextView text = (TextView) findViewById(R.id.textView1);
        final EditText input = (EditText) findViewById(R.id.editText1);
        final Editable string = input.getText();
        button.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Intent intent = new Intent(Main.this, Second.class);
                intent.putExtra("thetext", string);
                startActivity(intent);
            }
        });
    }
}

and in Second.java class I get StringExtra in this way:

public class Second extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);

        TextView text = (TextView) findViewById(R.id.textView2);
        String string = getIntent().getExtras().getString("thetext", "not found");
        text.setText(string); /// Here the text is not shown but the default message "not found" is set to `TextView`
    }
}

Please give me way to proceed in development.

Upvotes: 6

Views: 41257

Answers (6)

user2285778
user2285778

Reputation:

     private TextView mTextView;
     private String mString;

mTextView = (TextView) findViewById(R.id.tv);
mString = "hello everyone ! how r u?";
mTextView.setText(mString);

Upvotes: 2

Aerrow
Aerrow

Reputation: 12134

In this line use

final String string = input.getText().toString(); 

instead of

final String string = input.getText();

Upvotes: 0

Sam
Sam

Reputation: 86948

Try something like this:

public class Main extends Activity {
    EditText input;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TextView text = (TextView) findViewById(R.id.textView1);
        input = (EditText) findViewById(R.id.editText1);

        Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(Main.this, Second.class);
                intent.putExtra("thetext", input.getText().toString());
                startActivity(intent);
            }
        });
    }
}

(Hint: The easiest way to post code is to paste your code, select it, and use crtl+k to indent/format it.)

Upvotes: 1

Drake Clarris
Drake Clarris

Reputation: 1045

According to the android docs, the name of the string put into extras must include a package prefix... i.e. som.arshay.dev.thetext Secondly, getExtras() returns a bundle, which is not what you added. You need getStringExtra( name )

Upvotes: 0

skywall
skywall

Reputation: 4005

The problem should be, you're sending Editable, not String. Try this:

final String string = input.getText().toString();

Upvotes: 5

Brayden
Brayden

Reputation: 519

I think the problem is you're actually putting an "Editable" in the intent, not a String. Although close, they're not the same thing. If you toString() your Editable to get a String object and put that in the intent, you should be able to get it back out with getString like you're doing.

Upvotes: 3

Related Questions