-1

I am making a text editor in Android and I want to add syntax highlighting to it. With my current implementation, the UI is laggy when I type something. I need help with optimizing my current implementation.

private void onEditorListener() {

    edtEditor.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

            if (timer != null) {
                timer.cancel();
            }


        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        }

        @Override
        public void afterTextChanged(final Editable s) {

            timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    // do your actual work here
                    MaineActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {

                        }
                    });

                    try {

                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    MaineActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            textHighLighter(s);
                        }
                    });
                }
            }, 600);


        }

    });
}
private void textHighLighter(Editable s) {

    // Check the matcher for general keywords..
    Matcher a = Patterns.GENERAL_KEYWORDS.matcher(s.toString());
    // Check the matcher for html tags..
    Matcher b = Patterns.HTML_TAGS.matcher(s.toString());
    // Check the matcher for html attribute..
    Matcher c = Patterns.HTML_ATTRS.matcher(s.toString());
    // Check the matcher for symbol..
    Matcher d = Patterns.SYMBOLS.matcher(s.toString());
    // Check the matcher for general strings..
    Matcher e = Patterns.GENERAL_STRINGS.matcher(s.toString());
    // Find all the general key words and change the text color...
    // Find all the html tags words and change the text color...
    while (b.find()) {
        s.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.orchid)), b.start(), b.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        //android.util.Log.i("VINCE", b.toString());
    }

    while (a.find()) {
        s.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.bounded)), a.start(), a.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        //android.util.Log.i("VINCE", b.toString());
    }

    while (c.find()) {
        s.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.henn)), c.start(), c.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        //android.util.Log.i("VINCE", b.toString());
    }

    while (d.find()) {
        s.setSpan(new ForegroundColorSpan(Color.WHITE), d.start(), d.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        //android.util.Log.i("VINCE", b.toString());
    }

    while (e.find()) {
        s.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.general_str)), e.start(), e.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        //android.util.Log.i("VINCE", b.toString());
    }

}
randers
  • 5,031
  • 5
  • 37
  • 64
  • 3
    `Thread.sleep(2000);` no wonder it lags... – njzk2 May 05 '16 at 17:15
  • An actual answer might be too long for this format; in short you are trying to a background task the wrong way, you are starting a new timer task after every character is typed, and a proper highlighting function will distinguish between changed and unchanged test so it only needs to re-highlight changed text. – antlersoft May 05 '16 at 17:25
  • I put an thread because i want to pause immediately the effect of syntax highlighting then when it comes to more characters it edit text it becomes a loggy How can i optimize my edittext sir ? – Vincent Llauderes May 05 '16 at 17:27
  • any idea or format sir how can i optimize my edittext ? – Vincent Llauderes May 05 '16 at 17:29
  • sir what is your actual answer ? – Vincent Llauderes May 05 '16 at 17:38

2 Answers2

0

Here are some suggestions:

Use a Handler and a Runnable instead of TimerTask. I'm assuming you want to wait n milliseconds before calling textHighLighter(Editable s) after afterTextChanged(final Editable s) is invoked. To do this, use handler.postDelayed(Runnable r, long delay) and handler.removeCallbacks(Runnable).

Remove Thread.sleep(2000).

An Editable extends CharSequence. Do no call toString() on your Editable, just pass it as an argument to Pattern#matcher(CharSequence).

Jared Rummler
  • 37,824
  • 19
  • 133
  • 148
0
@Jared Rummler here is my code

@Overrideenter code here
            public void afterTextChanged(final Editable s) {
                last_text_edit = System.currentTimeMillis();
                handler.postDelayed(runnable, idle_min);
                runnable = new Runnable() {
                    @Override
                    public void run() {
                        try {
                            if (System.currentTimeMillis() >= (last_text_edit + idle_min - 500)) {
                                // user hasn't changed the EditText for longer than
                                // the min delay (with half second buffer window)

                                textHighLighter(s);  // your queries
                                if (!already_queried) { // don't do this stuff twice.
                                    already_queried = true;
                                    //textHighLighter(s);  // your queries
                                    android.util.Log.i("VINCE", "already_queried");
                                }
                            }

                            //already_queried = false;
                            handler.removeCallbacks(runnable);
                            android.util.Log.i("VINCE", "Cancelled");
                        }
                        catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                };