Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EditText.getText().toString() sometimes returns ""

I get EditText's text and its length in onTextChanged() method of TextWatcher.
It works fine when I type to add characters, but while removing characters from text getText() gives empty even if text is not empty. This occurs randomly and not everytime I remove characters. I have observed this happens mostly when 3-4 characters are there in text and I press backspace.

Strange part is that this issue occurs only on device, not on emulator.

Layout file :

<LinearLayout
    style="@style/create_trip_activity_components_style"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="30dp"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <EditText
        android:id="@+id/from_location"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="5dp"
        android:ems="10"
        android:hint="@string/from_location_hint"
        android:inputType="text" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/use_current_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:onClick="useCurrentLocation"
        android:text="@string/use_current"
        android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>

Code :

fromLocation.addTextChangedListener(new TextWatcher() {
    @Override
    public void afterTextChanged(Editable s) {

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before,
                    int count) {

        if (fromLocation == null) {
            Log.d(TAG, "fromLocation is null................");
        }

        Log.d(TAG, "fromLocation text : "
                    + fromLocation.getText().toString());
        Log.d(TAG, "fromLocation text length :  "
                    + fromLocation.getText().length());
        Log.d(TAG, "S : "
                    + s);
        Log.d(TAG, "S length :  "
                    + s.length());
    }
});

Note : I tried using afterTextChanged() and beforeTextChanged() methods. But that did not solve the issue.

like image 910
Geek Avatar asked Aug 01 '13 12:08

Geek


People also ask

Does GetText return string?

GetText(Int32)Return the string value associated with a particular resource ID.

What does GetText return in Android?

GetText returns the text from the single-line text field. It returns only the first line of a multi-line text field.

What does an empty EditText return?

It returns CharSequence whose contents may be empty. CharSequence is an object reference to some class that implements the interface.


1 Answers

I don't see any problem. The text of from_location should change and it does.

Why are you "checking" the EditText (from_location) while you are inside the TextWatcher code. I don't think that the value of the EditText has to be "stable" while is changing.

Maybe what happens is that when you check the CharSequence in from_location is being updated and, sometimes, you catch it just in the middle of a change, sometimes after.

Did you check if the (CharSequence s, int start, int before, int count) are returning the expected values?

As I see this situation. If you want to do any changes on the text you should do them on the String s argument of the afterTextChanged method.

I wrote this code to check what is happening while changing the content of the EditText, maybe it is of any

    mEdtText.addTextChangedListener(new TextWatcher() {
        private static final String TAG = "TextWatcher";
        private static final boolean DEBUG = true;

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            if (DEBUG)
                Log.d(TAG,
                        "(onTextChanged) In \"" + s + "\" " + before
                                + " characters "
                                + " are being replaced at " + start
                                + " by " + count + " ("
                                + s.subSequence(start, start + count) + ")");
            if (DEBUG)
                Log.d(TAG,
                        "(onTextChanged) mEdtText: \"" + mEdtText.getText()
                                + "\"");
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            if (DEBUG)
                Log.d(TAG,
                        "(beforeTextChanged) In \"" + s + "\" " + count
                                + " characters ("
                                + s.subSequence(start, start + count)
                                + ") are about to be replaced at " + start
                                + " by " + after);
            if (DEBUG)
                Log.d(TAG,
                        "(beforeTextChanged) mEdtText: \""
                                + mEdtText.getText() + "\"");

        }
    }

If you check the source code of EditText you can see that inherit the getText method from TextView and it returns the CharSequence mText. You can seed that another CharSequence mTransformed is defined too. And that in the huge setText method there is a moment where mText is replaced by mTransformed. Probably when you call from_location's getText you are getting mText and the second time you do it after setText kicked in and you get mTransformedas answer.

If you want to check this just change

CharSequence cs = from_location.getText();
Log.d(... + cs + ...); // no need to call toString as implicit call.
...
Log.d(... + cs.length() + ...);
like image 176
Asincrono Avatar answered Sep 18 '22 17:09

Asincrono