addTextChangedListener and onTextChanged are always called when Android Fragment loads
I got same problem but in adapter. The above solution is working fine for the direct child of the view but in list view adapter I have problem. Finally I found the solution with focus change.
editQty.setTag(false);
editQty.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
editQty.setTag(true);
}
});
editQty.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (!s.toString().equals("") && (Boolean) editQty.getTag()) {
// Do your Logic here
}
}
}
});
Make your EditText
a member of the class. Or, make the View
a member of the class and call findViewById
later.
public class MyFragment extends Fragment {
private EditText notes;
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.detailfragment, container, false);
notes = (EditText)view.findViewById(R.id.stitchnotes);
// your other stuff
return view;
}
@Override
public void onResume() {
super.onResume();
notes.setText("Now I can access my EditText!");
}
...
Small And Simple
Basically in this case when fragment loads onTextChanged character sequence return "" which is not empty string so this will gives you some better idea
myEditText.addTextChangedListener(new TextWatcher() {
@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 (!s.toString().isEmpty()) {
//control come here when user enter some string to edit text
} else {
//When fragment loads control comes here but below condition prevents execution because s.toString() give ""
if (!s.toString().equals("")) {
}
}
}
@Override
public void afterTextChanged(Editable s) {
}
});