I am working on an Android application. In my app I am facing a strange problem.Please have a look on my screen.
when i select any thing from selction box then it will add twice to the edittext.
the following is my code for edittext
snd_txt.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
System.out.println("onTextChanged"+start+"b4"+before+"count"+count);
if (start>0) {
int m=snd_txt.getText().toString().lastIndexOf(".");
makeColorText(m,snd_txt.getText().toString().length());
}
}
@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
}
});
public void makeColorText(int num,int last) {
int start=last-44;
if (start<1){
start=0;
}
SpannableString ss = new SpannableString(snd_txt.getText());
ss.setSpan(new ForegroundColorSpan(R.color.Gray_Light), 0, start,0);
ss.setSpan(new ForegroundColorSpan(Color.BLACK), start, last, 0);
// customize ss here
// ...
snd_txt.setText(ss);
snd_txt.setSelection(last);
}
If i comment the snd_txt.setText(ss);
statement .the the edittext will work perfectly. But I can't do that.I used that method to change edittext color at runtime. working fine for all the time..But at the time of selecting predictions will add the word two times.
Anybody have any idea to solve, Please help me.
Edit: How can I create a custom edittext with multicolor text.. for eg: current line in blue and previous are black.something like that
You are doing a think which can
causes a Exception due to recycling
. You are watching for text change event of edittext and if there is a text change then you are again setting a text on the EditText so again your text change event will be occurred and you will again set the text so it will continue and it will probably give you Stack overflow exception or hang your UI.You should use
beforeTextChanged
event and modify theCharSequence
which you are getting in argument. Probably this will work for you.Solved my problem by changing my code.