Lately I encountered this annoying android EditText lag, each time I try to type a character, it makes me wait for around 1 to 3 seconds before I can type the next character! Now that’s horrible. EditText is not useful. Keyboard looks broken.
If you have the same issue, I have a good news for you, I found a fix! Now take a look at the code below.
Old Code
This is the XML layout I used for my customized search bar, and it is very laggy.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/tvSearchBy" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:padding="10dp" android:text="u25BC" /> <TextView android:id="@+id/tvSearchButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_marginRight="1dp" android:layout_marginTop="1dp" android:background="#d1d1d1" android:padding="10dp" android:text="Search" /> <EditText android:id="@+id/autoCompleteTv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_toLeftOf="@+id/tvSearchButton" android:layout_toRightOf="@+id/tvSearchBy" android:hint="Type..." android:padding="10dp" android:singleLine="true" android:text="" /> </RelativeLayout>
New Code
Now here’s a fix. The code below makes everything smooth and feel good.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/tvSearchBy" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:text="u25BC" /> <EditText android:id="@+id/autoCompleteTv" android:layout_width="220dp" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:hint="Type..." android:padding="10dp" android:singleLine="true" android:text="" /> <TextView android:id="@+id/tvSearchButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="1dp" android:layout_marginTop="1dp" android:background="#d1d1d1" android:padding="10dp" android:text="Search" /> </LinearLayout>
The Solution
So what’s the actual solution? Avoid using EditText inside a RelativeLayout, use LinearLayout instead. According to James, If you look at the DDMS, a lot of redraws and recalculations occur while entering the text which is related to the RelativeLayout. So that gives us a clue the the problem is indeed the RelativeLayoutUpdate: I forgot to mention that setting a fixed with of an EditText will help a lot with the performance. It prevents re-calculation and re-drawing of layout. Thanks to Giorgos Kylafas for pointing it out in the comments section below! He also included links that can be useful for you when it comes to Android performance tips so I suggest reading his comment.
Did you have the same experience? If this solution did not work for you and you found your own solution, please share it in the comments section below, I’m willing to include your story and update this post!
12 responses to “Fixing Android EditText Lag”
There is another difference.
In the first case, EditText’s width is “wrap_content”. Everytime you change the text, i.e. EditText’s content, the view needs to re-measure and re-layout [1], which is slow. Being contained insided a RelativeLayout makes things even worse, because RelativeLayout is always multi-pass [2].
In the second case, EditText’s width is fixed to “220 dip”. Its measuring and layout pass is simple and quick. Plus you use no “layout_weight”, so its parent LinearLayout is single-pass [2].
[1] http://developer.android.com/guide/topics/ui/how-android-draws.html
[2] Slide 123 of https://www.dropbox.com/sh/b5eobrkhyt0h1rs/GPgO8zJG10/Devoxx%202013/Performance%20Tips.pdf
Hi @9b17b2ec9c96457e923d67caa582640c:disqus, wow! thanks for sharing your valuable thoughts and including the links, learned something new. I agree that setting a fixed width of an EditText really helps with the performance, I forgot to mention it.
but setting the width to 220dp or sumthing fixed dp for an edittext is not a good practice. isn’t it??.
@mayurgatare:disqus, I think dp is fine, but px is not..
Try to avoid Relative Layout as possible and stick to Linear layout, Relative being 2-Pass is really the cause of many problems.
Romain Guy in the recent I/O conferences even said something about avoiding the relative layout if possible.
Thanks for giving your thoughts @mayurgatare:disqus! We’re learning more, but it is also worth noting that in Android 4.4, RelativeLayout is less expensive. See Romain Guy’s post here https://twitter.com/romainguy/status/398766682279510016
Hi guys,
Is there any other way of improving edit text performance without giving edit text width
Hello @Prem, giving an EditText a width value is your way to go, but if your app works good even without giving an EditText a width, that should be alright too!
Edit AndroidManifest:
My old code
My new code
I just removed the targetSdkVersion and the lag gone away…
Hello @jaksnon:disqus, thanks for the tip, I’m gonna try this! :)
I was using constraint layout, and on some devices the lagging is so high haha, but with linear layout the problem is fixed now, tyyy c:
Thank you…
you saved so much time with this solution