title: Android Extends Button - Accessing Members from Java to XML
body: <p>I'm new to android programming and would like to extend a button class so that I can add a custom field to the button, and then change the way the button is displayed dependent on the field. I've done a few tutorials, had a good bash around and have something that compiles, but doesn't work - so I'm looking for tips on where I have gone wrong.</p>
  
  <p>What I'd like to achieve is more complex than my example, but once I can get this example to work I think I would have a good crack at getting the rest up and going.</p>
  
  <p>For my example, I'd like a button that has a isRed boolean member/attribute. If the member is set, then we change a drawable rectangle on the button to a red colour, if it is false, then the drawable is green.</p>
  
  <p>To do this I have:</p>
  
  <ul>
  <li>created a redbutton.xml file in my res/drawable directory. This file consists of a selector that provides either red or green rectangle, dependent on the isRed state.</li>
  <li>created a RedButton.java file in my src/com.example.redbutton directory. This file contains a RedButton class that extends Button</li>
  <li>created a attrs.xml file in my res/values directory. This lists the isRed member/attributes</li>
  <li>put two buttons in my main layout. One button has custom:isRed set to true, the other has isRed set to false.</li>
  </ul>
  
  <p>In theory I should then have two buttons in my app, one red, one green. Unfortunately I have two green buttons! I cannot seem to get the </p>
  
  <pre><code>&lt;item custom:isRed="true"&gt; 
  </code></pre>
  
  <p>in redbutton.xlm to evaluate to true. However, If I change this line to be </p>
  
  <pre><code>&lt;item android:state_pressed="true"&gt;
  </code></pre>
  
  <p>then i have a red button whenever I press the button. To me the problem seems to be that accessing my custom field always evaluates to false! </p>
  
  <p>Below is the code that I have written. Can anyone give me some tips on what I have done wrong?</p>
  
  <p><strong>redbutton.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;selector xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:custom="http://schemas.android.com/apk/res/com.example.redbutton" &gt;
  
  &lt;!-- if the isred value is true, then make the background red --&gt;
  &lt;!--    &lt;item android:state_pressed="true"&gt; --&gt; 
       &lt;item custom:isred="true" &gt;  
               &lt;shape android:shape="rectangle"  &gt;
               &lt;size android:width="100dp"
                  android:height="100dp"/&gt;
               &lt;solid  android:color="#ff0000"/&gt;             
           &lt;/shape&gt;
       &lt;/item&gt;
  &lt;!-- otherwise make it green --&gt;
      &lt;item &gt;
          &lt;shape android:shape="rectangle"  &gt;
               &lt;size android:width="100dp"
                  android:height="100dp"/&gt;
              &lt;solid  android:color="#00ff00"/&gt;       
           &lt;/shape&gt;
       &lt;/item&gt;
  &lt;/selector&gt;  
  </code></pre>
  
  <p><strong>RedButton.java</strong></p>
  
  <pre><code>package com.example.redbutton;
  import android.content.Context;
  import android.content.res.TypedArray;
  import android.util.AttributeSet;
  import android.widget.Button;
  
  public class RedButton extends Button
  {
  
      private boolean mIsRed;
  
      public RedButton (Context context, AttributeSet attrs) {
          super(context, attrs);
  
          TypedArray a = context.getTheme().obtainStyledAttributes(
                  attrs,
                  R.styleable.RedButton,
                  0, 0);
  
             try {
                 mIsRed = a.getBoolean(R.styleable.RedButton_isred, false);
             } finally {
                 a.recycle();
             }
      }
  
  
      //sets and gets for members
      public boolean getIsRed() 
      {
          return mIsRed;
      }
  
      public void setIsRed(boolean isRed) 
      {
          mIsRed =  isRed;
      }
  }
  </code></pre>
  
  <p><strong>attrs.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;resources&gt;
      &lt;declare-styleable name="RedButton"&gt;
          &lt;attr name="isred" format="boolean" /&gt;
      &lt;/declare-styleable&gt;
  &lt;/resources&gt; 
  </code></pre>
  
  <p><strong>fragment_main.xml</strong></p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:custom="http://schemas.android.com/apk/res/com.example.redbutton"
      xmlns:tools="http://schemas.android.com/tools"
  
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      tools:context="com.example.redbutton.MainActivity$PlaceholderFragment" &gt;
  
      &lt;com.example.redbutton.RedButton 
                  android:id="@+id/Button1"
                  android:layout_width="0dp"
                  android:layout_height="fill_parent"
                  android:layout_weight="1"
                  android:text="RedButton"
                  android:textSize="30sp" 
                  custom:isred="true" 
                  android:drawableBottom="@drawable/redbutton"              
      /&gt;
      &lt;com.example.redbutton.RedButton 
  
                  android:id="@+id/Button2"
                  android:layout_width="0dp"
                  android:layout_height="fill_parent"
                  android:layout_weight="1"
                  android:text="GreenButton"
                  android:textSize="30sp" 
                  custom:isred="false"              
                  android:drawableBottom="@drawable/redbutton"
      /&gt;
  &lt;/LinearLayout&gt;
  </code></pre>