Reputation: 139
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
Upvotes: 6
Views: 7679
Reputation: 131
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;
}
Upvotes: 7
Reputation: 21
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>
Upvotes: 0
Reputation: 5434
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;
}
Upvotes: 1
Reputation: 131
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.
Upvotes: 13
Reputation: 3536
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();
Upvotes: 6
Reputation: 4510
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
.
Upvotes: 0