📜 ⬆️ ⬇️

Create your dialog in Android (dirty trick in official documentation)

I decided to create my own Dialog in andriod. Knowledge gained from official documentation. But as it turned out there is a catch. If you follow the recommendations from the Creating a Custom Dialog documentation, you always get an error:

Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

an error causes the method:

@Override
protected Dialog onCreateDialog(int id) {
Context mContext = getApplicationContext();
Dialog dialog = new Dialog(mContext);

dialog.setContentView(R.layout.quicklog);
dialog.setTitle("Custom Dialog");
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");

return dialog;
}


The reason is that the wrong context is passed to the Dialog object's constructor:
')
Context mContext = getApplicationContext();
Dialog dialog = new Dialog(mContext);


Fixed easy enough. Change getApplicationContext() to this :

Dialog dialog = new Dialog(this);


I think this inaccuracy will be corrected in the documentation later, but be careful.

Source: https://habr.com/ru/post/77870/


All Articles