Quantcast
Viewing all articles
Browse latest Browse all 100

Fixed: Android EditText Lag

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.

<RelativeLayoutxmlns: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 feel good.

<LinearLayoutxmlns: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 LinearLayoutinstead. 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

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!


Viewing all articles
Browse latest Browse all 100

Trending Articles