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.
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.
The problem should be, you're sending Editable
, not String
. Try this:
final String string = input.getText().toString();
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With