Xamarin Forms FAB Icon Suddenly Squeezes











up vote
0
down vote

favorite












I have upgraded from Xamarin Forms 2.5 to 3.4 and I started getting a FAB button issue on Android only. The button appears right for the first time, but when I push and pop few times (on top of the page containing the button), the button suddenly shrinks around 40%



Here is my renderer code:



using Xamarin.Forms;

namespace MyApp.SLayout
{
[Preserve(AllMembers = true)]
public class SFloatingActionButton : Button
{
public static BindableProperty ButtonColorProperty = BindableProperty.Create(nameof(ButtonColor), typeof(Color),
typeof(SFloatingActionButton), Color.Accent);

public Color ButtonColor
{
get => (Color) GetValue(ButtonColorProperty);
set => SetValue(ButtonColorProperty, value);
}
}
}

using System;
using Android.Content;
using Android.Content.Res;
using Android.Graphics;
using Android.Runtime;
using Android.Support.V4.View;
using FFImageLoading;
using MyApp.Droid.SLayout;
using MyApp.SLayout;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using FAB = Android.Support.Design.Widget.FloatingActionButton;

[assembly: ExportRenderer(typeof(SFloatingActionButton), typeof(FloatingActionButtonRenderer))]
namespace MyApp.Droid.SLayout
{
[Preserve(AllMembers = true)]
public class FloatingActionButtonRenderer : Xamarin.Forms.Platform.Android.AppCompat.ViewRenderer<SFloatingActionButton, FAB>
{
private readonly Context _context;
private FAB _fab;
private Bitmap _img;

public FloatingActionButtonRenderer(Context context) : base(context)
{
_context = context;
}

protected override void OnElementChanged(ElementChangedEventArgs<SFloatingActionButton> e)
{
base.OnElementChanged(e);
if (Control == null)
{
// Instantiate the native control
_fab = new FAB(_context);
ViewCompat.SetBackgroundTintList(_fab, ColorStateList.ValueOf(Element.ButtonColor.ToAndroid()));
_fab.UseCompatPadding = true;
string imageFile = Element.Image.File;
string drawableNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(imageFile)?.ToLower();
int imageResourceName = _context.Resources.GetIdentifier(drawableNameWithoutExtension, "drawable", _context.PackageName);
_img = BitmapFactory.DecodeResource(_context.Resources, imageResourceName);
_fab.SetImageBitmap(_img);
SetNativeControl(_fab);
}

if (e.OldElement != null)
{
_img?.TryDispose();
_fab.Click -= Fab_Click;
}

if (e.NewElement != null)
{
_fab.Click += Fab_Click;
}
}


protected override void OnLayout(bool changed, int l, int t, int r, int b)
{
base.OnLayout(changed, l, t, r, b);
Control.BringToFront();
}

private void Fab_Click(object sender, EventArgs e)
{
// proxy the click to the element
((IButtonController) Element).SendClicked();
}
}
}









