0

I need a button to which I can set custom font and color. I created a custom renderer for the font, and set the BackgroundColor to whichever color I need, but the problem is that when I disable the button, the background color doesn't change. I found this solution but I have no idea how to use it in my Xamarin Forms app. Any suggestions?

Community
  • 1
  • 1
nicks
  • 2,161
  • 8
  • 49
  • 101

1 Answers1

1

You can save the state list selector in your Resources/drawable directory with a name like button_bg.xml. Then in your layout xml file you can set your button's background like this

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_bg"/>

Please check this guide on using resources:https://developer.xamarin.com/guides/android/application_fundamentals/resources_in_android/part_1_-_android_resource_basics/

EDIT:

The above solution is for classic Xamarin Android, for Xamarin Forms you can achieve the same by setting the background to the button within the custom renderer class as described here: http://dailydotnettips.com/2016/03/05/applying-styles-for-xamarin-forms-control-in-android-from-xml-file/

public class CustomButtonRenderer : ButtonRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs&amp;lt;Xamarin.Forms.Button&amp;gt; e)
{
base.OnElementChanged(e);
var btn = this.Control as Android.Widget.Button;
btn?.SetBackgroundResource(Resource.Drawable.Style);
}
}

Another approach is to use Xamarin Forms global styles as described in the guide: https://developer.xamarin.com/guides/xamarin-forms/user-interface/styles/application/

Also there is Xamarin Dynamic Styles which enables you to change styles in runtom: https://developer.xamarin.com/guides/xamarin-forms/user-interface/styles/dynamic/

Mina Wissa
  • 10,923
  • 13
  • 90
  • 158
  • I am using Xamarin Forms though – nicks Aug 06 '16 at 11:11
  • Can I pass values to that xml? – nicks Aug 06 '16 at 11:22
  • You can define the colors for the different states as color resources in your Resources/values file, and reference them in your state list drawable – Mina Wissa Aug 06 '16 at 11:24
  • i'm trying to make a universal button. I want to be able to set just the color from xaml and then calculate the disabled state color and then apply them all in a button renderer. – nicks Aug 06 '16 at 11:25
  • I believe Dynamic Styles can do this, though not sure as I didn't use it before – Mina Wissa Aug 06 '16 at 11:33
  • weird. there clearly is an entry `custom_button` in `Resource.Drawable`, the xml file is present in Resrouces\drawable, build aciton is set to `Android Resource` and despite that, I'm getting an error "Android.Content.Res.Resources NotFoundException..." – nicks Aug 06 '16 at 12:08
  • Well, this is another issue, is that a runtime exception? make sure you can reference the file from code through Resource.Drawable.custom_button. otherwise check if the xml file has errors – Mina Wissa Aug 07 '16 at 11:23
  • Great, glad to hear that – Mina Wissa Aug 07 '16 at 11:58