Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 button.and my dialog is not moving to top.

private void AlertEditMode(final MyIshVo myIshVo) {
    final LinearLayout linearLayout=new LinearLayout(getContext());
    final EditText edittext = new EditText(getContext());
    LinearLayout.LayoutParams layoutParams =new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
    layoutParams.setMargins(50,0,50,0);
    edittext.setLayoutParams(layoutParams);
    linearLayout.addView(edittext);
    final AlertDialog dialog = new AlertDialog.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(new DialogInterface.OnShowListener() {

        @Override
        public void onShow(final DialogInterface dialog) {

            Button button = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);
            button.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    String ish_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.show();
   // dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

}

i have already tried to give adjustresize to activity and dynamically give

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

but both solutions are not working.any other solutions please suggest thanks in advance.

[![check image][1]][1]

like image 726
umesh vashisth Avatar asked Dec 08 '22 15:12

umesh vashisth


2 Answers

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.

like image 137
Jukurrpa Avatar answered Dec 11 '22 10:12

Jukurrpa


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

    private void AlertEditMode(final MyIshVo myIshVo) {
    final LinearLayout linearLayout=new LinearLayout(getContext());
    final EditText edittext = new EditText(getContext());
    LinearLayout.LayoutParams layoutParams =new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
    layoutParams.setMargins(50,0,50,0);
    edittext.setLayoutParams(layoutParams);
    linearLayout.addView(edittext);
    final AlertDialog dialog = new AlertDialog.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(new DialogInterface.OnShowListener() {

        @Override
        public void onShow(final DialogInterface dialog) {

            Button button = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);
            button.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    String ish_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.LayoutParams wmlp = 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);

}
like image 37
Android Team Avatar answered Dec 11 '22 09:12

Android Team