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.