title: Cascade the value of a custom attribute from a parent view to a child view?
body: <p>How do I "cascade" the value of a custom attribute from a parent view to its child view?</p>
  
  <p>This is easiest to explain using an example:</p>
  
  <pre><code>&lt;com.example.CustomLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      app:percent="35" &gt;
  
      &lt;com.example.CustomView
          android:id="@+id/customView1"
          app:percent="how-to-get-app:percent-value-here???"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" /&gt;
  
  &lt;/com.example.CustomLayout&gt;
  </code></pre>
  
  <p>Here, <code>CustomLayout</code> extends <code>LinearLayout</code>. I have defined a custom attribute "percent" in <code>attrs.xml</code> using <code>&lt;declare-styleable&gt;</code> element. As you can see, I set percent to 35 in the XML for <code>CustomLayout</code>.</p>
  
  <p>I now want to pass in the same value to <code>CustomView</code> (which extends <code>View</code>) and which I will include in <code>CustomLayout</code>. I am unable to find a way to do this in XML (it is easy to do this in code though).</p>
  
  <p>I tried the following:</p>
  
  <p><code>app:percent="@attr/percent"</code></p>
  
  <p><code>app:percent="?attr/percent"</code></p>
  
  <p>Both of these (expectedly) fail with <code>NumberFormatException</code> at <code>TypedArray#getInt()</code>.</p>
  
  <p>So, any ideas on how to get this to work?</p>