23

I have a few edittext within a listview. i have a generic focuslistener on the edittext that updates the value of the data model and also the background of the edittext when focus is lost by calling notifydatachanged . The problem is that if one of the edittext is holding focus, when i touch the next edittext, it gains focus momentarily then loses focus. I suspect it is due to the notifydatachanged method call that is causing all views to be redrawn, after which the focus is lost. Does anyone have a suggestion or work around on the issue? Thanks.

5 Answers 5

22

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.

5
  • Elegant and effective solution. Thanks! :)
    – g t
    Mar 2, 2012 at 11:11
  • Hi all and @Monkeyless, having adopted the above solution, I have encountered another problem. My list view also contains a few spinners,but I observed that if the spinner are touched after any edittext had been focused before, the spinner will not set the selection even though the selection dialog is shown. As I suspect it is due to the focus still set to the last edittext, I have tried to clear the currentlyfocusedrow variable once any edittext has requestfocus. This has not rectified the problem though. Any insight on this?
    – g t
    Mar 5, 2012 at 15:47
  • 2
    I have the same problem and understand the solution but cannot get this working - relative newbie to Android though so any pointers / code would be a great help. Any chance of sticking some code up - especially the onFocusedChanged listener. Thanks in advance.
    – Kern
    Jul 12, 2012 at 5:02
  • 1
    This solution can't work: when Android changes focus without touch (scrolling the list), the currentlyFocusedRow will be set to the new row, loosing the actual information.
    – 3c71
    Apr 30, 2013 at 6:10
  • Nice solution. Additionally I used a switch use_last_focus to request focus only when the notifyDataSetChanged is send. I'm going to add an example as a separate answer, because it doesn't fit as a comment.
    – tsig
    Apr 12, 2017 at 9:02
18

Set

android:windowSoftInputMode="adjustPan"

for your activity in the AndroidManifest.xml

3
  • 9
    This works regarding the focus, but if the list is longer than the screen and you start typing, the EditText field will move out of the visible area of the user, so he doesn't see what he's actually typing once he starts entering the first character. (Android 4.1) Apr 14, 2013 at 19:42
  • 2
    tihs worked for me but I have no idea why or what the problem was :/ Jul 5, 2013 at 10:28
  • @PadmaKumar I have editText in the header of the recyclerview. When I call notifydatastatechanged, it lost the focus from that edit text. So I need to again click on that edit text to have the focus on that and type. Please let me know the solution for this. Sep 8, 2016 at 3:31
6

I also had a problem of EditText loosing the focus, but calling setDescendantFocusability(ListView.FOCUS_AFTER_DESCENDANTS) solved it.

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    final ListView lv = getListView();
    lv.setDescendantFocusability(ListView.FOCUS_AFTER_DESCENDANTS);
}
1
  • 6
    This only works for one EditText in the list, but if each list item has such EditText element, and I'm clicking the EditText in the second row, the focus - after the softkeyboard is shown - will be in the EditText of the first list row, but it should be in the second. (Android 4.1) Apr 14, 2013 at 19:43
2

For future users, everything here is overcomplicated. The correct answer is to add

android:descendantFocusability="afterDescendants"

to your listview.

0

Example of implementing Sam Judd's suggestion with an additional switch use_last_focus

public class my_friends_adapter extends ArrayAdapter<group_user> {
    boolean use_last_focus=false;
    int currentlyFocusedRow=-1;
    String currentlyFocusedField="";

    public my_friends_adapter(Context context, int resource, ArrayList<group_user> users) {
            super(context, resource, users);
              }
        EditText tvName,tvEmail;

    @Override
   public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) convertView = LayoutInflater.from(getContext()).inflate(R.layout.my_friends_item, parent, false);
        tvName = (EditText) convertView.findViewById(R.id.tvName);
        tvEmail = (EditText) convertView.findViewById(R.id.tvEmail);
        if (use_last_focus && currentlyFocusedRow ==position){
            if (currentlyFocusedField=="tvName")tvName.requestFocus();
            else tvEmail.requestFocus();
            use_last_focus=false;
        }
        tvName.setTag(position);//When focus is lost save the entered value for later use
        tvName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {save_data2( v,  hasFocus);
            }
        });

        tvEmail.setTag(position);//When focus is lost save the entered value for later use
        tvEmail.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {save_data2( v,  hasFocus);}
        });

        return convertView;
        }


    public  void save_data2(View v, boolean hasFocus){
        int position = (Integer) v.getTag();
        EditText tvName1 = (EditText) v.findViewById(R.id.tvName);
        EditText tvEmail1 = (EditText) v.findViewById(R.id.tvEmail);
        boolean data_changed=false;
        if (hasFocus) currentlyFocusedRow=position;
        if (!(tvName1==null)) {
            if (hasFocus) currentlyFocusedField="tvName";
        }
        if (!(tvEmail1==null)) {
            if (hasFocus) currentlyFocusedField="tvEmail";
        }
        // set when data_changed
        // ..........
        if (!hasFocus && data_changed) {
            //your code to save the changed data
            // ...............
            //
            my_friends.adapter.notifyDataSetChanged();
            use_last_focus=true;
        }

    }



}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.