6

I'm trying to create a DialogFragment that contains a RecyclerView of EditTexts. It scrolls and the Copy/Cut/Paste appears when I click on an EditText but the keyboard never appears. The adapter works since I tried implementing the RecyclerView in an Activity.

I already tried finding solutions for making the keyboard show up such as adding this in the XML

</request focus>

or this to the dialog

dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

but still, none works.

Thank you so much.

additional: here's how it currently appears

enter image description here

Cù Đức Hiếu
  • 5,569
  • 4
  • 27
  • 35
Yeol
  • 119
  • 2
  • 14

6 Answers6

13

I know this question was asked one year ago, but I asked this very same question today, so it seems that this question is still puzzling people...

Anyways, I came across multiple "solutions" after browsing Stack Overflow and the Android API reference, but only one that would work. Now I'm not sure why these other, seemingly good solutions, wouldn't work for me. But at least I think I understand (or I have a theory) why the solution that finally worked, worked.

NOTE! This solution may not work if you are using some other way to create your dialog in onCreateDialog(Bundle) than by utilizing AlertDialog and its AlertDialog.Builder.

Here is the solution that worked for me.

By doing as above solution suggests, you are "tricking" the AlertDialog into setting the right flags for you. I'm not saying it's bad code practice, but it may not be the ideal code practice. For those of you not just looking for a quick solution but prepared to dive deep into this matter, continue reading:

So according to AlertDialog's documentation, AlertDialog automatically sets some window placement -related flags based on if any views in the dialog return true from View.onCheckIsTextEditor(). This behaviour of AlertLayout is brought up by this solution that seems to have helped a lot of people asking the same question but without a RecyclerView involved and with only AlertDialog involved, not AlertDialog used by a DialogFragment subclass.

You might get the latter solution working (check out its most up-voted comment), but your DialogFragment subclass must provide a way, after the DialogFragment has been shown, to get the AlertDialog's window, so that you can modify its flags. I haven't tried this, as my DialogFragment doesn't provide that functionality.

Daniel Giljam
  • 131
  • 1
  • 5
7

I had the same problem when using a RecyclerView.Adapter, in a view that is created for a class that extends a DialogFragment. Also, in my ViewHolder I use an EditText that was not showing the keyboard when it gets focus.

@Override
public Dialog onCreateDialog(final Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(requireContext());
    View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_text_detected_list, null);
    //{...setAdapter, and other setups...}
    AlertDialog dialog = builder.setView(view).create();

    //The below line, solves my problem with the keyboard not showing up
    dialog.setOnShowListener(d -> dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM));
    return dialog;
}
Felipe Ferreira
  • 131
  • 2
  • 5
6

a) Force the input method open.

InputMethodManager inputMananger = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputMananger.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

b) Request focus of the EditText that you wish to gain focus.

editText.requestFocusFromTouch();
Ryan
  • 3,414
  • 2
  • 27
  • 34
  • 3
    hello! method a does make the keyboard appear. however, it shows below the dialog fragment. how do i fix this problem? – Yeol Apr 09 '16 at 04:31
  • Are you toggling the soft input from within the `DialogFragment` or the `Activity`? – Ryan Apr 09 '16 at 04:39
  • I'm toggling it from the DialogFragment – Yeol Apr 09 '16 at 05:54
  • Okay, try setting `getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);` from within your `DialogFragment`'s `onCreateView()` method. – Ryan Apr 09 '16 at 13:49
  • @Ryan - Where should I use your first method, is it inside edittext click event? – R15 Oct 24 '18 at 07:17
  • @Arvindraja I would call it wherever/whenever you need to show it. You could do it on initial load of the view (some `onCreate...()` or `onStart()` lifecycle method), or you could call it from the click event of the `EditText`. – Ryan Oct 25 '18 at 14:19
1

This worked for me :

public AlertDialog onCreateDialog(Bundle savedInstanceState) {
    super.onCreateDialog(savedInstanceState);

    // ...
    alertDialog.show();

    // This line is the key
    alertDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);

    return alertDialog;
}
Greelings
  • 4,964
  • 7
  • 34
  • 70
0

Try using :

InputMethodManager imm1 = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        edt_user_name.postDelayed(new Runnable()
        {
            @Override
            public void run()
            {
                edt_user_name.requestFocus();
                imm1.showSoftInput(edt_user_name, 0);
            }
        }, 100);

For a particular EditText e.g edt_user_name.

Janki Gadhiya
  • 4,492
  • 2
  • 29
  • 59
  • Try adding `android:windowSoftInputMode="adjustPan"` to your AndroidManifest.xml. and Remove `` and `dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);` If it is there – Janki Gadhiya Apr 09 '16 at 04:38
0

In my case, I've solved this by adding an EditText inside the fragment layout (not just RecyclerView)like this :

<android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <EditText
            android:id="@+id/editFake"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:visibility="gone"
            android:importantForAutofill="no"
            android:hint="@string/empty"
            android:inputType="none"
            tools:targetApi="o"
            android:background="@null"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

    <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
    />
</android.support.constraint.ConstraintLayout>
J. Joel
  • 21
  • 1
  • 4