share|improve this question


























    up vote
    0
    down vote

    favorite












    I have upgraded from Xamarin Forms 2.5 to 3.4 and I started getting a FAB button issue on Android only. The button appears right for the first time, but when I push and pop few times (on top of the page containing the button), the button suddenly shrinks around 40%



    Here is my renderer code:



    using Xamarin.Forms;

    namespace MyApp.SLayout
    {
    [Preserve(AllMembers = true)]
    public class SFloatingActionButton : Button
    {
    public static BindableProperty ButtonColorProperty = BindableProperty.Create(nameof(ButtonColor), typeof(Color),
    typeof(SFloatingActionButton), Color.Accent);

    public Color ButtonColor
    {
    get => (Color) GetValue(ButtonColorProperty);
    set => SetValue(ButtonColorProperty, value);
    }
    }
    }

    using System;
    using Android.Content;
    using Android.Content.Res;
    using Android.Graphics;
    using Android.Runtime;
    using Android.Support.V4.View;
    using FFImageLoading;
    using MyApp.Droid.SLayout;
    using MyApp.SLayout;
    using Xamarin.Forms;
    using Xamarin.Forms.Platform.Android;
    using FAB = Android.Support.Design.Widget.FloatingActionButton;

    [assembly: ExportRenderer(typeof(SFloatingActionButton), typeof(FloatingActionButtonRenderer))]
    namespace MyApp.Droid.SLayout
    {
    [Preserve(AllMembers = true)]
    public class FloatingActionButtonRenderer : Xamarin.Forms.Platform.Android.AppCompat.ViewRenderer<SFloatingActionButton, FAB>
    {
    private readonly Context _context;
    private FAB _fab;
    private Bitmap _img;

    public FloatingActionButtonRenderer(Context context) : base(context)
    {
    _context = context;
    }

    protected override void OnElementChanged(ElementChangedEventArgs<SFloatingActionButton> e)
    {
    base.OnElementChanged(e);
    if (Control == null)
    {
    // Instantiate the native control
    _fab = new FAB(_context);
    ViewCompat.SetBackgroundTintList(_fab, ColorStateList.ValueOf(Element.ButtonColor.ToAndroid()));
    _fab.UseCompatPadding = true;
    string imageFile = Element.Image.File;
    string drawableNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(imageFile)?.ToLower();
    int imageResourceName = _context.Resources.GetIdentifier(drawableNameWithoutExtension, "drawable", _context.PackageName);
    _img = BitmapFactory.DecodeResource(_context.Resources, imageResourceName);
    _fab.SetImageBitmap(_img);
    SetNativeControl(_fab);
    }

    if (e.OldElement != null)
    {
    _img?.TryDispose();
    _fab.Click -= Fab_Click;
    }

    if (e.NewElement != null)
    {
    _fab.Click += Fab_Click;
    }
    }


    protected override void OnLayout(bool changed, int l, int t, int r, int b)
    {
    base.OnLayout(changed, l, t, r, b);
    Control.BringToFront();
    }

    private void Fab_Click(object sender, EventArgs e)
    {
    // proxy the click to the element
    ((IButtonController) Element).SendClicked();
    }
    }
    }









    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have upgraded from Xamarin Forms 2.5 to 3.4 and I started getting a FAB button issue on Android only. The button appears right for the first time, but when I push and pop few times (on top of the page containing the button), the button suddenly shrinks around 40%



      Here is my renderer code:



      using Xamarin.Forms;

      namespace MyApp.SLayout
      {
      [Preserve(AllMembers = true)]
      public class SFloatingActionButton : Button
      {
      public static BindableProperty ButtonColorProperty = BindableProperty.Create(nameof(ButtonColor), typeof(Color),
      typeof(SFloatingActionButton), Color.Accent);

      public Color ButtonColor
      {
      get => (Color) GetValue(ButtonColorProperty);
      set => SetValue(ButtonColorProperty, value);
      }
      }
      }

      using System;
      using Android.Content;
      using Android.Content.Res;
      using Android.Graphics;
      using Android.Runtime;
      using Android.Support.V4.View;
      using FFImageLoading;
      using MyApp.Droid.SLayout;
      using MyApp.SLayout;
      using Xamarin.Forms;
      using Xamarin.Forms.Platform.Android;
      using FAB = Android.Support.Design.Widget.FloatingActionButton;

      [assembly: ExportRenderer(typeof(SFloatingActionButton), typeof(FloatingActionButtonRenderer))]
      namespace MyApp.Droid.SLayout
      {
      [Preserve(AllMembers = true)]
      public class FloatingActionButtonRenderer : Xamarin.Forms.Platform.Android.AppCompat.ViewRenderer<SFloatingActionButton, FAB>
      {
      private readonly Context _context;
      private FAB _fab;
      private Bitmap _img;

      public FloatingActionButtonRenderer(Context context) : base(context)
      {
      _context = context;
      }

      protected override void OnElementChanged(ElementChangedEventArgs<SFloatingActionButton> e)
      {
      base.OnElementChanged(e);
      if (Control == null)
      {
      // Instantiate the native control
      _fab = new FAB(_context);
      ViewCompat.SetBackgroundTintList(_fab, ColorStateList.ValueOf(Element.ButtonColor.ToAndroid()));
      _fab.UseCompatPadding = true;
      string imageFile = Element.Image.File;
      string drawableNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(imageFile)?.ToLower();
      int imageResourceName = _context.Resources.GetIdentifier(drawableNameWithoutExtension, "drawable", _context.PackageName);
      _img = BitmapFactory.DecodeResource(_context.Resources, imageResourceName);
      _fab.SetImageBitmap(_img);
      SetNativeControl(_fab);
      }

      if (e.OldElement != null)
      {
      _img?.TryDispose();
      _fab.Click -= Fab_Click;
      }

      if (e.NewElement != null)
      {
      _fab.Click += Fab_Click;
      }
      }


      protected override void OnLayout(bool changed, int l, int t, int r, int b)
      {
      base.OnLayout(changed, l, t, r, b);
      Control.BringToFront();
      }

      private void Fab_Click(object sender, EventArgs e)
      {
      // proxy the click to the element
      ((IButtonController) Element).SendClicked();
      }
      }
      }









      share|improve this question













      I have upgraded from Xamarin Forms 2.5 to 3.4 and I started getting a FAB button issue on Android only. The button appears right for the first time, but when I push and pop few times (on top of the page containing the button), the button suddenly shrinks around 40%



      Here is my renderer code:



      using Xamarin.Forms;

      namespace MyApp.SLayout
      {
      [Preserve(AllMembers = true)]
      public class SFloatingActionButton : Button
      {
      public static BindableProperty ButtonColorProperty = BindableProperty.Create(nameof(ButtonColor), typeof(Color),
      typeof(SFloatingActionButton), Color.Accent);

      public Color ButtonColor
      {
      get => (Color) GetValue(ButtonColorProperty);
      set => SetValue(ButtonColorProperty, value);
      }
      }
      }

      using System;
      using Android.Content;
      using Android.Content.Res;
      using Android.Graphics;
      using Android.Runtime;
      using Android.Support.V4.View;
      using FFImageLoading;
      using MyApp.Droid.SLayout;
      using MyApp.SLayout;
      using Xamarin.Forms;
      using Xamarin.Forms.Platform.Android;
      using FAB = Android.Support.Design.Widget.FloatingActionButton;

      [assembly: ExportRenderer(typeof(SFloatingActionButton), typeof(FloatingActionButtonRenderer))]
      namespace MyApp.Droid.SLayout
      {
      [Preserve(AllMembers = true)]
      public class FloatingActionButtonRenderer : Xamarin.Forms.Platform.Android.AppCompat.ViewRenderer<SFloatingActionButton, FAB>
      {
      private readonly Context _context;
      private FAB _fab;
      private Bitmap _img;

      public FloatingActionButtonRenderer(Context context) : base(context)
      {
      _context = context;
      }

      protected override void OnElementChanged(ElementChangedEventArgs<SFloatingActionButton> e)
      {
      base.OnElementChanged(e);
      if (Control == null)
      {
      // Instantiate the native control
      _fab = new FAB(_context);
      ViewCompat.SetBackgroundTintList(_fab, ColorStateList.ValueOf(Element.ButtonColor.ToAndroid()));
      _fab.UseCompatPadding = true;
      string imageFile = Element.Image.File;
      string drawableNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(imageFile)?.ToLower();
      int imageResourceName = _context.Resources.GetIdentifier(drawableNameWithoutExtension, "drawable", _context.PackageName);
      _img = BitmapFactory.DecodeResource(_context.Resources, imageResourceName);
      _fab.SetImageBitmap(_img);
      SetNativeControl(_fab);
      }

      if (e.OldElement != null)
      {
      _img?.TryDispose();
      _fab.Click -= Fab_Click;
      }

      if (e.NewElement != null)
      {
      _fab.Click += Fab_Click;
      }
      }


      protected override void OnLayout(bool changed, int l, int t, int r, int b)
      {
      base.OnLayout(changed, l, t, r, b);
      Control.BringToFront();
      }

      private void Fab_Click(object sender, EventArgs e)
      {
      // proxy the click to the element
      ((IButtonController) Element).SendClicked();
      }
      }
      }






      c# xamarin.forms fab custom-renderer






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 at 3:34









      Adam

      1,38511638




      1,38511638
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          Xamarin.Forms 2.5 is a very old version, with many changes since 3.0.Here is a demo that you can refer.(This should be a comment, but my reputation is less than 50).






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423506%2fxamarin-forms-fab-icon-suddenly-squeezes%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            Xamarin.Forms 2.5 is a very old version, with many changes since 3.0.Here is a demo that you can refer.(This should be a comment, but my reputation is less than 50).






            share|improve this answer

























              up vote
              0
              down vote













              Xamarin.Forms 2.5 is a very old version, with many changes since 3.0.Here is a demo that you can refer.(This should be a comment, but my reputation is less than 50).






              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                Xamarin.Forms 2.5 is a very old version, with many changes since 3.0.Here is a demo that you can refer.(This should be a comment, but my reputation is less than 50).






                share|improve this answer












                Xamarin.Forms 2.5 is a very old version, with many changes since 3.0.Here is a demo that you can refer.(This should be a comment, but my reputation is less than 50).







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 23 at 6:55









                Akai Rika

                245




                245






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423506%2fxamarin-forms-fab-icon-suddenly-squeezes%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    What visual should I use to simply compare current year value vs last year in Power BI desktop

                    How to ignore python UserWarning in pytest?

                    Alexandru Averescu