Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 701
Arshad Ali Avatar asked May 01 '12 14:05

Arshad Ali


People also ask

Can you set text in Edittext?

In android, we can set the text of EditText control either while declaring it in Layout file or by using setText() method in Activity file.


2 Answers

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

final String string = input.getText().toString();
like image 196
skywall Avatar answered Sep 30 '22 20:09

skywall


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.

like image 37
Brayden Avatar answered Sep 30 '22 21:09

Brayden