Created
November 30, 2017 22:32
-
-
Save rdelrosario/38e1fc1a83a525489a0442ddbf1f4135 to your computer and use it in GitHub Desktop.
Android - Toolbar Background
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void UpdateToolbarBackground(Android.Support.V7.Widget.Toolbar toolbar, Page lastPage, Activity activity, Android.Graphics.Drawables.Drawable defaultBackground) | |
{ | |
if (string.IsNullOrEmpty(CustomNavigationPage.GetBarBackground(lastPage)) && CustomNavigationPage.GetGradientColors(lastPage) == null) | |
{ | |
toolbar.SetBackground(defaultBackground); | |
} | |
else | |
{ | |
if (!string.IsNullOrEmpty(CustomNavigationPage.GetBarBackground(lastPage))) | |
{ | |
toolbar.SetBackgroundResource(this.Context.Resources.GetIdentifier(CustomNavigationPage.GetBarBackground(lastPage), "drawable", Android.App.Application.Context.PackageName)); | |
} | |
if (CustomNavigationPage.GetGradientColors(lastPage) != null) | |
{ | |
var colors = CustomNavigationPage.GetGradientColors(lastPage); | |
var direction = GradientDrawable.Orientation.TopBottom; | |
switch (CustomNavigationPage.GetGradientDirection(lastPage)) | |
{ | |
case CustomNavigationPage.GradientDirection.BottomToTop: | |
direction = GradientDrawable.Orientation.BottomTop; | |
break; | |
case CustomNavigationPage.GradientDirection.RightToLeft: | |
direction = GradientDrawable.Orientation.RightLeft; | |
break; | |
case CustomNavigationPage.GradientDirection.LeftToRight: | |
direction = GradientDrawable.Orientation.LeftRight; | |
break; | |
} | |
GradientDrawable gradient = new GradientDrawable(direction, new int[] { colors.Item1.ToAndroid().ToArgb(), colors.Item2.ToAndroid().ToArgb() }); | |
gradient.SetCornerRadius(0f); | |
if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.JellyBean) | |
{ | |
toolbar.SetBackground(gradient); | |
} | |
else | |
{ | |
toolbar.SetBackgroundDrawable(gradient); | |
} | |
} | |
} | |
toolbar.Background.SetAlpha((int)(CustomNavigationPage.GetBarBackgroundOpacity(lastPage) * 255)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment