BennyKok
BennyKok

Reputation: 809

Android EditText setText cause softkeyboard to freeze

I am having a problem with the EditText.

I have implemented a TextWatcher and I check every time in afterTextChanged to highlight some specific keywords in an AsyncTask and set the text in onPostExcute(I only touch the UI here), but the soft keyboard freeze when setText was called in onPostExcute(the app didn't freeze).

public class AsyncHighLight extends AsyncTask<String,String,String>
{
    @Override
    protected String doInBackground(String[] p1){
        return SyntaxHighlighter.getInstance(MainActivity.this).highlight(p1[0]);
    }

    @Override
    protected void onPostExecute(String result){
        et.setText(Html.fromHtml(result));
    }
}

The highlight code here

public String highlight(String s){
    String newString = s;
    newString = newString.replace("\n","<br>");
    for (int i = 0 ; i < currentLang.keyword.length ; i ++){
        newString = newString.replace(currentLang.keyword[i],warpColorTag(currentLang.keyword[i]));
    }
    return newString;
}

Upvotes: 1

Views: 1122

Answers (2)

Satpal Yadav
Satpal Yadav

Reputation: 81

This is due to tha reason that as soon as you settext in onpostexecute, textchange event fires again and it goes into async task again thus it goes into a infinite loop. You should use a boolean to trace whether the event is genrated from onpostexecute or by keyboard input

Upvotes: 1

Praween Kumar Mishra
Praween Kumar Mishra

Reputation: 833

You have to write the logic of terminate the afterTextChange() method of TextWatcher as whenever the text got change afterTextChange() will gets called and every time afterTextChange() gets called you highlight some specific keywords in an AsyncTask and again in onPostExecute() will gets called and it will setText() again. SO you have to find the way of terminating the afterTextChange() logic. For better help please post TextWatcher code too.

Upvotes: 3

Related Questions