I'm having a problem with setting text to a TextView:
TextView android:editable = "true".
In my .java it seems like that this should work:
text = (EditText) findViewById(R.id.this_is_the_id_of_textview);
text.setText("TEST");
But it doesn't. Can anyone tell me what's wrong here?
Try Like this :
TextView text=(TextView)findViewById(R.id.textviewID);
text.setText("Text");
Instead of this:
text = (EditText) findViewById(R.id.this_is_the_id_of_textview);
text.setText("TEST");
The code should instead be something like this:
TextView text = (TextView) findViewById(R.id.this_is_the_id_of_textview);
text.setText("test");
To settext in any of in activity than use this below code... Follow these step one by one:
1.Declare
private TextView event_post;
2.Bind this
event_post = (TextView) findViewById(R.id.text_post);
3.SetText in
event_post.setText(event_post_count);
I was having a similar problem, however my program would crash when I tried to set the text. I was attempting to set the text value from within a class that extends AsyncTask, and that was what was causing the problem.
To resolve it, I moved my setText to the onPostExecute method
protected void onPostExecute(Void result) {
super.onPostExecute(result);
TextView text = (TextView) findViewById(R.id.errorsToday);
text.setText("new string value");
}
In your XML, you had used Textview, But in Java Code you had used EditText instead of TextView. If you change it into TextView you can set Text to to your TextView Object.
text = (TextView) findViewById(R.id.this_is_the_id_of_textview);
text.setText("TEST");
hope it will work.
Or you can do this way :
((TextView)findViewById(R.id.this_is_the_id_of_textview)).setText("Test");
You set the text to a field that was rendered in another instance of your activity residing in cash,say when it was landscape oriented. Then it was recreated due to some reason. You need to set the text in an unusual way by saving the string to SharedPreferences and force relaunch the activity using recreate(), say for mOutputText:
@Override
protected void onResume() {
super.onResume();
String sLcl=mPreferences.getString("response","");
if(!sLcl.isEmpty()){
mOutputText.setText(sLcl);
mPreferences.edit().putString("response","").commit();
}
}
private void changeText() {
mPreferences.edit().putString("response",responseText).commit();
recreate();
}
@Override
protected void onActivityResult(
int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case CAPTURE_MEDIA_RESULT_CODE:
if (null == data || null == data.getData()) {
showMessage("Sorry. You didn't save any video");
} else {
videoUri = data.getData();
mProgress = new ProgressDialog(this);
mProgress.setMessage("Uploading onYoutube ...");
authorizeIt();// SAY YOU CALL THE changeText() at the end of this method
}
break;
}
}
Source: Stackoverflow.com