EditText in Listview loses focus when pressed on Android 4.x
A classic hack for situations like this is to use a handler and postDelayed()
. In your adapter:
private int lastFocussedPosition = -1;
private Handler handler = new Handler();
public View getView(final int position, View convertView, ViewGroup parent) {
// ...
edittext.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (lastFocussedPosition == -1 || lastFocussedPosition == position) {
lastFocussedPosition = position;
edittext.requestFocus();
}
}
}, 200);
} else {
lastFocussedPosition = -1;
}
}
});
return convertView;
}
This works on my device, but keep this code out of production. I also wouldn't be surprised if the focus bug manifests itself differently in different android versions or roms.
There are also many other problems with embedding an EditText
within a ListView
that have solutions that feel like a hack. See all of the other people struggling.
It's also very easy to have something like this happen:
.
After having gone down similar paths many times myself, I've mostly given up on trying to override any of the default keyboard behaviours or quirks. I would recommend trying to find alternative solution in your app if possible.
Have you considered having the ListView
rows be just a styled TextView
and then displaying a Dialog
with an EditText
when a row is clicked, updating the TextView as necessary?
Edit Text in ListActivity ListView loses focus when keyboard comes up
All you really need to do is apply this to your ListView:
XML:
android:descendantFocusability="afterDescendants"
Java:
listView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
Issues focusing EditTexts in a ListView (Android)
You are facing ListView
recycling issue. When you scroll up or down or when keyboard appears ListView
again refreshes and lost your EditText's
focus. So, first of all read this answer to understand ListView
Recycling mechanism and then follow the suggestion from my side the solution of your current scenario in my mind.
I suggest you should use a Button
on ListView
Item and text of this button should be generic like Enter Student Info or what ever you'd like. After clicking on this Button
open AlertDialog
and set your xml view (currently your all edittexts
like et_gpa, et_min, et_max etc on listview
items)
For Example:
btnInfo.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showStudentInfoAlert();
}
});
public void showStudentInfoAlert()
{
AlertDialog.Builder builder = new AlertDialog.Builder(context);
LayoutInflater in = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = in.inflate(R.layout.your_xml_having_edittexts, null);
EditText et_gpa = (EditText) v.findViewById(R.id.et_gpa);
//add all edit texts like this and after that just set view on alert dialog
builder.setTitle("Enter student's info");
builder.setView(v);
builder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//save all edittexts values and do what erver you want with those values
}
});
dialog.show();
}
Android edittext in listview loses focus on calling notifydatachanged
It is indeed happening because all the views are redrawn, so the edit text representing whatever row used to be focused is now a completely different object. Set a variable in your adapter: int currentlyFocusedRow;
in getView for your adapter: Add an onFocusChanged listener to each edit text and when that edit text gains focus, set currentlyFocusedRow = whatever row the focused edit text happens to be in. Also set any edit text that is in the currentlyFocusedRow to be focused.
Android ListView with EditText loses focus when the keyboard displays. Keyboards appear randomly
I am not sure if you have xml for this view or you are generating these edittext programatically if you have xml then for each edit text do android:inputType="number" if you are generating these editext programatically then do this for each edittext.setInputType(InputType.TYPE_NUMBER_FLAG_SIGNED | InputType.TYPE_CLASS_NUMBER); with this only number key willl be shown for user input next just add addTextChangedListener to each editText
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//handle your changes here
}
@Override
public void afterTextChanged(Editable s) {
}
});
Update
Android ListView with EditText loses focus when the keyboard displays The issue is when you have a ListView with input capable fields that displays the soft keyboard on focus, the EditText loses its focus for the first time but the second time it just works fine. The reason it happens is all the views are getting rendered again, so the EditText field object in the item row that used to be focused is now a completely different object. android:descendantFocusability="beforeDescendants" in your ListView and android:windowSoftInputMode="adjustPan" for your activity in the app Manifest
<ListView android:id="@+id/productList" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_alignLeft="@+id/search"
android:layout_below="@+id/search"
android:padding="5dp"
android:descendantFocusability="beforeDescendants"/>
<activity android:name=".myActivity"
android:label="@string/app_name" android:screenOrientation="sensorPortrait" android:windowSoftInputMode="adjustPan"/>
Reference https://www.mysamplecode.com/2013/02/android-edittext-listview-loses-focus.html
Numeric edittext loses focus after first input in listview
I managed to fix it, after debugging the currentFocus i found out that the listview onExpand() took the focus away, setting editText.requestfocus() after returned the focus to the edittext.
Related Topics
Prevent Ussd Dialog and Read Ussd Response
Difference Between a Clickable Imageview and Imagebutton
Android:What Is Difference Between Setflags and Addflags for Intent
Difference of Setvalue() & Postvalue() in Mutablelivedata
Android Location Manager, Get Gps Location ,If No Gps Then Get to Network Provider Location
Android: Integer from Xml Resource
How to Encrypt/Decrypt String in Android
Caused By: Java.Lang.Outofmemoryerror: Bitmap Size Exceeds Vm Budget
Android Alert Dialog with One, Two, and Three Buttons
Android Accelerometer Not Working When Screen Is Turned Off
How to Correctly Use Google Plus Sign in with Multiple Activities
Interstitial Admob Ads: "Illegalstateexception: Only Fullscreen Activities Can Request Orientation"
How to Notify Users About an Android App Update
Endless Recyclerview with Progressbar for Pagination
How to Build a Native (Command Line) Executable to Run on Android