title: Define default values for layout_width and layout_height properties for a subclass in a style
body: <p>Min and target SDK are 21 (Lollipop), no support library is used.</p>
  
  <p>I am trying to create a <a href="http://www.google.de/design/spec/components/buttons.html#buttons-flat-raised-buttons" rel="noreferrer">floating action button</a>. So far so good, it does indeed work (based on a few other SO threads), here is the code:</p>
  
  <ul>
  <li><p>attrs.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;resources&gt;
  
      &lt;attr name="fabStyle" format="reference"/&gt;
  
  &lt;/resources&gt;
  </code></pre></li>
  <li><p>styles.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;resources&gt;
  
      &lt;style name="MyTheme" parent="android:Theme.Material.Light.NoActionBar"&gt;
          &lt;item name="floatingActionButtonStyle"&gt;@style/FabStyle&lt;/item&gt;
      &lt;/style&gt;
  
      &lt;style name="FabStyle" parent="android:Widget.ImageButton"&gt;
          &lt;item name="android:background"&gt;@drawable/ripple&lt;/item&gt;
          &lt;item name="android:elevation"&gt;4dp&lt;/item&gt;
      &lt;/style&gt;
  </code></pre></li>
  </ul>
  
  <p>(I want the button to inherit default ImageButton characteristics, and it seems to work fine, is this the correct way?)</p>
  
  <ul>
  <li><p>the View code (notice the usage of the custom style attribute in the second constructor):</p>
  
  <pre><code>public class FloatingActionImageButton extends ImageButton {
  
      public FloatingActionImageButton(Context context) {
          this(context, null);
      }
  
      public FloatingActionImageButton(Context context, AttributeSet attrs) {
          this(context, attrs, R.attr.floatingActionButtonStyle);
      }
  
      public FloatingActionImageButton(Context context, AttributeSet attrs, int defStyleAttr) {
          this(context, attrs, defStyleAttr, 0);
      }
  
      public FloatingActionImageButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
          super(context, attrs, defStyleAttr, defStyleRes);
  
          setOutlineProvider(new ViewOutlineProvider() {
  
              @Override
              public void getOutline(View view, Outline outline) {
                  int width = view.getMeasuredWidth();
                  int height = view.getMeasuredHeight();
                  outline.setOval(0, 0, width, height);
              }
          });
          setClipToOutline(true);
      }
  }
  </code></pre></li>
  <li><p>ripple.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;ripple xmlns:android="http://schemas.android.com/apk/res/android"
          android:color="?android:attr/colorPrimary"&gt;
  
      &lt;item android:drawable="?android:attr/colorAccent"/&gt;
  
  &lt;/ripple&gt;
  </code></pre></li>
  <li><p>usage in XML:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  xmlns:tools="http://schemas.android.com/tools"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  tools:context="com.test.TestActivity"&gt;
  
      &lt;com.test.FloatingActionImageButton
          android:id="@+id/refresh_button"
          android:layout_width="@dimen/fab_size"
          android:layout_height="@dimen/fab_size"
          android:layout_alignParentBottom="true"
          android:layout_alignParentEnd="true"
          android:layout_marginBottom="@dimen/padding_big"
          android:layout_marginEnd="@dimen/padding_big"
          android:src="@drawable/ic_action_restart"/&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre></li>
  </ul>
  
  <p>(@dimen/fab_size is 56dp)</p>
  
  <p>The result is a nicely looking elevated FAB, with ripple effect working as intended:</p>
  
  <p><img src="https://i.stack.imgur.com/ba8hx.png" alt="fab"></p>
  
  <ol>
  <li><p>As this is my first such attempt at styling etc. in Android, I would like to ask if what I'm doing is 'canon'. How would I go about it if I wanted to publish my very own Fab-ulous library? Is the styling code now ready for potential clients of my lib to just define the fabStyle somehow (how?) in their styles.xml and my View would use the settings?</p></li>
  <li><p>What I don't like about my solution is that although I would like all FABs to be 56dp, as suggested by the Google in the document linked above, I have to define the size whenever the fab is used in XML. As I would like the button to default to 56dp, I tried putting the layout_width and _height in the FabStyle and removing from the XML, but AndroidStudio says I should set them, and the application actually crashes at runtime saying the values need to be set.</p></li>
  </ol>
  
  <p>I tried setting @null in the XML, which crashed, and 0dp/px, which just made the button, well, invisible...</p>
  
  <p>Is it possible to define default values for these attributes, or am I out of luck here?</p>