question

RonaldGANS-4488 avatar image
0 Votes"
RonaldGANS-4488 asked RobCaplan edited

Android Dark Activity Dialog Box Elements Not Visible

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}"
  />
dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RonaldGANS-4488 avatar image
0 Votes"
RonaldGANS-4488 answered

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();




5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LeonLu-MSFT avatar image
0 Votes"
LeonLu-MSFT answered LeonLu-MSFT edited

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.



image.png (19.5 KiB)
image.png (21.8 KiB)
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I have two errors after making (I hope) your changes:

nbutton.SetTextColor(Color.White);
pbutton.SetTextColor(Color.White);

both generate the same error: cannot convert from 'System.Drawing.Color' to 'Android.Content.Res.ColorStateList

And in the list_item I get this error:

{AppThemeBinding Light=Black, Dark=Black}' is incompatible with attribute textColor (attr) reference|color.

Here's list_item.xml:

<?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:gravity="left"
android:padding="3dp"
android:textSize="15sp"
android:textStyle="bold"
android:background="#ff6699"
android:textColor="{AppThemeBinding Light=Black, Dark=Black}"
/>



0 Votes 0 ·

And, it seems no matter what I do I get errors:

nbutton.SetTextColor(Color.parse);

It is using System.Drawing.Color, but no matter what I try to use I get can't convert from System.Drawing.Color to to 'Android.Content.Res.ColorStateList

0 Votes 0 ·

If you fixed this issue?

0 Votes 0 ·