Leanback version of AlertDialog
Asked Answered
O

3

7

In Android, making an AlertDialog with Positive/Negative buttons is easy.

new AlertDialog.Builder(getActivity())
           .setTitle("Question?")
           .setPositiveButton("YES", new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int which) {
                   // User chose YES
               }
           })
           .setNegativeButton("NO", new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialogInterface, int i) {
                   // User chose NO
                }
           }).create().show();

I would like to make the equivalent in my TV App. (ask the user a question, get the Yes/No response)

I have found two things, neither of which solve my use case:

  1. ErrorFragment (close, but only has 1 button)

  2. GuidedStepFragment (allows me multiple options, but requires creating a separate Activity, and I have to write my own code to pass the selected choice back to the first Activity)

I feel like i'm missing something. This is only a few lines of code in a normal Android app.

I tried just using AlertDialog in my TV App, but it throws an error about having the incorrect Theme set on the App. Plus, I don't think those dialogs are accesibility friendly anyway.

Ocotillo answered 31/1, 2018 at 15:56 Comment(0)
O
9

Looks like this is possible by adding a Theme to the AlertDialog call:

new AlertDialog.Builder(getActivity(), R.style.Theme_AppCompat)
   ....
Ocotillo answered 31/1, 2018 at 19:9 Comment(3)
It makes the alert dialog full screen and kinda fugly. Any ideas?Pozzy
@Pozzy pass R.style.Theme_AppCompat_Dialog_Alert or other suitable dialog style to make it smaller.Bandwagon
R.style.Theme_AppCompat_Dialog_Alert works fine for meChorography
R
1

If you just want to show an AlertDialog without throwing an error then you can construct the AlertDialog.Builder with a theme, I prefer using AlertDialog.THEME_DEVICE_DEFAULT_DARK

new AlertDialog.Builder(getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_DARK)
       .setTitle("Question?")
       .setPositiveButton("YES", new DialogInterface.OnClickListener() {
           @Override
           public void onClick(DialogInterface dialog, int which) {
               // User chose YES
           }
       })
       .setNegativeButton("NO", new DialogInterface.OnClickListener() {
           @Override
           public void onClick(DialogInterface dialogInterface, int i) {
               // User chose NO
            }
       }).create().show();

I can't help you if you want to build an AlertDialog that looks like an ErrorFragment or a GuidedStepFragment

Edit:

AlertDialog.THEME_DEVICE_DEFAULT_DARK was deprecated in API level 23, so you can use android.R.style.Theme_DeviceDefault_Dialog_Alert instead, like this:

new AlertDialog.Builder(getActivity(), android.R.style.Theme_DeviceDefault_Dialog_Alert)
       .setTitle("Question?")
       .setPositiveButton("YES", new DialogInterface.OnClickListener() {
           @Override
           public void onClick(DialogInterface dialog, int which) {
               // User chose YES
           }
       })
       .setNegativeButton("NO", new DialogInterface.OnClickListener() {
           @Override
           public void onClick(DialogInterface dialogInterface, int i) {
               // User chose NO
            }
       }).create().show();
Ro answered 2/8, 2022 at 20:29 Comment(2)
Cannot resolve symbol 'THEME_DEVICE_DEFAULT_DARK'. Where does it come from?Thorathoracic
See the edit that i've done to this post.Ro
R
1

Alternatively, you can use a class called TvDialog, which uses GuidedStepFragment to create something like this:

You can see it here

Ro answered 27/9, 2022 at 10:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.