0

I made a custom alertDialog and in it I have put an EditText, but the problem is the keyboard won't show when I click on the EditText to write an input!

Here's my alert dialog class:

public class EditTaskDialog extends AlertDialog {



  Activity mParent;
    public EditTaskDialog(Context context, Activity parent) {
        super(context);
        mParent = parent;
    }

    @BindView(R.id.btn_edit_edti_task_dialog)
    Button btn_edit_edti_task_dialog;
    @BindView(R.id.tv_time_edit_task_dialog)
    TextView tv_time_edit_task_dialog;
    @BindView(R.id.ll_time_edit_task_dialog)
    LinearLayout ll_time_edit_task_dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //the layout that have the edit text view
        setContentView(R.layout.edit_task_dialog);

        ButterKnife.bind(this);

        final DatePickerDialog.OnDateSetListener x = new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                tv_time_edit_task_dialog.setText(dayOfMonth+ "/" + (month+1) +"/"+ year);
            }
        };

        ll_time_edit_task_dialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                new DatePickerDialog(
                        mParent,
                        x,
                        Calendar.getInstance().get(Calendar.YEAR),
                        Calendar.getInstance().get(Calendar.MONTH),
                        Calendar.getInstance().get(Calendar.DAY_OF_MONTH)
                ).show();

            }
        });
    }
}

Please notice that I can't use getSystemService() because it's connected to the activity that started the alertDialog so when I use it like this

InputMethodManager imm = (InputMethodManager)   mParent.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

the keyboard shows behind the alert dialog and in front of the starting activity

Mina Shaker
  • 333
  • 3
  • 12
  • 2
    Possible duplicate of [Close/hide the Android Soft Keyboard](https://stackoverflow.com/questions/1109022/close-hide-the-android-soft-keyboard) – Alireza aslami Mar 04 '18 at 11:10
  • no this is different the problem is the edit text not in an activity, it's in an alert dialog – Mina Shaker Mar 04 '18 at 11:22

2 Answers2

0

For hiding keyboard:

InputMethodManager imm = (InputMethodManager)getSystemService(
  Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);

For showing keyboard:

InputMethodManager imm = (InputMethodManager)   getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
Rainmaker
  • 7,673
  • 4
  • 38
  • 63
  • this shows the keyboard behind the alert dialog, because getSystemService is connected to the activity that started the alert dialog not the alert dialog itself – Mina Shaker Mar 04 '18 at 11:29
  • adding this to your manifest for the activity you're using android:windowSoftInputMode="adjustNothing" should keep the keyboard on top if the dialogue is overlaid for some reason.The point is you need to get the keyboard appear ontop of any other view. Play with different softinputmodes – Rainmaker Mar 04 '18 at 11:57
0

I fixed the problem instead of using AlertDialog i used the Dialog Class

Mina Shaker
  • 333
  • 3
  • 12