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
edittext. requestFocus(); -> in code. This will open soft keyboard on which edit-text has request focus as activity appears. This open the keyboard at Activity creation.
You can force Android to hide the virtual keyboard using the InputMethodManager, calling hideSoftInputFromWindow, passing in the token of the window containing your edit field. This will force the keyboard to be hidden in all situations.
RecyclerView is the ViewGroup that contains the views corresponding to your data. It's a view itself, so you add RecyclerView into your layout the way you would add any other UI element. Each individual element in the list is defined by a view holder object.
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.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With