Reputation: 5552
When i click on a TextInputEditText, which is parent of a LinearLayout
added programmatically to an AlertDialog
, my keyboard doesn't show (tested on multiple devices)
First I create a new LinearLayout
and add a new Spinner
to it.
After the last item on the spinner is selected, i remove the spinner from the LinearLayout
and add a TextInputEditText
:
layout.removeAllViews();
layout.addView(input);
When i click on the TextInputEditText
it gets focused but no soft keyboard pops up
However if i add the
TextInputEditText
directly as aView
to theAlertDialog
, the keyboard pops up and gets displayed correctly.
My AndroidManifest.xml
has no special entrys.
My full code:
private void dialogAddContact() {
ArrayList<String> mails = new ArrayList<>();
final LinearLayout layout = new LinearLayout(this);
final TextInputEditText input = new TextInputEditText(this);
final Spinner sp = new Spinner(this);
layout.addView(sp);
layout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
input.setInputType(InputType.TYPE_CLASS_TEXT);
input.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
[....]
final ArrayAdapter<String> adp = new ArrayAdapter<>([....]);
sp.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
sp.setAdapter(adp);
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Log.d(Tools.LOGTAG, position + " " + totalMails);
if(position == totalMails - 1){
/****** Here i remove the spinner and add the input ****/
layout.removeAllViews();
layout.addView(input);
}
}
[....]
});
final AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setView(layout, 50, 0, 50, 0)
[....]
});
dialog = builder.create();
dialog.show();
}
Upvotes: 3
Views: 2922
Reputation: 3
The only thing that worked for me is to use a simple Dialog instead of an AlertDialog...
Upvotes: 0
Reputation: 2009
I was having the same problem but any given solution did not work, then i use its parent class (i.e. Dialog
) instead of AlertDialog
. It worked for me.
Upvotes: 2
Reputation: 21766
#. Add focus change listener to your TextInputEditText
and in onFocusChange()
show keyboard using .toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)
.
Required to open keyboard:
final TextInputEditText input = new TextInputEditText(this);
// Keyboard
final InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
// Auto show keyboard
input.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean isFocused) {
if (isFocused) {
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
}
});
#. Use below code to hide
keyboard when your dialog's button
pressed.
Required to hide keyboard:
// Hide keyboard
imm.hideSoftInputFromWindow(input.getWindowToken(), 0);
Hope this will help~
Upvotes: 3
Reputation: 6509
This code works for me.
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,InputMethodManager.HIDE_IMPLICIT_ONLY);
Upvotes: 0
Reputation: 3423
If you want to show the keyboard when the dialog opens you can add this after dialog.show()
:
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
Upvotes: 0