Forum Discussion
EvgenyZobnin
Apr 25, 2022Copper Contributor
Dialogs and toasts in spanned mode
Hi. When the application is expanded to both screens, dialogs and toast are always displayed on the right screen. Is there anything I can do to change this behavior?
CesarValiente
Microsoft
Apr 25, 2022Hi Evgeny,
thanks for your question!
Currently there is not a way to show a Toast on a specific screen. Before Android 11, this was possible by setting its gravity to left, right or center. But as the docs say, starting Android 11 onwards gravity is just a no-op: https://developer.android.com/reference/android/widget/Toast#setGravity(int,%20int,%20int)
For dialogs the approach is the same, changing its window's gravity associated field. You can set the dialog to the left or to the right (by default). As you can imagine, having the folding feature in the middle, it doesn't make sense to have the dialog showing up in just there in the middle of both screens.
How can we align the dialog then?
```
private fun showDialog(side: Side) {
val builder = AlertDialog.Builder(this)
builder.setTitle("Hello world!").setMessage("Side ${side.name} !!")
val dialog = builder.create()
val window = dialog.window
window?.let {
val wlp: WindowManager.LayoutParams = it.attributes
wlp.gravity = when (side) {
Side.LEFT -> Gravity.LEFT
Side.RIGHT -> Gravity.RIGHT
else -> Gravity.CENTER
}
window.attributes = wlp
}
dialog.show()
}
```
Being `Side` an enum we have created previously:
`private enum class Side { LEFT, RIGHT, CENTER }`
Note that CENTER won't place the dialg in the middle of the screen but in the middle of the right screen. RIGHT will align the dialog more to the right of the right screen.
You can find a full sample that I've created here: https://github.com/CesarValiente/dialogs-sample (it includes as well a sample with a ContextMenu).
Please, let me know how it went.
Thanks,
Cesar
thanks for your question!
Currently there is not a way to show a Toast on a specific screen. Before Android 11, this was possible by setting its gravity to left, right or center. But as the docs say, starting Android 11 onwards gravity is just a no-op: https://developer.android.com/reference/android/widget/Toast#setGravity(int,%20int,%20int)
For dialogs the approach is the same, changing its window's gravity associated field. You can set the dialog to the left or to the right (by default). As you can imagine, having the folding feature in the middle, it doesn't make sense to have the dialog showing up in just there in the middle of both screens.
How can we align the dialog then?
```
private fun showDialog(side: Side) {
val builder = AlertDialog.Builder(this)
builder.setTitle("Hello world!").setMessage("Side ${side.name} !!")
val dialog = builder.create()
val window = dialog.window
window?.let {
val wlp: WindowManager.LayoutParams = it.attributes
wlp.gravity = when (side) {
Side.LEFT -> Gravity.LEFT
Side.RIGHT -> Gravity.RIGHT
else -> Gravity.CENTER
}
window.attributes = wlp
}
dialog.show()
}
```
Being `Side` an enum we have created previously:
`private enum class Side { LEFT, RIGHT, CENTER }`
Note that CENTER won't place the dialg in the middle of the screen but in the middle of the right screen. RIGHT will align the dialog more to the right of the right screen.
You can find a full sample that I've created here: https://github.com/CesarValiente/dialogs-sample (it includes as well a sample with a ContextMenu).
Please, let me know how it went.
Thanks,
Cesar
EvgenyZobnin
Apr 25, 2022Copper Contributor
CesarValienteThank you for your quick and complete response. I solved the problem with the dialogues. For toasts I will use a third party library that allows me to change the location of the message.
- CesarValienteApr 25, 2022
Microsoft
Awesome! Happy to hear that!
I completely forgot to mention what you just have said, that probably there are libraries out there that wrap a toast an allow you to move it around.
In fact, this is an approach we followed to build our SnackbarContainer. If you are using snackbars and want to place them on specific screen or both, don't miss our latest lib! https://docs.microsoft.com/dual-screen/android/api-reference/dualscreen-library/snackbar
Thanks,
Cesar- EvgenyZobninApr 26, 2022Copper ContributorAnother problem: I can show "my" dialogs on the side I want, but the system dialogs are still on the right side. For example, I show a dialog in which I explain to the user why a certain permission is needed, then I invoke a request for that permission and the system shows the dialog on a different screen.
- CesarValienteApr 26, 2022
Microsoft
Hi Evgeny,
this is indeed an interesting scenario. Currently, there is not a way to indicate to the framework where the system dialogs should be shown when the app is spanned.
We can see if we can add something to work around this somehow. I will keep you posted in here, but please, don't expect anything in short term (don't want to block you on your work), this is indeed a more complicated task than handling app's dialogs.
Thanks for your question,
Cesar