Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AlertDialog - Show soft keyboard with a custom class not working

My issue is that my custom alertdialog class is not displaying the softkeyboard correctly. I am creating it using

SettingsDialog settingsDialog = new SettingsDialog(MainActivity.this);
settingsDialog.show();

And the softkeyboard is not displaying. I've followed other stackoverflow answers to displaying the keyboard ... Show soft keyboard for dialog

and it works if I do not use a custom class

AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this);
mBuilder.setView(R.layout.alertdialog_settings);
AlertDialog alertDialog = mBuilder.create();
alertDialog.show();

enter image description here

However when using a custom AlertDialog class I can't seem to get the same outcome as the picture above

I have tried manually displaying the keyboard

SettingsDialog settingsDialog = new SettingsDialog(MainActivity.this);
settingsDialog.show();
InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm != null){
   imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
}

enter image description here

However it shows the keyboard behind the alertdialog and doesn't give the same effect as AlertDialog Builder.

How can I display the softkeyboard using a Custom AlertDialog to have output as using AlertDialog Builder?

Edit:

I have also tried manually displaying it in the AlertDialog's onCreate Method

public class SettingsDialog extends AlertDialog {
     public SettingsDialog(@NonNull Context context, String subName) {
            super(context);
            this.mContext = context;
            this.mSubName = subName;

     }

     @Override
     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.alertdialog_settings);

        InputMethodManager imm = (InputMethodManager) 
        mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        if(imm != null){
          imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
        }

     }
}

However this still causes the keyboard to be displayed behind the alertDialog

like image 477
DIRTY DAVE Avatar asked Dec 29 '19 21:12

DIRTY DAVE


People also ask

Is AlertDialog deprecated?

A simple dialog containing an DatePicker . This class was deprecated in API level 26.

What is the difference between dialog and AlertDialog?

AlertDialog is a lightweight version of a Dialog. This is supposed to deal with INFORMATIVE matters only, That's the reason why complex interactions with the user are limited. Dialog on the other hand is able to do even more complex things .

How many buttons can be set up on an AlertDialog?

AlertDialog. A dialog that can show a title, up to three buttons, a list of selectable items, or a custom layout.


2 Answers

I think you do not need to extend the Alert Dialog class, what you can do is simply a your custom java which contain helper function create your custom dialog, so you will still have code abstraction and can add additional features with ease.

public class SettingsDialog  {

  private  AlertDialog.Builder mBuilder = null;
  private  AlertDialog alertDialog = null;

 public SettingsDialog(@NonNull Context context, String subName) {
        this.mSubName = subName;
        this.mContext = context;
 }

 public show(){
    mBuilder = new AlertDialog.Builder(mContext);
    mBuilder.setView(R.layout.someID);
    alertDialog = mBuilder.create();
    alertDialog.show();
 }

 public void dismiss(){
    if(alertDialog == null) return;
    alertDialog.dismiss();
 }

 // can use interface to handle callbacks

}


// usage 

SettingsDialog sd = new SettingsDialog(this, "MATHS");
sd.show();
//sd.dismiss();
like image 74
Abhishek Garg Avatar answered Oct 17 '22 20:10

Abhishek Garg


I've tried many other methods but this one finally works.

SortByDialog sortByDialog = new SortByDialog(MainActivity.this);
sortByDialog.show();
sortByDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
|WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);

Need to make sure the clearFlags is after the .show() for your custom AlertDialogClass

like image 39
DIRTY DAVE Avatar answered Oct 17 '22 18:10

DIRTY DAVE