3

In my application, the AppCompat Theme is working fine created using steps here. I want to apply RaisedButton color in Xamarin.Forms Button. For this I have tried following:

  • Added style in MyApplication.Droid > Resources > Values> style.xml refering as:

    <style name="MyRaisedButton" parent="Widget.AppCompat.Button.Colored"></style>

  • Added a button in MyApplication.Shared Portable form library in view MyView.xaml:

    <Button Style="{StaticResource MyRaisedButton}" Text="Raised Button" />

But its not working. As these two files (MyView.xaml and style.xml) are in different projects. How can I add buttons in Xamarin.Forms with Widget.AppCompat.Button.Colored style?

Community
  • 1
  • 1
Vishnu
  • 2,135
  • 2
  • 30
  • 51

1 Answers1

4

You can do this with a custom ButtonRenderer added to your Xamarin.Android project:

enter image description here

new Button {
    WidthRequest = 50,
    Text = "Button"
},
new RaisedButton {
    WidthRequest = 50,
    Text = "RaisedButton"
}

layout/raisedbutton.axml (customize to your liking)

Note: Link it to a style id if desired and edit your style.xml

<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/raisedbutton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="StackOverflow"
    android:textAllCaps="false"
    style="@style/Base.Widget.AppCompat.Button.Colored"
    android:gravity="center" />

RaisedButton.cs (In PCL project):


using Xamarin.Forms;

namespace MaterialDesignButton
{
    public class RaisedButton : Button
    {
    }
}

RaisedButtonRender.cs (In Android project)


using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using MaterialDesignButton;
using MaterialDesignButton.Droid;

[assembly: ExportRenderer(typeof(RaisedButton), typeof(RaisedButtonRender))]
namespace MaterialDesignButton.Droid
{
    public class RaisedButtonRender : ButtonRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
        {
            if (Control == null)
            {
                var raisedButton = (Android.Widget.Button)Inflate(Context, Resource.Layout.raisedbutton, null);

                //Additional code: To make it generic               
                if (e.NewElement != null)
                {
                    raisedButton.Text = e.NewElement.Text;
                    raisedButton.Click += (s, ar) =>
                    {
                        if (e.NewElement.Command.CanExecute(null))
                        {
                            e.NewElement.Command.Execute(null);
                        }
                    };
                }

                SetNativeControl(raisedButton);
            }
            base.OnElementChanged(e);
        }
    }
}

Ref: https://developer.xamarin.com/guides/xamarin-forms/custom-renderer/

Vishnu
  • 2,135
  • 2
  • 30
  • 51
SushiHangover
  • 73,120
  • 10
  • 106
  • 165
  • This will replace all buttons in app with RaisedButton and I need to use this style for specific once. – Vishnu Apr 27 '16 at 13:19
  • @Vishnu No it would not. you registering `CustomButtonRenderer` for your `Xamarin.Form` `Button` subclass only – SushiHangover Apr 27 '16 at 13:22
  • I tried this solution, creating `class RaisedButton:Xamarin.Forms.Button` and then adding a custom renderer in .Droid project with following code. There is RunTime error in below line, I may be missing some reference: `button.SetBackgroundResource(Resource.Style.Widget_AppCompat_Button_Colored);` Exception occurred: `Exception: android.content.res.Resources$NotFoundException` – Vishnu Apr 28 '16 at 06:09
  • @Vishnu I stripped down a version that I am using and updated the answer – SushiHangover Apr 28 '16 at 08:33
  • @SushilHangover Can you please provide the reference for Base.Widget.AppCompat.Button.Colored list? – Vishnu May 03 '16 at 12:41
  • @Vishnu Reference: `Base.Widget.AppCompat.Button.Colored` would be in `Xamarin.Android.v7.AppCompat` – SushiHangover May 03 '16 at 12:49
  • @SushilHangover, sorry, I was asking if any reference link on Google/SO/Xamarin is there for this list values like `Base.Widget.AppCompat.Button.Colored` – Vishnu May 03 '16 at 13:08
  • 1
    @Vishnu :-) Ok, for a master listing of IDs for `android.support.v7.appcompat.R.style` I use http://developer.android.com/reference/android/support/v7/appcompat/R.style.html It is easy to remove the "_" from google's constants to get the C# style name... Is that what you are looking for? – SushiHangover May 03 '16 at 13:14
  • @SushilHangover, Yes. Thank you. – Vishnu May 03 '16 at 13:16
  • @Vishnu Great, happy coding :-) – SushiHangover May 03 '16 at 13:26