Cody
Cody

Reputation: 319

.NET 8 MAUI: How to customize the look of the default Page.IsBusy ActivityIndicator on Android?

In .NET MAUI, each page has an IsBusy property that, when true, shows a built-in activity indicator. I just want to know how to change the style of that specific indicator. I tried changing the style of ActivityIndicator in Styles.xaml, but that didn't do anything, so I assume I have to alter something in Platforms>Android>Resources>values but I don't know what.

In a default .NET 8 MAUI app running on Android, when you set IsBusy to True in the AppShell (or any other page), a blue activity indicator runs. I want to change the style of that. Ideally at least the colour. screenshot

Upvotes: 1

Views: 1010

Answers (1)

Jianwei Sun - MSFT
Jianwei Sun - MSFT

Reputation: 4332

Well, I figure out it.

For built-in activity indicator, it is achieved through MessagingCenter(refer to source code). On specific platform, e.g. Android, it adds and remove ProgressBar to achieve the effect of showing and hiding.

So, get the native view and modify it:

private void Button_Clicked(object sender, EventArgs e)
        {
            IsBusy = !IsBusy;

#if ANDROID
            var progressLayout = Platform.CurrentActivity?.FindViewById(16908999) as Android.Widget.FrameLayout;
            var progressbar = progressLayout?.GetChildAt(0) as Android.Widget.ProgressBar;


           //when IsBusy is false, progressbar will be removed, will be null. So should make a judgment
            if (progressbar != null)
            {
                // set it to red
                var iconColorStates = Android.Content.Res.ColorStateList.ValueOf(Android.Graphics.Color.Red);
                progressbar.IndeterminateTintList = iconColorStates;
            }
#endif

Here is the effect.

Upvotes: 1

Related Questions