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.
This is the XML layout I used for my customized search bar, and it is very laggy.
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 RelativeLayout
Update: 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!
For FREE programming tutorials, click the red button below and subscribe! :)
website