Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

My custom Android buttons are rendered differently on different pages. Is it a Forms bug?

$
0
0

I have ported an Android app to Xamarin Forms.

I have a Tabbed page with two sub-pages.
The first tab page renders my custom buttons correctly. On the second tab page, the buttons are too small. Same is true for all subsequent pages created.
The buttons on every page except the first tab are sized as if the button is not a custom button, but they are rendered using my custom style.
I have tried adding two pages that are exactly the same - same class, same layout code, same problem.

Here is the code form my custom renderer:

[assembly: ExportRendererAttribute(typeof(Xamarin.Forms.Button), typeof(MyNamespace.Android.MyButtonRenderer))]
namespace MyNamespace.Android {

public class MyButtonRenderer : ButtonRenderer
{
    public MyButtonRenderer() : base()
    {
        SetWillNotDraw(false);
    }

    protected override void OnAttachedToWindow()
    {
        base.OnAttachedToWindow();

        int id = Resource.Drawable.my_large_button;
        StateListDrawable drawable = (StateListDrawable)Context.Resources.GetDrawable(id);
        var button = (global::Android.Widget.Button)Control;
        button.SetBackgroundDrawable(drawable);
        button.SetTextSize(global::Android.Util.ComplexUnitType.Dip, 30);
        button.SetTextColor(global::Android.Graphics.Color.White);
    }
}

This code is being called for every button on every page displayed, but only the first tab of the TabbedPage is rendered correctly.
Any clues as to what's going on?

What's unusual about this code is that I hook into OnAttachedToWindow. I couldn't find any other place to change the style that worked.
I have a lot of Android specific xml styling and I want to avoid rewriting it if possible.

Am I doing something wrong? Is there any other way to do this?

I tried changing the default button style in my android theme, but the button style from the Android theme seems to be ignored by Xamarin Forms.
The background colour and other aspects of the theme work correctly.

Any suggestions would be appreciated.


Viewing all articles
Browse latest Browse all 58056

Trending Articles