Skip to content Skip to sidebar Skip to footer

Alert Dialog Not Moving To Up When Keyboard Is Open In Fragment

i have created one alert dialog in fragment dynamically.which has one edit text and one button.what my problem is when user focus on edit text keyboard is open and its hide dialog

Solution 1:

I've had this issue when the Activity in which the AlertDialog is shown had a translucent status bar theme.

So instead of initializing the builder with the classic AlertDialog.Builder(context) which uses the context's theme, I explicitly pass a dialog theme with no translucent status bar to it:

AlertDialog.Builder(context, android.R.style.Theme_DeviceDefault_Light_Dialog_NoActionBar)

I picked this theme as it ensures compatibility with pre-lollipop devices, but other ones like android.R.style.Theme_Material_Dialog should do the trick too.

Solution 2:

if you want to show dialog at the top then used below code..

privatevoidAlertEditMode(final MyIshVo myIshVo) {
    final LinearLayout linearLayout=newLinearLayout(getContext());
    finalEditTextedittext=newEditText(getContext());
    LinearLayout.LayoutParamslayoutParams=newLinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
    layoutParams.setMargins(50,0,50,0);
    edittext.setLayoutParams(layoutParams);
    linearLayout.addView(edittext);
    finalAlertDialogdialog=newAlertDialog.Builder(getContext())
            .setView(linearLayout)
            .setTitle("Instant Share")
            .setPositiveButton(android.R.string.ok, null) //Set to null. We override the onclick
            .setNegativeButton(android.R.string.cancel, null)
            .create();


    // relativeLayout.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

    edittext.setText(myIshVo.getMish_name());

    edittext.setSelection(edittext.getText().length());
    dialog.setOnShowListener(newDialogInterface.OnShowListener() {

        @OverridepublicvoidonShow(final DialogInterface dialog) {

            Buttonbutton= ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);
            button.setOnClickListener(newView.OnClickListener() {

                @OverridepublicvoidonClick(View view) {
                    Stringish_title= edittext.getText().toString().trim();
                    String instant_share_Id=myIshVo.getMinstant_share_Id();
                    if(ish_title==null || ish_title.equalsIgnoreCase("") || ish_title.contains("<") || ish_title.contains("\\") ){
                        //showToastMessage("Title should not be Empty");
                        edittext.setError("Please enter valid title.");
                    }else{
                        presenter.setEditIsh(ish_title,instant_share_Id);
                        dialog.dismiss();
                    }
                }
            });
        }
    });
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    WindowManager.LayoutParamswmlp= dialog.getWindow().getAttributes();

    wmlp.gravity = Gravity.TOP | Gravity.LEFT;
    wmlp.x = 100;   //x position
    wmlp.y = 100;   //y position

    dialog.show();
    // dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

}

Solution 3:

In my case, I'm using MaterialComponents as my application's theme, so no solutions posted in here worked for me.

My real problem was that the AlertDialog was not resizing whenever the keyboard opened, so the alert was always behind the keyboard. Changing the soft input mode did the trick:

AlertDialog.Builderbuilder=newAlertDialog.Builder(this);
builder.setTitle("Example");

// set your custom view...// Then show itAlertDialogdialog= builder.show();

// This did the trick
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

Post a Comment for "Alert Dialog Not Moving To Up When Keyboard Is Open In Fragment"