I am using TextWatcher
on EditText
.when I input something in edittext
it works perfectly but when I set data into edittext
from database getting NullPointerException
. I used onTouchListener
to on this Edittext
. my question is.
could I check the condition that TextWatcher
works only after touch event is true.
EditText det=(EditText) findViewById(R.id.det);
det.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
System.out.println(v.getId());
d_id=v.getId();
if (event.getAction() == MotionEvent.ACTION_UP) {
getPopUp(det);
}
return false;
}
});
when I call setText on det it goes to TextWatcher
det.setText(ft.get_det().toString());
TextWatcher
@Override
public void afterTextChanged(Editable string) {
try {
int rp=0;
det=(EditText)findViewById(d_id);
s_id=d_id-4;
sev=(EditText)findViewById(s_id);
o_id=d_id-2;
occ=(EditText)findViewById(o_id);
rp=d_id+1;
rpn=(TextView)findViewById(rp);
if (sev.getText().length() > 0
|| occ.getText().length() > 0
|| sev.getText().length() == 0) {
if (string.length() > 0) {
int s, o, d, r;
System.out.println(sev.getId());
System.out.println(det.getId());
s = Integer.parseInt(sev.getText().toString());
o = Integer.parseInt(occ.getText().toString());
d = Integer.parseInt(det.getText().toString());
r = s * o * d;
String m = r + "";
rpn.setText(m);
} else {
// det.setText("");
// rpn.setText("");
System.out.println("In else");
// makeToast();
}
}
} catch (Exception e) {
det.setText("");
rpn.setText("");
System.out.println("IN Exception");
makeToast();
}
}
};
To disable an EditText while keeping this properties, just use UI. setReadOnly(myEditText, true) from this library. If you want to replicate this behaviour without the library, check out the source code for this small method.
android:editable="false" should work, but it is deprecated, you should be using android:inputType="none" instead. Alternatively, if you want to do it in the code you could do this : EditText mEdit = (EditText) findViewById(R. id.
The most reliable way to do this is using UI. setReadOnly(myEditText, true) from this library. There are a few properties that have to be set, which you can check out in the source code.
We can set editable property of EditText in XML layout but not programatically, but there is no setEditable() method! If EditText is not Enabled [ by setEnabled(false) ] it still Editable!
Create a TextWatcher like below
TextWatcher textWatcher=new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// 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 afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
};
and then to add this to your EditText call
EditText.addTextChangedListener(textWatcher);
and to remove call
EditText.removeTextChangedListener(textWatcher);
i solved this by taking a global boolen variable.Set this variable true in onTouchListener and check condition in afterTextChanged method that if varible is true of false.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With