Android - EditText input is slow
I have an EditText that SLOW is responsible for entering text. This lag is annoying enough to make me find a solution. I did some research and found the SO thread EditText lagging when typing and it suggests moving the code to its own thread. I did it, but I am still experiencing lag.
EDIT
After looking at the comments below (thanks to dreamtale), I know that a new thread is not needed. But returning code on the onTextChanged or afterTextChanged event still triggers a slow response. I changed 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 snippet:
@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;
}
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 still lags behind when typing. If I remove the event addTextChangeListener
then it works fine. Ideas?
source to share
I had a similar problem using EditText
inside ListView
, which was fixed by changing EditText width
to 0dp
using weighted width to match / fill the parent.
I don't know exactly why this happened, but I believe it is because when the width is EditText
set to the content wrapper, it will adjust / redraw itself to fit everything, and ListView
also try to redraw itself to fit. Thus, if the value EditText
has a fixed width, this redrawing is no longer necessary.
source to share
When you enter text, it onTextChanged
is called over and over, so you start a new thread ( EditTextWatcherTask
) over and over again . It will consume a lot of system resources.
Also the task takes a long time to complete, so in your situation you don't need a thread, delete the task, just put the computation code in onTextChanged
.
source to share