SetText does not set text in EditText

I had a problem that I had never had before almost three years of development with Android ...

I want to take a picture, and after the image is recorded, the EditText activity will become clear. What I'm doing is setting the values ​​of EditText - Strings , using getText().toString() to restore them after shooting.

Strings are perfectly saved with data, but when I use setText , it does not work ... It is strange that setHint works!

How can it be?

Here is 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 to set the text?

+6
source share
4 answers

onActivityResult() not the last method called when returning to Activity. You can update your life cycle memory in documents. :)

As we discussed in the comments, if you call setText() again in methods like onResume() , this will override any text set to onActivityResult() .

The same goes for Fragments, you need to make updates in the onViewStateRestored () method (which was added in API 17).

+14
source

First of all you need to debug this.

There is a class called TextWatcher. This will be called every time your Textbox.Text changes. So it’s easier to debug and deal with this problem. Url: http://developer.android.com/reference/android/text/TextWatcher.html

Example for implementation:

name.addTextChangedListener (new TextWatcher () {

  @Override public void afterTextChanged(Editable s) { // TODO Auto-generated method stub } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { doSomething(); } }); 

Good luck :)

+3
source

Several times changing the edittext in onactivity result does not work. I also faced the same problem

instead of setting

 edittext.settext("yourtext"); 

change to next in onactivityresult

 edittext.post(new Runnable(){ edittext.settext("yourtext"); }); 

It worked for me.

+2
source

You can force EditText.SetText ("blablabla ..."); inside your OnActivity result in 3 easy steps:

  • Reload the layout in your activity.
  • Drag your EditText
  • use SetText as usual.

In this code example, I pass a URL string with both intentions and write them to a TextView:

 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK) { QRdata= data.getStringExtra("QRURL"); if (QRdata.length()>0) { //Step1 setContentView(R.layout.activity_confirmpackage); //Step2 TextView qrtxt=(TextView)this.findViewById(R.id.qrurl); //Setp 3,VoilΓ ! qrtxt.setText(QRdata.toString()); } 
0
source

All Articles