public void CreateGradient() { //Need to convert the colors to Android Color objects var androidColors = new int[] { _startColor.ToAndroid(), _endColor.ToAndroid() }; var gradient = new GradientDrawable(GradientDrawable.Orientation.LeftRight, androidColors); layout.SetBackground(gradient); }
void UpdateTitleViewLayoutBackground(LinearLayout titleViewLayout, string backgroundResource, Android.Graphics.Drawables.Drawable defaultBackground) { if (!string.IsNullOrEmpty(backgroundResource)) { titleViewLayout?.SetBackgroundResource(this.Context.Resources.GetIdentifier(backgroundResource, "drawable", Android.App.Application.Context.PackageName)); } else { titleViewLayout?.SetBackground(defaultBackground); } }
public void CreateGradient() { //Need to convert the colors to Android Color objects int[] androidColors = new int[gradientColors.Count()]; for (int i = 0; i < gradientColors.Count(); i++) { Xamarin.Forms.Color temp = gradientColors[i]; androidColors[i] = temp.ToAndroid(); } GradientDrawable gradient = new GradientDrawable(GradientDrawable.Orientation.LeftRight, androidColors); if (roundCorners) { gradient.SetCornerRadii(new float[] { cornerRadius, cornerRadius, cornerRadius, cornerRadius, cornerRadius, cornerRadius, cornerRadius, cornerRadius }); } layout.SetBackground(gradient); }
protected virtual BottomSheetDialog CreateMoreBottomSheet(Action <int, BottomSheetDialog> selectCallback) { var bottomSheetDialog = new BottomSheetDialog(Context); var bottomSheetLayout = new LinearLayout(Context); using (var bottomShellLP = new LP(LP.MatchParent, LP.WrapContent)) bottomSheetLayout.LayoutParameters = bottomShellLP; bottomSheetLayout.Orientation = Orientation.Vertical; // handle the more tab for (int i = _bottomView.MaxItemCount - 1; i < ShellItem.Items.Count; i++) { var closure_i = i; var shellContent = ShellItem.Items[i]; using (var innerLayout = new LinearLayout(Context)) { innerLayout.SetClipToOutline(true); innerLayout.SetBackground(CreateItemBackgroundDrawable()); innerLayout.SetPadding(0, (int)Context.ToPixels(6), 0, (int)Context.ToPixels(6)); innerLayout.Orientation = Orientation.Horizontal; using (var param = new LP(LP.MatchParent, LP.WrapContent)) innerLayout.LayoutParameters = param; // technically the unhook isn't needed // we dont even unhook the events that dont fire void clickCallback(object s, EventArgs e) { selectCallback(closure_i, bottomSheetDialog); if (!innerLayout.IsDisposed()) { innerLayout.Click -= clickCallback; } } innerLayout.Click += clickCallback; var image = new ImageView(Context); var lp = new LinearLayout.LayoutParams((int)Context.ToPixels(32), (int)Context.ToPixels(32)) { LeftMargin = (int)Context.ToPixels(20), RightMargin = (int)Context.ToPixels(20), TopMargin = (int)Context.ToPixels(6), BottomMargin = (int)Context.ToPixels(6), Gravity = GravityFlags.Center }; image.LayoutParameters = lp; lp.Dispose(); image.ImageTintList = ColorStateList.ValueOf(Color.Black.MultiplyAlpha(0.6).ToAndroid()); ShellContext.ApplyDrawableAsync(shellContent, ShellSection.IconProperty, icon => { if (!image.IsDisposed()) { image.SetImageDrawable(icon); } }); innerLayout.AddView(image); using (var text = new TextView(Context)) { text.Typeface = "sans-serif-medium".ToTypeFace(); text.SetTextColor(AColor.Black); text.Text = shellContent.Title; lp = new LinearLayout.LayoutParams(0, LP.WrapContent) { Gravity = GravityFlags.Center, Weight = 1 }; text.LayoutParameters = lp; lp.Dispose(); innerLayout.AddView(text); } bottomSheetLayout.AddView(innerLayout); } } bottomSheetDialog.SetContentView(bottomSheetLayout); bottomSheetLayout.Dispose(); return(bottomSheetDialog); }
private void LastPage_PropertyChanged(object sender, PropertyChangedEventArgs e) { var lastPage = sender as Page; if (e.PropertyName == CustomNavigationPage.HasShadowProperty.PropertyName) { UpdateToolbarShadow(_toolbar, CustomNavigationPage.GetHasShadow(lastPage), Context as Activity, _originalWindowContent); } else if (e.PropertyName == CustomNavigationPage.TitleBackgroundProperty.PropertyName) { UpdateTitleViewLayoutBackground(_titleViewLayout, CustomNavigationPage.GetTitleBackground(lastPage), _originalDrawable); } else if (e.PropertyName == CustomNavigationPage.BarBackgroundProperty.PropertyName) { UpdateToolbarBackground(_toolbar, lastPage, Context as Activity, _originalToolbarBackground); } else if (e.PropertyName == CustomNavigationPage.GradientColorsProperty.PropertyName) { UpdateToolbarBackground(_toolbar, lastPage, Context as Activity, _originalToolbarBackground); } else if (e.PropertyName == CustomNavigationPage.GradientDirectionProperty.PropertyName) { UpdateToolbarBackground(_toolbar, lastPage, Context as Activity, _originalToolbarBackground); } else if (e.PropertyName == CustomNavigationPage.BarBackgroundOpacityProperty.PropertyName) { UpdateToolbarBackground(_toolbar, lastPage, Context as Activity, _originalToolbarBackground); } else if (e.PropertyName == CustomNavigationPage.TitleBorderCornerRadiusProperty.PropertyName) { _titleViewLayout?.SetBackground(CreateShape(ShapeType.Rectangle, (int)CustomNavigationPage.GetTitleBorderWidth(lastPage), (int)CustomNavigationPage.GetTitleBorderCornerRadius(lastPage), CustomNavigationPage.GetTitleFillColor(lastPage), CustomNavigationPage.GetTitleBorderColor(lastPage))); } else if (e.PropertyName == CustomNavigationPage.TitleBorderWidthProperty.PropertyName) { _titleViewLayout?.SetBackground(CreateShape(ShapeType.Rectangle, (int)CustomNavigationPage.GetTitleBorderWidth(lastPage), (int)CustomNavigationPage.GetTitleBorderCornerRadius(lastPage), CustomNavigationPage.GetTitleFillColor(lastPage), CustomNavigationPage.GetTitleBorderColor(lastPage))); } else if (e.PropertyName == CustomNavigationPage.TitleBorderColorProperty.PropertyName) { _titleViewLayout?.SetBackground(CreateShape(ShapeType.Rectangle, (int)CustomNavigationPage.GetTitleBorderWidth(lastPage), (int)CustomNavigationPage.GetTitleBorderCornerRadius(lastPage), CustomNavigationPage.GetTitleFillColor(lastPage), CustomNavigationPage.GetTitleBorderColor(lastPage))); } else if (e.PropertyName == CustomNavigationPage.TitleFillColorProperty.PropertyName) { _titleViewLayout?.SetBackground(CreateShape(ShapeType.Rectangle, (int)CustomNavigationPage.GetTitleBorderWidth(lastPage), (int)CustomNavigationPage.GetTitleBorderCornerRadius(lastPage), CustomNavigationPage.GetTitleFillColor(lastPage), CustomNavigationPage.GetTitleBorderColor(lastPage))); } else if (e.PropertyName == CustomNavigationPage.TitlePositionProperty.PropertyName) { UpdateTitleViewLayoutAlignment(_titleViewLayout, _titleTextView, _subTitleTextView, CustomNavigationPage.GetTitlePosition(lastPage)); //UpdateTitleViewLayout(lastPage, _titleViewLayout, _titleTextView, _subTitleTextView, _originalDrawable); } else if (e.PropertyName == CustomNavigationPage.TitlePaddingProperty.PropertyName) { UpdateTitleViewLayoutPadding(_titleViewLayout, CustomNavigationPage.GetTitlePadding(lastPage)); } else if (e.PropertyName == CustomNavigationPage.TitleMarginProperty.PropertyName) { UpdateTitleViewLayoutMargin(_titleViewLayout, CustomNavigationPage.GetTitleMargin(lastPage)); } else if (e.PropertyName == CustomNavigationPage.TitleColorProperty.PropertyName) { UpdateToolbarTextColor(_titleTextView, CustomNavigationPage.GetTitleColor(lastPage), _originalColorStateList); } else if (e.PropertyName == CustomNavigationPage.TitleFontProperty.PropertyName) { UpdateToolbarTextFont(_titleTextView, CustomNavigationPage.GetTitleFont(lastPage), _originalFont); } else if (e.PropertyName == Page.TitleProperty.PropertyName) { UpdateTitleText(_titleTextView, lastPage.Title); } else if (e.PropertyName == CustomPage.FormattedTitleProperty.PropertyName && (lastPage is CustomPage)) { var cPage = lastPage as CustomPage; UpdateFormattedTitleText(_titleTextView, cPage.FormattedTitle, cPage.Title); } else if (e.PropertyName == CustomNavigationPage.SubtitleColorProperty.PropertyName) { UpdateToolbarTextColor(_subTitleTextView, CustomNavigationPage.GetSubtitleColor(lastPage), _originalColorStateList); } else if (e.PropertyName == CustomNavigationPage.SubtitleFontProperty.PropertyName) { UpdateToolbarTextFont(_subTitleTextView, CustomNavigationPage.GetSubtitleFont(lastPage), _originalFont); } else if (e.PropertyName == CustomPage.SubtitleProperty.PropertyName && (lastPage is CustomPage)) { var cPage = lastPage as CustomPage; if (!string.IsNullOrEmpty(cPage.Subtitle)) { _subTitleTextView.Text = cPage.Subtitle; _subTitleTextView.Visibility = ViewStates.Visible; } else { ClearTextView(_subTitleTextView, true); } } else if (e.PropertyName == CustomPage.FormattedTitleProperty.PropertyName && (lastPage is CustomPage)) { var cPage = lastPage as CustomPage; if (cPage.FormattedSubtitle != null && cPage.FormattedSubtitle.Spans.Count > 0) { _subTitleTextView.TextFormatted = cPage.FormattedSubtitle.ToAttributed(Font.Default, Xamarin.Forms.Color.Default, _subTitleTextView); _subTitleTextView.Visibility = ViewStates.Visible; } else { ClearTextView(_subTitleTextView, true); } } }
protected virtual BottomSheetDialog CreateMoreBottomSheet(Action <int, BottomSheetDialog> selectCallback) { var bottomSheetDialog = new BottomSheetDialog(Context); var bottomSheetLayout = new LinearLayout(Context); using (var bottomShellLP = new LP(LP.MatchParent, LP.WrapContent)) bottomSheetLayout.LayoutParameters = bottomShellLP; bottomSheetLayout.Orientation = Orientation.Vertical; // handle the more tab var items = ((IShellItemController)ShellItem).GetItems(); for (int i = _bottomView.MaxItemCount - 1; i < items.Count; i++) { var closure_i = i; var shellContent = items[i]; using (var innerLayout = new LinearLayout(Context)) { innerLayout.SetClipToOutline(true); innerLayout.SetBackground(CreateItemBackgroundDrawable()); innerLayout.SetPadding(0, (int)Context.ToPixels(6), 0, (int)Context.ToPixels(6)); innerLayout.Orientation = Orientation.Horizontal; using (var param = new LP(LP.MatchParent, LP.WrapContent)) innerLayout.LayoutParameters = param; // technically the unhook isn't needed // we dont even unhook the events that dont fire void clickCallback(object s, EventArgs e) { selectCallback(closure_i, bottomSheetDialog); if (!innerLayout.IsDisposed()) { innerLayout.Click -= clickCallback; } } innerLayout.Click += clickCallback; var image = new ImageView(Context); var lp = new LinearLayout.LayoutParams((int)Context.ToPixels(32), (int)Context.ToPixels(32)) { LeftMargin = (int)Context.ToPixels(20), RightMargin = (int)Context.ToPixels(20), TopMargin = (int)Context.ToPixels(6), BottomMargin = (int)Context.ToPixels(6), Gravity = GravityFlags.Center }; image.LayoutParameters = lp; lp.Dispose(); var services = MauiContext.Services; var provider = services.GetRequiredService <IImageSourceServiceProvider>(); var icon = shellContent.Icon; shellContent.Icon.LoadImage( MauiContext, (result) => { image.SetImageDrawable(result?.Value); if (result?.Value != null) { var color = Colors.Black.MultiplyAlpha(0.6f).ToPlatform(); result.Value.SetTint(color); } }); innerLayout.AddView(image); using (var text = new TextView(Context)) { text.Typeface = services.GetRequiredService <IFontManager>() .GetTypeface(Font.OfSize("sans-serif-medium", 0.0)); text.SetTextColor(AColor.Black); text.Text = shellContent.Title; lp = new LinearLayout.LayoutParams(0, LP.WrapContent) { Gravity = GravityFlags.Center, Weight = 1 }; text.LayoutParameters = lp; lp.Dispose(); innerLayout.AddView(text); } bottomSheetLayout.AddView(innerLayout); } } bottomSheetDialog.SetContentView(bottomSheetLayout); bottomSheetLayout.Dispose(); return(bottomSheetDialog); }
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) { base.OnElementPropertyChanged(sender, e); var lastPage = Element.CurrentPage; if (e.PropertyName == CustomNavigationPage.HasShadowProperty.PropertyName) { UpdateToolbarShadow(_toolbar, CustomNavigationPage.GetHasShadow(lastPage), Context as Activity, _originalWindowContent); } else if (e.PropertyName == CustomNavigationPage.TitleBackgroundProperty.PropertyName) { UpdateTitleViewLayoutBackground(_titleViewLayout, CustomNavigationPage.GetTitleBackground(lastPage), _originalDrawable); } else if (e.PropertyName == CustomNavigationPage.BarBackgroundProperty.PropertyName) { UpdateToolbarBackground(_toolbar, lastPage, Context as Activity, _originalToolbarBackground); } else if (e.PropertyName == CustomNavigationPage.GradientColorsProperty.PropertyName) { UpdateToolbarBackground(_toolbar, lastPage, Context as Activity, _originalToolbarBackground); } else if (e.PropertyName == CustomNavigationPage.GradientDirectionProperty.PropertyName) { UpdateToolbarBackground(_toolbar, lastPage, Context as Activity, _originalToolbarBackground); } else if (e.PropertyName == CustomNavigationPage.BarBackgroundOpacityProperty.PropertyName) { UpdateToolbarBackground(_toolbar, lastPage, Context as Activity, _originalToolbarBackground); } else if (e.PropertyName == CustomNavigationPage.TitleBorderCornerRadiusProperty.PropertyName) { _titleViewLayout?.SetBackground(CreateShape(ShapeType.Rectangle, (int)CustomNavigationPage.GetTitleBorderWidth(lastPage), (int)CustomNavigationPage.GetTitleBorderCornerRadius(lastPage), CustomNavigationPage.GetTitleFillColor(lastPage), CustomNavigationPage.GetTitleBorderColor(lastPage))); } else if (e.PropertyName == CustomNavigationPage.TitleBorderWidthProperty.PropertyName) { _titleViewLayout?.SetBackground(CreateShape(ShapeType.Rectangle, (int)CustomNavigationPage.GetTitleBorderWidth(lastPage), (int)CustomNavigationPage.GetTitleBorderCornerRadius(lastPage), CustomNavigationPage.GetTitleFillColor(lastPage), CustomNavigationPage.GetTitleBorderColor(lastPage))); } else if (e.PropertyName == CustomNavigationPage.TitleBorderColorProperty.PropertyName) { _titleViewLayout?.SetBackground(CreateShape(ShapeType.Rectangle, (int)CustomNavigationPage.GetTitleBorderWidth(lastPage), (int)CustomNavigationPage.GetTitleBorderCornerRadius(lastPage), CustomNavigationPage.GetTitleFillColor(lastPage), CustomNavigationPage.GetTitleBorderColor(lastPage))); } else if (e.PropertyName == CustomNavigationPage.TitleFillColorProperty.PropertyName) { _titleViewLayout?.SetBackground(CreateShape(ShapeType.Rectangle, (int)CustomNavigationPage.GetTitleBorderWidth(lastPage), (int)CustomNavigationPage.GetTitleBorderCornerRadius(lastPage), CustomNavigationPage.GetTitleFillColor(lastPage), CustomNavigationPage.GetTitleBorderColor(lastPage))); } else if (e.PropertyName == CustomNavigationPage.TitlePositionProperty.PropertyName) { UpdateTitleViewLayoutAlignment(_titleViewLayout, _titleTextView, _subTitleTextView, CustomNavigationPage.GetTitlePosition(lastPage)); } else if (e.PropertyName == CustomNavigationPage.TitlePaddingProperty.PropertyName) { UpdateTitleViewLayoutPadding(_titleViewLayout, CustomNavigationPage.GetTitlePadding(lastPage)); } else if (e.PropertyName == CustomNavigationPage.TitleMarginProperty.PropertyName) { UpdateTitleViewLayoutMargin(_titleViewLayout, CustomNavigationPage.GetTitleMargin(lastPage)); } else if (e.PropertyName == CustomNavigationPage.TitleColorProperty.PropertyName) { UpdateToolbarTextColor(_titleTextView, CustomNavigationPage.GetTitleColor(lastPage), _originalColorStateList); } else if (e.PropertyName == CustomNavigationPage.TitleFontProperty.PropertyName) { UpdateToolbarTextFont(_titleTextView, CustomNavigationPage.GetTitleFont(lastPage), _originalFont); } else if (e.PropertyName == Page.TitleProperty.PropertyName) { UpdateTitleText(_titleTextView, lastPage.Title); } else if (e.PropertyName == CustomNavigationPage.SubtitleColorProperty.PropertyName) { UpdateToolbarTextColor(_subTitleTextView, CustomNavigationPage.GetSubtitleColor(lastPage), _originalColorStateList); } else if (e.PropertyName == CustomNavigationPage.SubtitleFontProperty.PropertyName) { UpdateToolbarTextFont(_subTitleTextView, CustomNavigationPage.GetSubtitleFont(lastPage), _originalFont); } }
public void Show(ToastView view, object viewModel = null) { view.Parent = XF.Application.Current.MainPage; view.BindingContext = viewModel; var toast = new Android.Widget.Toast(Dialogs.Context); var offsetX = (int)Dialogs.Context.ToPixels(view.OffsetX); var offsetY = (int)Dialogs.Context.ToPixels(view.OffsetY); // HACK: For some reason, the offset direction is reversed when GravityFlags contains Left or Bottom. if (view.HorizontalLayoutAlignment == XF.LayoutAlignment.End) { offsetX *= -1; } if (view.VerticalLayoutAlignment == XF.LayoutAlignment.End) { offsetY *= -1; } toast.SetGravity(Dialogs.GetGravity(view), offsetX, offsetY); toast.Duration = Android.Widget.ToastLength.Long; var renderer = Dialogs.CreateNativeView(view); var measure = Dialogs.Measure(view); view.Layout(new XF.Rectangle(new XF.Point(0, 0), measure)); var realW = (int)Dialogs.Context.ToPixels(measure.Width); var realH = (int)Dialogs.Context.ToPixels(measure.Height); var layout = new LinearLayout(Dialogs.Context); using (var param = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent) { Width = realW, Height = realH }){ layout.LayoutParameters = param; } using (var param = new LinearLayout.LayoutParams(realW, realH) { Width = realW, Height = realH }){ layout.AddView(renderer.View, param); } if (view.CornerRadius > 0) { var border = new GradientDrawable(); border.SetCornerRadius(Dialogs.Context.ToPixels(view.CornerRadius)); if (!view.BackgroundColor.IsDefault) { border.SetColor(view.BackgroundColor.ToAndroid()); border.Alpha = (int)(view.Opacity * 255); } layout.ClipToOutline = true; layout.SetBackground(border); } toast.View = layout; view.RunPresentationAnimation(); toast.Show(); var duration = Math.Max(Math.Min(view.Duration - 260, 3500), 0); // give a bit millisecond margin var handler = new Handler(); handler.PostDelayed(new Runnable(view.RunDismissalAnimation), duration); handler.PostDelayed(new Runnable(() => { //view.RunDismissalAnimation(); //await Task.Delay(250); toast?.Cancel(); view.Parent = null; if (!renderer.View.IsDisposed()) { renderer.View.RemoveFromParent(); renderer.View.Dispose(); } layout.Dispose(); // I coudn't reproduce https://github.com/muak/AiForms.Dialogs/issues/2. // But I let this code disabled because it has no influent even if it is disabled. //toast.View = null; renderer.Dispose(); renderer = null; toast?.Dispose(); toast = null; view.Destroy(); view.BindingContext = null; }), view.Duration); }