Reputation: 900
I have few edittext in vertical linear layout. When in focus, and I pressed next button in my keyboard, ideally focus should move to next edittext. But when I register onClickListener to edittext and override onClick method, I have observed that; pressing next key does not take focus to next(or any other) edittext.
Where as when I don't register onClickListener, focus moves when next button is pressed.
Can you explain why is this the case? Is there workaround?
Here is the code :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:weightSum="1"
android:focusableInTouchMode="true">
<ScrollView
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/TblLayout">
<TableRow
android:weightSum="10">
<TextView
style="@style/TvTblLabel"
android:text="@string/str1" />
<android.support.design.widget.TextInputLayout
android:id="@+id/il1"
style="@style/IlStyle">
<EditText
android:id="@+id/et1"
style="@style/EditTextNumber"/>
</android.support.design.widget.TextInputLayout>
</TableRow>
<TableRow
android:weightSum="10">
<TextView
style="@style/TvTblLabel"
android:text="@string/str2" />
<android.support.design.widget.TextInputLayout
android:id="@+id/il2"
style="@style/IlStyle">
<EditText
android:id="@+id/et2"
style="@style/EditTextNumber"/>
</android.support.design.widget.TextInputLayout>
</TableRow>
</TableLayout>
</ScrollView>
</LinearLayout>
Styles :
<style name="TvTblLabel" parent="@android:style/TextAppearance">
<item name="android:layout_gravity">center_vertical</item>
<item name="android:layout_weight">6.5</item>
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">wrap_content</item>
<!--<item name="android:paddingBottom">3dp</item>-->
<item name="android:paddingRight">3dp</item>
<!-- <item name="android:textSize">20sp</item>-->
<!-- <item name="android:padding">3dip</item>-->
</style>
<style name="IlStyle">
<item name="android:layout_weight">3.5</item>
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">wrap_content</item>
</style>
<style name="EditTextNumber" parent="@android:style/TextAppearance.Medium">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:inputType">numberDecimal</item>
<item name="android:selectAllOnFocus">true</item>
<item name="android:gravity">right</item>
</style>
Fragment code :
private void setup(){
et1.addTextChangedListener(new AppInputWatcher(et1));
et1.setOnClickListener(this);
}
...
@Override
public void onClick(View v) {
EditText et = (EditText)v;
et.setSelection(et.getText().toString().length());
}
...
@Override
public void afterTextChanged(Editable s) {
..
et.removeTextChangedListener(this);
et.setText(setEtStr);
et.setSelection(et.getText().toString().length());
et.addTextChangedListener(this);
}
Upvotes: 3
Views: 458
Reputation: 1113
I thinks this is a bug in Android framework. Here is a workaround you can use:
Use setOnEditorActionListener
to listen for keyboard action button events.
Listener implementation can look like this:
@Override
public void onEditorAction(int actionCode) {
// check if "next" button is pressed
if (actionCode == EditorInfo.IME_ACTION_NEXT) {
View view = focusSearch(FOCUS_FORWARD);
view.requestFocus();
}
super.onEditorAction(actionCode);
}
This is what Android usually does when no click listener is set to edit text, but since it does not execute it when listener is set, you can just write it yourself :) I'm not sure if FOCUS_FORWARD or FOCUS_DOWN should be used as focusSearch
method parameter, probably depends on your usecase. Also, you should probably add a null check for a view
before calling requestFocus
on it.
Upvotes: 2
Reputation: 1043
You should add these to your xml file for your EditTexts:
android:nextFocusDown="@+id/.."
android:nextFocusLeft="@+id/.."
android:nextFocusRight="@+id/.."
android:nextFocusUp="@+id/.."
That should do the trick for you.
Upvotes: 0