Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Soft Keyboard does not show up on EditText RecyclerView in a DialogFragment

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

like image 864
Yeol Avatar asked Apr 09 '16 02:04

Yeol


People also ask

How do I get the keyboard to show in Edittext?

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.

How to hide keyboard in Dialog in Android?

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.

What is RecyclerView?

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.


2 Answers

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.

like image 125
Daniel Giljam Avatar answered Nov 15 '22 03:11

Daniel Giljam


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();
like image 29
Ryan Avatar answered Nov 15 '22 03:11

Ryan