Android Dark Activity Dialog Box Elements Not Visible

Ronald GANS 136 Reputation points
2021-08-31T23:11:27.113+00:00

In Light Mode, this dialog box works OK. But in dark mode, the individual elements, A, B, C, are not visible. WHY?

     var items = new string[] { "A","B","C" };              
     var adapter = new ArrayAdapter<string>(this, Resource.Layout.list_item, items);
     using (var dialog = new Android.App.AlertDialog.Builder(this))
     { 
        listview.Adapter = adapter;
        listview.ItemClick += Listview_ItemClick;
        dialog.SetTitle("Switch Language");
        dialog.SetMessage(language.ClickOnTheLetter()); 
        dialog.SetView(dialogView); 
         dialog.SetNegativeButton("Cancel", (s, a) => { });
          dialog.SetPositiveButton("OK", (s, a) =>
<ListView
    android:id="@+id/listview"
    android:gravity="left"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:choiceMode="singleChoice"
    android:textStyle="bold"
    android:background="#009933"
    TextColor="{AppThemeBinding Light=Black, Dark=Black}"
    />
   </LinearLayout>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/background"
android:gravity="left"
android:padding="3dp"
android:textSize="15sp"
android:textStyle="bold"     
TextColor="{AppThemeBinding Light=Black, Dark=Black}"
 />
Developer technologies | .NET | Xamarin
0 comments No comments
{count} votes

Accepted answer
  1. Ronald GANS 136 Reputation points
    2021-09-03T16:48:17.547+00:00

    HI. I've been trying to implement your changes but I fail.

    The list is shown when the user makes a selection:

    SelectLetter

    And the list box appears:

    A
    B
    C

    void SwitchLetters()
    {

    var dialogView = LayoutInflater.Inflate(Resource.Layout.list_view, null);
    Android.App.AlertDialog alertDialog;

                listview = dialogView.FindViewById<ListView>(Resource.Id.listview);
                textview = dialogView.FindViewById<TextView>(Resource.Id.textview);
    
                var items = new string[] { "A","B","C"};
                var adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, items);
    

    using (var dialog = new Android.App.AlertDialog.Builder(this))
    {

                  listview.Adapter = adapter;
                  listview.ItemClick += Listview_ItemClick;
    
                     dialog.SetTitle("Switch Language");
                    dialog.SetMessage(language.ClickOnTheLanguage()); // ("Click on the language you want to switch to");
                    dialog.SetView(dialogView);
    
                dialog.SetNegativeButton("Cancel", (s, a) => { });
                    dialog.SetPositiveButton("OK", (s, a) =>
                    {
                        string newLetter = string.Empty;
                        switch (prefs.GetInt("selectitemforAlert", 0))
                        {
                            case 0:
                                newLetter = "A";
                                break;
                            case 1:
                                newLetter = "B";
                                break;
                            case 2:
                               newLetter = "C";
                                break;
    
                        }
                        if (newLetter== currentLetter) return;
    
                    Toast.MakeText(this, "Selected Letter is " + newLetter, ToastLength.Short).Show();
    
                    });
                    alertDialog = dialog.Create();
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2021-09-01T05:13:55.09+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    You use this way TextColor="{AppThemeBinding Light=Black, Dark=Black}", it is used in the Xamarin.Forms, but your xml code is related to the Xamarin.Android, it is different. You can use them at the same time.

    For Xamarin android, if you want to create a custom AlertDialog, then change the button's text by the theme(light or dark).

    First of all, you should create a layout called Customlayout.xml in your layout folder. I add your listview to it.

       <?xml version="1.0" encoding="utf-8"?>  
       <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
           android:orientation="vertical"  
           android:layout_width="match_parent"  
           android:layout_height="match_parent">  
           <RelativeLayout  
               android:layout_width="250dp"  
               android:layout_height="250dp"  
               android:layout_centerInParent="true"  
            >  
         
           <ListView  
            android:id="@+id/listview"  
            android:gravity="left"  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content"  
            android:choiceMode="singleChoice"  
            android:textStyle="bold"  
            android:background="#009933"  
              
            />  
         
           </RelativeLayout>  
       </LinearLayout>  
    

    And I notice your Listview used list_item.xml, please create a xml in the layout folder again.

       <?xml version="1.0" encoding="utf-8"?>  
        <TextView xmlns:android="http://schemas.android.com/apk/res/android"  
        android:id="@android:id/text1"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
       android:text="test"  
        android:gravity="left"  
        android:padding="3dp"  
        android:textSize="15sp"  
        android:textStyle="bold"       
          
         />  
    

    Then in you xamarin.android, you can pop up your custom AlertDialog.

       public class MainActivity : AppCompatActivity  
           {  
               Android.App.AlertDialog alertDialog;  
         
               protected override void OnCreate(Bundle savedInstanceState)  
               {  
                   base.OnCreate(savedInstanceState);  
                   Xamarin.Essentials.Platform.Init(this, savedInstanceState);  
                   // Set our view from the "main" layout resource  
             
         
                   // I use following line to change the light mode or dark mode.  
                   AppCompatDelegate.DefaultNightMode = AppCompatDelegate.ModeNightYes;  
         
         
                   SetContentView(Resource.Layout.activity_main);  
                   
         
         
                   var items = new string[] { "A", "B", "C" };  
                   var adapter = new ArrayAdapter<string>(this, Resource.Layout.list_item, items);  
         
                   //1.inflate the Customlayout  
                   View content = LayoutInflater.Inflate(Resource.Layout.Customlayout, null);  
                   //2. Getting the view elements  
                   ListView listview = (ListView)content.FindViewById(Resource.Id.listview);  
         
                   listview.Adapter = adapter;  
         
         
         
                   using (var builder = new Android.App.AlertDialog.Builder(this))  
                   {  
                       listview.Adapter = adapter;  
                       listview.ItemClick += Listview_ItemClick; ;  
                       builder.SetTitle("Switch Language");  
                       builder.SetMessage("select a selection");  
                       builder.SetView(content);  
                       builder.SetNegativeButton("Cancel", (s, a) => { });  
                       builder.SetPositiveButton("OK", (s, a) => { });  
         
         
                       var alert = builder.Create();  
                       alert.Show();  
                       Button nbutton = alert.GetButton((int)DialogButtonType.Negative);  
         
                       
         
                       Button pbutton = alert.GetButton((int)DialogButtonType.Positive);  
                       
                       // if the dark mode, I set the button's textcolor to white, if the light mode, I set the textcolor to   
                       //black  
                       if (AppCompatDelegate.DefaultNightMode.Equals(AppCompatDelegate.ModeNightYes))  
                       {  
                           nbutton.SetTextColor(Color.White);  
         
                           pbutton.SetTextColor(Color.White);  
                       }  
                       else  
                       {  
                           nbutton.SetTextColor(Color.Black);  
                           pbutton.SetTextColor(Color.Black);  
         
                       }  
                   }  
               }  
         
               private void Listview_ItemClick(object sender, AdapterView.ItemClickEventArgs e)  
               {  
                   throw new System.NotImplementedException();  
               }  
    

    Light theme:
    128171-image.png

    Dark theme:

    128172-image.png

    Best Regards,

    Leon Lu


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.