I'm having a problem that I've never had before in almost three years of developing with Android...
I want to take a picture and after the picture is taked, the EditText
s of the activity become clear. What I'm doing is set the values of the EditText
to Strings
using getText().toString()
to restore them after taking the picture.
The strings are stored perfectly with the data, but when I use setText
, it doesn't work... The strange thing is that setHint
works!
How can it be?
Here's the code I'm using:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Image captured and saved to fileUri specified in the Intent
grabImage(imgView);
for (int u = 0; u <= 2; u++)
{
if (savedImgs[u].equals(""))
{
imgs = u + 1;
savedImgs[u] = photo.toString();
break;
}
}
/*Bitmap thumbnail = (Bitmap) data.getExtras().get("data"); ---> It is a small bitmap, for icons...
imgView.setImageBitmap(thumbnail);
imgView.setVisibility(View.VISIBLE);*/
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
} else {
Toast.makeText(this, "Image couldn't be taken. Try again later.", Toast.LENGTH_LONG).show();
}
}
if (!tempSpotName.equals("") || !tempSpotDesc.equals("")) {
name.setText(tempSpotName);
description.setText(tempSpotDesc);
}
}
name
and description
are global EditTexts
and tempSpotName
and tempSpotDesc
are global Strings
.
How can I set the text?
You can force a EditText.SetText("blablabla..."); inside your OnActivity Result in 3 EASY steps:
In this sample code, I pass a URL string with and intent and write it into a TextView:
First of all you have to debug this.
There is a class called TextWatcher. This will be called every time your Textbox.Text will change. So this is easier to debug and handle the problem. Url: http://developer.android.com/reference/android/text/TextWatcher.html
Example for implementation:
Good luck :)
Some times changing
edittext
inonactivity
result is not working. I too faced the same probleminstead of setting
change to following in
onactivityresult
It worked for me.
onActivityResult()
is not the last method called when returning to an Activity. You can refresh your memory of the Life Cycle in the docs. :)As we discussed in the comments, if you call
setText()
again in methods likeonResume()
this will override any text set inonActivityResult()
.The same goes for Fragments, you need to make updates in onViewStateRestored() method (which was added in API 17).