Reputation: 2285
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?
Upvotes: 0
Views: 1281
Reputation: 10971
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&lt;Xamarin.Forms.Button&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/
Upvotes: 1