Ok, I'm using the code:
EditText editText = (EditText) findViewById(R.id.editText1);
String value = editText.getText().toString();
to get text from an EditText, but how do I get it to display with a TextView? Also, how will the code change if the TextView is in a different Activity?
Like this:
Well, I tried doing this:
EditText input = (EditText) findViewById(R.id.editText1);
String thing = input.getText().toString();
TextView display = (TextView) findViewById(R.id.textView1);
String text = display.getText().toString();
text.setText(thing);
But I get an error "The method setText(String) is undefined for the type String" on this line:
text.setText(thing);
how do I get it to display with a TextView?
Here is code which will update display (the TextView used in your example) with the contents of input (the EditText used in your example) whenever enter key is pressed on the keyboard (I saw you requested this in a comment to another Answer):
// get reference to the TextView
final TextView display = (TextView) findViewById(R.id.textView1);
// get reference to EditText and set listener to update TextView
final EditText input = (EditText) findViewById(R.id.editText1);
input.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
display.setText(input.getText());
return true;
}
return false;
}
});
Code above is inside of a single Activity.
Also, how will the code change if the TextView is in a different Activity?
If display (the TextView in your example) is in another Activity, then one quick way to update it is to store the value of input (the EditText in your example) as a SharedPreference and have display Activity's onResume read in the value:
// Activity with display
public class DisplayActivity extends Activity {
private TextView display;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// get reference to the TextView
display = (TextView) findViewById(R.id.textView1);
}
public void onResume() {
super.onResume();
String inputsValue = getSharedPreferences("myprefs", 0)
.getString("input", "" /** Default value*/);
display.setText(inputsValue);
}
}
// Activity with input
public class InputActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// get reference to EditText and store in SharedPreference
final EditText input = (EditText) findViewById(R.id.editText1);
input.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
getSharedPreferences("myprefs", 0).edit()
.putString("input", input.getText().toString()).commit();
return true;
}
return false;
}
});
}
}
You should replace all hardcoded string literals in my code with a code constant