7

I've got an EditText that is SLOW to respond when typing. The lag is annoying enough to cause me to find a solution. I did some research and found an SO thread EditText lagging when typing text and it suggests to move the code to it's own thread. I did that but I'm still experiencing the lag.

EDIT

After seeing the comments below (thanks dreamtale), I know the new thread isn't necessary. But putting the code back in the onTextChanged or afterTextChanged event still causes the slow response. I've modified the code to reflect the latest changes:

xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_weight="1" >

    <LinearLayout android:orientation="horizontal" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="2dp"
        android:layout_marginRight="2dp" > 
        <include 
            android:layout_width="match_parent" 
            android:layout_height="match_parent" 
            layout="@layout/right_layout_header" />             
    </LinearLayout>

<ScrollView 
   android:layout_width="match_parent" 
   android:layout_height="0dp" 
   android:layout_weight="1"
   android:layout_marginRight="5dp"
   android:layout_marginBottom="5dp">

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:shrinkColumns="1"
        android:drawable="@drawable/light_bg" >

        <TableRow
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:shrinkColumns="1">
                <TextView
                    android:id="@+id/tvSummaryBold"
                    android:layout_width="wrap_content"
                    android:textStyle="bold"
                    android:gravity="left"
                    android:text="@string/Summary"
                    style="@style/IssueDetailsLabelTextView" />
        </TableRow>
        <TableRow
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:shrinkColumns="1">

            <EditText
                    android:id="@+id/txtSummary" 
                    android:layout_width="match_parent" 
                    android:layout_height="match_parent" 
                    android:hint="@string/SummaryDefaultText" 
                    android:maxLength="255"
                    android:singleLine="true"
                    android:layout_weight="1"

                />
        </TableRow>
        <TableRow>
                <TextView 
                    android:id="@+id/tvCharactersRemaining"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="@color/Gray"
                    android:paddingLeft="5dp"
                    />
        </TableRow>
    </TableLayout>
  </ScrollView>
</LinearLayout>

Here's the code for the fragment:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    try
    {
        vView = inflater.inflate(R.layout.submit_issue, container, false);

        //Setup initial "characters remaining" text.
        mCharsRemaining = (TextView)vView.findViewById(R.id.tvCharactersRemaining);
        mCharsRemaining.setText(String.valueOf(iMaxChars) + ' ' + getString(R.string.CharsRemaining));

        //Event for counting the characters remaining.
        mSummaryEditText.addTextChangedListener(TextEditorWatcher);

        new LoadPOCsTask().execute();   
    }
    catch (Exception e)
    {
        Errors.LogError(e);
    }
    return vView;           
}

The TextEditorWatcher event:

private final TextWatcher TextEditorWatcher = new TextWatcher() { 
    public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

    } 

    public void onTextChanged(CharSequence s, int start, int before, int count) { 

        int iMaxCharsRemaining = (iMaxChars - s.length());
        mCharsRemaining.setText("Static Text "); //Intentionally put static text to see if problem still occurs, and yes it does.
    } 

    public void afterTextChanged(Editable s) { 
    } 
}; 

I can't figure out why it's still lagging when typing. If I remove the addTextChangeListener event, then it works fine. Ideas?

Community
  • 1
  • 1
Robert
  • 1,696
  • 3
  • 36
  • 70
  • EditTextWatcherTask task = new EditTextWatcherTask(s); task.execute(); `call in afterTextChanged...` – Samir Mangroliya Mar 27 '12 at 14:49
  • Still getting the same lag... – Robert Mar 27 '12 at 14:55
  • I don't think your task is helping much, the only thing you're moving to the background is the substraction. As for the actual lag, have you tried pinpointing the exact thing that is making it lag? Like, try to do `mCharsRemaining.setText("static text" + mMaxCharsRemaining)` to see if the resource fetching is what's hurting the performance. – dmon Mar 27 '12 at 15:01
  • If I put static text in the onTextChanged event (either "Static" or "static" + String.valueof(mMaxCharsRemaining) ), the text typing is still very slow. If I don't hook up the EditText to the listener, the text typing is fine. – Robert Mar 27 '12 at 15:43

5 Answers5

11

I was having a similar issue using EditText inside a ListView, that was fixed by changing the EditText width to 0dp using weighted widths to match/fill the parent.

I don't know for sure why this was occurring, however I believe it is because when the width of the EditText is set to wrap content it will adjust/redraw itself so that everything fits, and the ListView will also attempt to redraw itself so everything fits. So by making the EditText have a fixed width, this redraw is no longer required.

Sam Shute
  • 644
  • 1
  • 6
  • 13
3

I found that switching off the predictive text helped.

android:inputType="text|textNoSuggestions"
Zee
  • 1,592
  • 12
  • 26
2

Edit AndroidManifest:

My old code

<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" />

My new code

<uses-sdk android:minSdkVersion="8" />

I just removed the targetSdkVersion and the lag gone away...

udondan
  • 57,263
  • 20
  • 190
  • 175
jaksnon
  • 21
  • 1
0

I struggled for a while, I started removing attributes in the xml, finally got the answer. In my case, I removed these lines:

        android:ellipsize="end"
        android:textAllCaps="true"
0

When you typing text, the onTextChanged is called again and again, so you are starting an new thread (EditTextWatcherTask) again and again. It will consume many resource of the system.

As well thread is for the task that need many time to finish it, so in your situation, you don't need a thread, remove the task, just put the computation code in the onTextChanged.

dreamtale
  • 2,905
  • 1
  • 26
  • 38
  • Thanks for the response...but I had the code there originally and it was very slow to respond. – Robert Mar 27 '12 at 15:39
  • What means originally? Anyway you should not use `AsyncTask` at there.Remove the task, then response will be as normal. – dreamtale Mar 27 '12 at 15:41
  • When I first created the code, it was in the onTextChanged event (even tried the afterTextChanged event) and the response was still slow. I did remove the Async Task because of what you said, which makes sense. I was only going off that idea because of the other post (see link in thread) and it seemed to fix their issue with lagging text. – Robert Mar 27 '12 at 15:45
  • @Robert I have seen the post you noticed, maybe his UI is complicated or have something to do that need some time. So put the Time-consuming work in the thread and then post to the UI is ok. But in your situation, you do not need a thread to just compute a subtraction. Even if you need a thread, **you don't need to new Task every time once a word is typing** :( – dreamtale Mar 27 '12 at 16:04
  • I understand that from your original answer. I will edit the post to reflect that and hopefully I can find a resolution. – Robert Mar 27 '12 at 16:12
  • @Robert Good luck and sorry for my repeat explanations. – dreamtale Mar 27 '12 at 16:22
  • no worries....I wouldn't have seen it that way if you didn't explain it. Much appreciated! – Robert Mar 27 '12 at 16:59