2

I have a dialog with dynamic content, so I've created a listview inside this dialog and then an Adapter which decides what layout each item has,

Adapter chooses from 3 layouts,

  • one contains checkbox - works fine
  • second contains spinner - works fine
  • third contains edittext - SW keyboard will never display

Here is the edittext item layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:minHeight="?android:listPreferredItemHeight"
              android:paddingStart="24dp"
              android:paddingEnd="24dp"
>

    <TextView
            android:id="@+id/name"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:text="Test"
            android:gravity="center_vertical"
            android:layout_gravity="center_vertical"
    />

    <EditText

            android:id="@+id/value"
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:layout_gravity="center_vertical"
            android:focusable="true"
    />

</LinearLayout>

and the adapter choosing a layout:

class FieldAdapter extends ArrayAdapter<ProfileField> {
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ProfileField field = getItem(position);
        switch (field.type) {
            case ProfileFieldType.CUSTOM_TEXT:
                convertView = inflater.inflate(R.layout.scanprofile_dialog_edit_item, parent, false);
                break;
            case ProfileFieldType.CUSTOM_CHECK:
                convertView = inflater.inflate(R.layout.scanprofile_dialog_checkbox_item, parent, false);
                break;
            default:
                ProfileField.ListProfileField listField = (ProfileField.ListProfileField) field;
                convertView = inflater.inflate(R.layout.scanprofile_dialog_item, parent, false);
                break;
        }
        return convertView;
    }
    ...
}

I've tried adding this to onCreateDialog

final AlertDialog alertDialog = builder.create();
final Window dialogWindow = alertDialog.getWindow();
dialogWindow.clearFlags(
        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
dialogWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
return alertDialog;

But it didn't help at all.

Any ideas why the SW keyboard is not shown?

Mazmart
  • 2,703
  • 4
  • 17
  • 20

3 Answers3

0

EditText View inside ListView is not supported in android as both need focus to work properly. In Your case all the event are consume by ListView that's why keyboard is not opening when you are trying to clicking on EditText view. You can give focus to EditTextView but it will create problem in ListView functionality.

You can either put EditText in header/footer of ListView or if you have fix number of view like only 3 then directly plot those view on dialog view instead of inflating them inside ListView.

Refer the here if you still want to use EditText inside ListView.

Community
  • 1
  • 1
VikasGoyal
  • 3,308
  • 1
  • 22
  • 42
0

add in xml

<EditText...>
<requestFocus />

add in code

  boolean checkFocus=youredittext.requestFocus();
Log.i("CheckFocus", ""+checkFocus);
if(checkFocus==true)
{
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(youredittext, InputMethodManager.SHOW_IMPLICIT);
}
0

you can try this code (help you proactively do it) any time you want)

//edittext demo code

final EditText myEditText = (EditText)findViewById(R.id.editText);
    myEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            myEditText.postDelayed(new Runnable() {
                @Override
                public void run() {

                    InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    keyboard.showSoftInput(myEditText, 0);
                }
            }, 50); //require system show soft keyboard after 50 miliseconds
        }
    });
GiapLee
  • 436
  • 2
  • 8