Virtual keyboard does not open when dynamically inserting an EditText view into a Listview


I'm struggeling with an issue that really drives my crazy. I found comparable issues in the forums, but they all are not quite the same like this one. So I hope that someone has a brilliant idea how I could solve this. Or tell me what I'm doing wrong. ;-)

The setup:

I have a ListView. The following XML code represents the child elements:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android = ""
    android:layout_width    = "match_parent"
    android:layout_height   = "wrap_content"
    android:id              = "@+id/container">

    <EditText android:id            = "@+id/child"
              android:layout_width  = "300dp"
              android:layout_height = "wrap_content" />


it is a LinearLayout that in turn has an EditText view inside. The following code adds one child to the list. Since the EditText view is smaller than the LinearLayout where it is embedded, for testing I've attached a click listener to the empty space of this (first) child's LinearLayout. When clicking on this child a second child is inserted into the ListView:

public class Keyboard_Bug extends ListActivity
    static BugAdapter mAdapter;
    static String [] mNameArray = new String [2];
    static int mCount; 

    public void onCreate(Bundle savedInstanceState) {

        mNameArray[0] = "Entry 1";
        mCount = 1;
        mAdapter = new BugAdapter(this);

    public static class BugAdapter extends BaseAdapter
        final LayoutInflater mInflater;
        EditText mView;

        public BugAdapter(Context context)     { mInflater = LayoutInflater.from ( context );   }
        public int  getCount  ()               { return mCount  ; }
        public long getItemId ( int position ) { return position; }
        public Object getItem ( int position ) { return mNameArray[position]; }

        public View getView( int position, View convertView, ViewGroup parent )
            if ( convertView == null )
                convertView = mInflater.inflate(R.layout.child, parent, false);

            mView = (EditText) convertView.findViewById(;

            // Get focus 1
//          if ( mNameArray[position].equals("Entry 1") )
//              mView.requestFocus();

            // Get focus 2
            if ( mNameArray[position].equals("Entry 2") )

            LinearLayout ll = (LinearLayout) convertView.findViewById(;

            ll.setOnClickListener(new View.OnClickListener()     {
                public void onClick(View v) {
                    mNameArray[mCount] = "Entry 2";

            return  convertView;

When I use the (commented out) code section with the comment "Get focus 1", everything works perfect. "Entry 1" gets the focus and the keyboard pops up.

The issue:

When the onClick handler now inserts the second child and I use the code section with the comment "Get focus 2", the second child's EditText view obtains the focus (that's fine), but the keyboard does not open. I can also click on the newly created EditText, and even though the cursor is blinking I cannot open the keyboard. The only way is to select the first EditText and then select the second EditText again. Then the keyboard opens.

I tried already:

InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);

as posted in different forums, but it didn't work. The only thing that worked was:

mgr.toggleSoftInput ( 0, 0 );

But this of course is not the right approach, since in case the keyboard is already open, it would then be closed.

I would greatly apreciate any suggestions! Thanks!


Use this code on EditText Click event :

InputMethodManager m = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
  if(m != null){
      m.toggleSoftInput(0, InputMethodManager.SHOW_IMPLICIT);