setText doesn't set text to EditText
Asked Answered
B

5

8

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 EditTexts 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?

Bookkeeper answered 14/11, 2012 at 21:6 Comment(6)
may we see your layout xml code? Do you even reach name.setText(tempSpotName); ? Did you debug this?Monzon
Just checking, but setText is getting called but not displaying correct? The the if statement isnt preventing it from being called? (just double checking since setText should work)Mismatch
Are you calling setText() anywhere else? Possibly in onResume()?Addam
@Goot and @Mismatch Yes, I rech the if statement and it is being called. Otherwise setHint won't work... @Addam I think I call it in the onCreate()... I'm gonna check it...Bookkeeper
@Addam okey... I didn't realize I was calling setText in the onCreate()... I'm genius lol Thank you very much! If you answer I'll check it as the accepted answert!Bookkeeper
how come the text will stay empty when you call setText(value) AFTER the view was created?Monzon
A
17

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 like onResume() this will override any text set in onActivityResult().

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

Addam answered 14/11, 2012 at 21:30 Comment(2)
Any changes in onResume() also overrides changes in onNewIntent().Nikola
The same goes for Fragments, you need to make updates in onViewStateRestored() method (which was added in API 17).Bricklaying
K
10

Some times changing edittext in onactivity result is not working. I too faced the same problem

instead of setting

edittext.settext("yourtext");

change to following in onactivityresult

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

It worked for me.

Kbp answered 11/3, 2016 at 10:11 Comment(1)
probably edittext.post(new Runnable() { @Override public void run() { edittext.setText("yourtext"); /*and note setText is caMel */ } });Favor
M
3

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:

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 :)

Monzon answered 14/11, 2012 at 21:18 Comment(0)
B
0

You can force a EditText.SetText("blablabla..."); inside your OnActivity Result in 3 EASY steps:

  1. Reload your layout into your Activity
  2. Rebind your EditText
  3. use SetText as usual.

In this sample code, I pass a URL string with and intent and write it into 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()); 
        }
Bilharziasis answered 17/3, 2014 at 9:50 Comment(0)
P
0

this solve works for me (kotlin)

val editText= EditText(context)
editText.apply {
   layoutParams = LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTEXT
        )}
editText.doOnPreDraw {
    editText.setText("your text")
}
Parton answered 18/11, 2020 at 9:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.