1

I am learning Xamarin Android, and I have a xml file that I want to use for customizing a button. But whenever I "apply" the xml file to my button, the button disappear. Here is my xml file:

<selector xmlns:android="https://schemas.android.com/apk/res/android">
  <item android:state_pressed="false">
    <layer-list>
    <item android:right="5dp" android:top="5dp">
      <shape>
        <corners android:radius="2dp"/>
        <solid android:color="#D6D6D6"/>
      </shape>
    </item>
    <item android:bottom ="2dp" android:left="2dp">
      <shape>
        <gradient android:angle="270" android:endColor="#4A6EA9" android:startColor="4A6EA9"/>
        <stroke android:width="1dp" android:color="#BABABA"/>
        <corners android:radius="4dp"/>
        <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp"/>
      </shape>
    </item>
    </layer-list>
  </item>
  <item android:state_pressed="true">
    <layer-list>
      <item android:right="5dp" android:top="5dp">
        <shape>
          <corners android:radius="2dp"/>
          <solid android:color="#D6D6D6"/>
        </shape>
      </item>
      <item android:bottom ="2dp" android:left="2dp">
        <shape>
          <gradient android:angle="270" android:endColor="7C97C1" android:startColor="4A6EA9"/>
          <stroke android:width="1dp" android:color="#BABABA"/>
          <corners android:radius="4dp"/>
          <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp"/>
        </shape>
      </item>
    </layer-list>
  </item>
</selector>

And here is my Button code in the layout:

    <Button
    android:text="Log In"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="10"
    android:textSize="15dp"
    android:background="@drawable/SignInButtonStyle"
    android:layout_marginLeft="30dp"
    android:layout_marginRight="30dp"
    android:id="@+id/IDLI" />

Does anyone knows why is this happening? Thanks

Raducu Mihai
  • 313
  • 4
  • 14

2 Answers2

1

If your resource file contains capital letters in filename, it should be changed into lowercase.

android:background="@drawable/SignInButtonStyle"

Change your real filename to signinbuttonstyle.xml and change your code:

android:background="@drawable/signinbuttonstyle"

See below: Capital letters not allowed in android res

Community
  • 1
  • 1
Stanley Ko
  • 3,383
  • 3
  • 34
  • 60
  • It does not work. If it helps I am following this tutorial https://www.youtube.com/watch?v=RBQeTUNu_Kk&list=PLCuRg51-gw5VqYchUekCqxUS9hEZkDf6l&index=7 – Raducu Mihai Feb 29 '16 at 08:37
1

You have a few issues in your drawable definition:

  • 1: xmlns:android="https://schemas.android.com/apk/res/android" must be xmlns:android="http://schemas.android.com/apk/res/android". Drop the "s" on the https.
  • 2: Several of your colors are just integer numbers. They need a # on the start of them to be valid.

Below is the corrected xml:

<?xml version="1.0" encoding="UTF-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_pressed="false">
    <layer-list>
        <item android:right="5dp" android:top="5dp">
          <shape>
            <corners android:radius="2dp"/>
            <solid android:color="#D6D6D6"/>
          </shape>
        </item>
        <item android:bottom ="2dp" android:left="2dp">
          <shape>
            <gradient android:angle="270" android:endColor="#4A6EA9" android:startColor="#4A6EA9"/>
            <stroke android:width="1dp" android:color="#BABABA"/>
            <corners android:radius="4dp"/>
            <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp"/>
          </shape>
        </item>
    </layer-list>
  </item>
  <item android:state_pressed="true">
    <layer-list>
      <item android:right="5dp" android:top="5dp">
        <shape>
          <corners android:radius="2dp"/>
          <solid android:color="#D6D6D6"/>
        </shape>
      </item>
      <item android:bottom ="2dp" android:left="2dp">
        <shape>
          <gradient android:angle="270" android:endColor="#7C97C1" android:startColor="#4A6EA9"/>
          <stroke android:width="1dp" android:color="#BABABA"/>
          <corners android:radius="4dp"/>
          <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp"/>
        </shape>
      </item>
    </layer-list>
  </item>
</selector>

(Self promotion disclaimer) I found these issues using the resource static analyser that's included in MFractor, the tooling I build for Xamarin Studio. It's designed to immediately highlight errors like this.

matthewrdev
  • 11,930
  • 5
  • 52
  • 64