Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error inflating class android.widget.Button with custom selector

I am trying to style a button in an android view, but since adding a custom selector I get the following error, what is causing this?:

Android.Views.InflateException: Binary XML file line #1: Error inflating class android.widget.Button

View causing grief

<Button
    android:text="Next"
    android:id="@+id/nextButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    style="@style/primary_button_style"
    android:textColor="@color/button_primary"
    android:background="@color/button_primary" />

res/color/button_primary.xml

<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item
    android:color="@color/accent"
    android:state_enabled="true"
    />
  <item
    android:color="@color/grey"
    android:state_enabled="false"
    />
</selector>

res/values/Styles.xml

  <style name="primary_button_style" parent="android:Widget.Button">
    <item name="android:paddingLeft">50px</item>
    <item name="android:paddingRight">50px</item>
    <item name="android:height">50px</item>
    <item name="android:minHeight">50px</item>
    <item name="android:maxHeight">50px</item>
    <item name="android:textAllCaps">true</item>
    <item name="android:textSize">30px</item>
    <item name="android:textStyle">bold</item>
  </style>

I am using Xamarin in Visual Studio

like image 829
tallpaul Avatar asked Jul 18 '26 10:07

tallpaul


1 Answers

the selector you posted is fine for the text color, not for the background. You should add one in the folder drawable/ and use android:drawable instead of android:color

<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item
    android:drawable="@color/accent"
    android:state_enabled="true"
    />
  <item
    android:drawable="@color/grey"
    android:state_enabled="false"
    />
</selector>

btw. if you use the same colors for the background and for the text you will not be able to see the latter

like image 129
Blackbelt Avatar answered Jul 20 '26 22:07

Blackbelt