title: How can I give my widgets' width a percentage rather than an explicit value?
body: <p>Based on how XAML works, I thought I could give my android widgets a percentage value for width. After searching, I found that *theoretically," this is available via the "layout_weight" property. But using this does not produce the desired appearance. Specifically, this xml:</p>
  
  <pre><code>&lt;LinearLayout
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:orientation="horizontal"
      android:padding="5dp"&gt;
  
      &lt;Spinner
          android:id="@+id/spinnerUPCPLU"
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_weight=".40"
          android:entries="@array/delivery_upcplu_spinner" /&gt;
  
      &lt;LinearLayout
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:background="@drawable/greyframe"
          android:orientation="horizontal"&gt;
  
          &lt;EditText
              android:id="@+id/editTextUPCPLU"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_weight=".90"
              android:editable="false" /&gt;
      &lt;/LinearLayout&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>...gives me this:</p>
  
  <p><img src="https://i.stack.imgur.com/CTjtu.png" alt="enter image description here"></p>
  
  <p>I can brute-force the layout to look more-or-less as I want it using explicit width values, like so:</p>
  
  <pre><code>&lt;LinearLayout
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:orientation="horizontal"
      android:padding="5dp"&gt;
  
      &lt;Spinner
          android:id="@+id/spinnerUPCPLU"
          android:layout_width="160dp"
          android:layout_height="40dp"
          android:entries="@array/delivery_upcplu_spinner" /&gt;
  
      &lt;LinearLayout
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:background="@drawable/greyframe"
          android:orientation="horizontal"&gt;
  
          &lt;EditText
              android:id="@+id/editTextUPCPLU"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:editable="false"
              android:minWidth="200dp" /&gt;
      &lt;/LinearLayout&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>...which looks like this:</p>
  
  <p><img src="https://i.stack.imgur.com/86npC.png" alt="enter image description here"></p>
  
  <p>...but doing this (assigning specific dp vals to the width) makes me more nervous than a cat in a roomful of rocking chairs.</p>
  
  <p>Is there balsam in Gilead? I mean, is there, after all, a way to assign percentage widths (that actually works, of course)?</p>
  
  <p>Am I using layout_weight wrong, or am I using the wrong methodology, or is it (perish the thought) impossible?</p>
  
  <h2>UPDATE</h2>
  
  <p>The accepted answer worked in one instance, but in the next:</p>
  
  <p></p>
  
  <pre><code>        &lt;TextView
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_weight=".25"
              android:text="@string/id"
              android:textAppearance="?android:attr/textAppearanceMedium" /&gt;
  
          &lt;EditText
              android:id="@+id/editTextID"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_weight=".25" /&gt;
  
          &lt;TextView
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_weight=".25"
              android:text="@string/pack_size"
              android:textAppearance="?android:attr/textAppearanceMedium" /&gt;
  
          &lt;EditText
              android:id="@+id/editTextPackSize"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_weight=".25" /&gt;
      &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>...it doesn't:</p>
  
  <p><img src="https://i.stack.imgur.com/UaBQo.png" alt="enter image description here"></p>
  
  <p>It takes up half the width instead of all of it; must I put some "replace" talk in the EditText widgets for them to "widen out"?</p>
  
  <p>I can "force it" by adding to the EditTexts:</p>
  
  <pre><code>android:minWidth="120dp"
  </code></pre>
  
  <p>...but that returns me to the nervosity level of the rocking-chair-room cat.</p>
  
  <h2>UPDATE 2</h2>
  
  <p>Now even what I thought was working is not, or no longer. The xml is this:</p>
  
  <p></p>
  
  <pre><code>&lt;LinearLayout 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"
      android:orientation="vertical"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="4dp"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      tools:context="hhs.app.DeliveryActivity"&gt;
  
      &lt;!--Row 0--&gt;
      &lt;LinearLayout
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:orientation="horizontal"
          android:padding="5dp"
          &gt;
          &lt;Spinner
              android:id="@+id/spinnerUPCPLU"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_weight=".10"
              android:maxWidth="12dp"
              android:entries="@array/delivery_upcplu_spinner"
              /&gt;
          &lt;EditText
              android:id="@+id/editTextUPCPLU"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_weight=".90"
              android:minWidth="1200dp"
              android:editable="false"
              /&gt;
      &lt;/LinearLayout&gt;
  
      &lt;!--Row 1--&gt;
      &lt;LinearLayout
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:orientation="horizontal"
          android:padding="5dp"&gt;
  
          &lt;TextView
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_weight=".25"
              android:text="@string/id"
              android:textAppearance="?android:attr/textAppearanceMedium" /&gt;
  
          &lt;LinearLayout
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:background="@drawable/greyframe"
              android:orientation="horizontal"&gt;
          &lt;EditText
              android:id="@+id/editTextID"
              android:layout_width="0dp"
              android:minWidth="120dp"
              android:layout_height="wrap_content"
              android:layout_weight=".25" /&gt;
          &lt;/LinearLayout&gt;
  
          &lt;TextView
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_weight=".25"
              android:text="@string/pack_size"
              android:textAppearance="?android:attr/textAppearanceMedium" /&gt;
  
              &lt;LinearLayout
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:background="@drawable/greyframe"
                  android:orientation="horizontal"&gt;
          &lt;EditText
              android:id="@+id/editTextPackSize"
              android:layout_width="0dp"
              android:minWidth="120dp"
              android:layout_height="wrap_content"
              android:layout_weight=".25" /&gt;
              &lt;/LinearLayout&gt;
      &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>...and it looks like this:</p>
  
  <p><img src="https://i.stack.imgur.com/DN1Av.png" alt="enter image description here"></p>
  
  <p>IOW, in the first row, the EditText is getting scrunched like a svelte trainer by an age-old elephant, and in the next row, the attempted equality of widths is not being observed (although it actually looks fine, those labels are <em>not</em> taking one-quarter of the width, but only what they need and no more).</p>
  
  <p>And, the same is the case when I change ".25" to either "0.25" or "1" in all cases.</p>
  
  <h2>UPDATE 3</h2>
  
  <p>Okay, here is what I see with my LinearLayout with various combinations of "match_parent" and "wrap_content" for its "layout_width" and "layout_height" properties.</p>
  
  <p>When both are set to wrap_content:</p>
  
  <p><img src="https://i.stack.imgur.com/vC6rn.png" alt="enter image description here"></p>
  
  <p>When both are set to match_parent:</p>
  
  <p><img src="https://i.stack.imgur.com/9len4.png" alt="enter image description here"></p>
  
  <p>If width is set to match_parent, and height is set to wrap_content, it is the same as when BOTH are set to match_parent
  The other way around (with height set to match_parent and width set to wrap_content), it is the same as when BOTH are set to wrap_content</p>
  
  <h2>UPDATE 4</h2>
  
  <p>Here is the entire contents of this particular layout file, at its best, albeit not perfect by any means:</p>
  
  <pre><code>&lt;ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
  
      &lt;LinearLayout 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"
          android:orientation="vertical"
          android:paddingBottom="@dimen/activity_vertical_margin"
          android:paddingLeft="4dp"
          android:paddingRight="@dimen/activity_horizontal_margin"
          android:paddingTop="@dimen/activity_vertical_margin"
          tools:context="hhs.app.DeliveryActivity"&gt;
  
          &lt;!--Row 0--&gt;
          &lt;LinearLayout
              android:layout_width="wrap_content"
              android:layout_height="match_parent"
              android:orientation="horizontal"
              android:padding="5dp"
              &gt;
              &lt;Spinner
                  android:id="@+id/spinnerUPCPLU"
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_weight=".10"
                  android:maxWidth="12dp"
                  android:entries="@array/delivery_upcplu_spinner"
                  /&gt;
              &lt;EditText
                  android:id="@+id/editTextUPCPLU"
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_weight=".90"
                  android:minWidth="1200dp"
                  android:editable="false"
                  /&gt;
          &lt;/LinearLayout&gt;
  
          &lt;!--Row 1--&gt;
          &lt;LinearLayout
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:orientation="horizontal"
              android:padding="5dp"&gt;
  
              &lt;TextView
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_weight="25"
                  android:text="@string/id"
                  android:textAppearance="?android:attr/textAppearanceMedium" /&gt;
  
              &lt;LinearLayout
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:background="@drawable/greyframe"
                  android:orientation="horizontal"&gt;
              &lt;EditText
                  android:id="@+id/editTextID"
                  android:layout_width="0dp"
                  android:minWidth="120dp"
                  android:layout_height="wrap_content"
                  android:layout_weight="25" /&gt;
              &lt;/LinearLayout&gt;
  
              &lt;TextView
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_weight="25"
                  android:text="@string/pack_size"
                  android:textAppearance="?android:attr/textAppearanceMedium" /&gt;
  
                  &lt;LinearLayout
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:background="@drawable/greyframe"
                      android:orientation="horizontal"&gt;
              &lt;EditText
                  android:id="@+id/editTextPackSize"
                  android:layout_width="0dp"
                  android:minWidth="120dp"
                  android:layout_height="wrap_content"
                  android:layout_weight="25" /&gt;
                  &lt;/LinearLayout&gt;
          &lt;/LinearLayout&gt;
  
          &lt;!--Row 2--&gt;
          &lt;LinearLayout
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:orientation="horizontal"
              android:padding="5dp"&gt;
  
              &lt;TextView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="@string/desc"
                  android:textAppearance="?android:attr/textAppearanceMedium" /&gt;
  
              &lt;LinearLayout
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:background="@drawable/greyframe"
                  android:orientation="horizontal"&gt;
  
                  &lt;EditText
                      android:id="@+id/editTextDesc"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:minWidth="320dp" /&gt;
              &lt;/LinearLayout&gt;
  
          &lt;/LinearLayout&gt;
  
          &lt;!--Row 3--&gt;
          &lt;LinearLayout
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:orientation="horizontal"
              android:padding="5dp"&gt;
  
              &lt;TextView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="@string/qty"
                  android:textAppearance="?android:attr/textAppearanceMedium" /&gt;
  
              &lt;LinearLayout
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:background="@drawable/orangeframe"
                  android:orientation="horizontal"&gt;
  
                  &lt;EditText
                      android:id="@+id/editTextQty"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:minWidth="144dp" /&gt;
              &lt;/LinearLayout&gt;
  
              &lt;TextView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="@string/count"
                  android:textAppearance="?android:attr/textAppearanceMedium" /&gt;
  
              &lt;LinearLayout
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:background="@drawable/greyframe"
                  android:orientation="horizontal"&gt;
  
                  &lt;EditText
                      android:id="@+id/editTextCount"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:editable="false"
                      android:minWidth="144dp" /&gt;
              &lt;/LinearLayout&gt;
  
          &lt;/LinearLayout&gt;
  
          &lt;!--Row 4--&gt;
          &lt;LinearLayout
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:orientation="horizontal"
              android:padding="5dp"&gt;
  
              &lt;TextView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="@string/cost"
                  android:textAppearance="?android:attr/textAppearanceMedium" /&gt;
  
              &lt;LinearLayout
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:background="@drawable/orangeframe"
                  android:orientation="horizontal"&gt;
  
                  &lt;EditText
                      android:id="@+id/editCost"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:minWidth="48dp" /&gt;
              &lt;/LinearLayout&gt;
  
              &lt;TextView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="@string/margin"
                  android:textAppearance="?android:attr/textAppearanceMedium" /&gt;
  
              &lt;LinearLayout
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:background="@drawable/orangeframe"
                  android:orientation="horizontal"&gt;
  
                  &lt;EditText
                      android:id="@+id/editTextMargin"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:minWidth="48dp" /&gt;
              &lt;/LinearLayout&gt;
  
              &lt;TextView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="@string/list"
                  android:textAppearance="?android:attr/textAppearanceMedium" /&gt;
  
              &lt;LinearLayout
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:background="@drawable/orangeframe"
                  android:orientation="horizontal"&gt;
  
                  &lt;EditText
                      android:id="@+id/editList"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:minWidth="48dp" /&gt;
              &lt;/LinearLayout&gt;
  
          &lt;/LinearLayout&gt;
  
          &lt;!--Row 5--&gt;
          &lt;LinearLayout
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:orientation="horizontal"
              android:padding="5dp"&gt;
  
              &lt;TextView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="@string/dept"
                  android:textAppearance="?android:attr/textAppearanceMedium" /&gt;
  
              &lt;LinearLayout
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:background="@drawable/greyframe"
                  android:orientation="horizontal"&gt;
  
                  &lt;Spinner
                      android:id="@+id/spinnerDept"
                      android:layout_width="180dp"
                      android:layout_height="40dp"
                      android:entries="@array/departments" /&gt;
              &lt;/LinearLayout&gt;
  
              &lt;TextView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="$"
                  android:textAppearance="?android:attr/textAppearanceMedium" /&gt;
  
              &lt;LinearLayout
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:background="@drawable/greyframe"
                  android:orientation="horizontal"&gt;
  
                  &lt;EditText
                      android:id="@+id/editTextDollar"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:minWidth="48dp" /&gt;
              &lt;/LinearLayout&gt;
  
          &lt;/LinearLayout&gt;
  
          &lt;!--Row 6--&gt;
          &lt;LinearLayout
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:orientation="horizontal"
              android:padding="5dp"&gt;
  
              &lt;TextView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="@string/sub_dept"
                  android:textAppearance="?android:attr/textAppearanceMedium" /&gt;
  
              &lt;LinearLayout
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:background="@drawable/greyframe"
                  android:orientation="horizontal"&gt;
  
                  &lt;Spinner
                      android:id="@+id/spinnerSubdept"
                      android:layout_width="248dp"
                      android:layout_height="40dp"
                      android:entries="@array/subdepartments" /&gt;
              &lt;/LinearLayout&gt;
  
          &lt;/LinearLayout&gt;
  
          &lt;!--Row 7--&gt;
          &lt;LinearLayout
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:background="@drawable/box"
              android:orientation="vertical"&gt;
  
              &lt;LinearLayout
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:orientation="horizontal"
                  android:padding="5dp"&gt;
  
                  &lt;TextView
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="@string/delivery_invoice_number"
                      android:textAppearance="?android:attr/textAppearanceMedium" /&gt;
  
                  &lt;LinearLayout
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:background="@drawable/greyframe"
                      android:orientation="horizontal"&gt;
  
                      &lt;EditText
                          android:id="@+id/editTextDelivInvNum"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:editable="false"
                          android:minWidth="124dp" /&gt;
                  &lt;/LinearLayout&gt;
  
                  &lt;TextView
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="@string/vendor"
                      android:textAppearance="?android:attr/textAppearanceMedium" /&gt;
  
                  &lt;LinearLayout
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:background="@drawable/greyframe"
                      android:orientation="horizontal"&gt;
  
                      &lt;EditText
                          android:id="@+id/editTextVendor"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:editable="false"
                          android:minWidth="124dp" /&gt;
                  &lt;/LinearLayout&gt;
  
              &lt;/LinearLayout&gt;
  
              &lt;!--Row 8--&gt;
              &lt;LinearLayout
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:orientation="horizontal"
                  android:padding="5dp"&gt;
  
                  &lt;TextView
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="@string/total_dollars"
                      android:textAppearance="?android:attr/textAppearanceMedium" /&gt;
  
                  &lt;LinearLayout
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:background="@drawable/greyframe"
                      android:orientation="horizontal"&gt;
  
                      &lt;EditText
                          android:id="@+id/editTextTotalDollars"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:minWidth="80dp" /&gt;
                  &lt;/LinearLayout&gt;
  
                  &lt;TextView
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="@string/current_total"
                      android:textAppearance="?android:attr/textAppearanceMedium" /&gt;
  
                  &lt;LinearLayout
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:background="@drawable/greyframe"
                      android:orientation="horizontal"&gt;
  
                      &lt;EditText
                          android:id="@+id/editTextCurrentTotal"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:minWidth="80dp" /&gt;
                  &lt;/LinearLayout&gt;
  
                  &lt;TextView
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="@string/qty"
                      android:textAppearance="?android:attr/textAppearanceMedium" /&gt;
  
                  &lt;LinearLayout
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:background="@drawable/greyframe"
                      android:orientation="horizontal"&gt;
  
                      &lt;EditText
                          android:id="@+id/editTextReadonlyQty"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:minWidth="40dp" /&gt;
                  &lt;/LinearLayout&gt;
              &lt;/LinearLayout&gt;
          &lt;/LinearLayout&gt;
  
          &lt;!--Row 9--&gt;
          &lt;LinearLayout
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:orientation="horizontal"
              android:padding="5dp"&gt;
  
              &lt;Button
                  android:id="@+id/buttonSave"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="@string/save" /&gt;
  
              &lt;Button
                  android:id="@+id/buttonFind"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="@string/find" /&gt;
  
              &lt;Button
                  android:id="@+id/buttonClear"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="@string/clear" /&gt;
          &lt;/LinearLayout&gt;
  
      &lt;/LinearLayout&gt;
  &lt;/ScrollView&gt;
  </code></pre>
  
  <h2>UPDATE 5</h2>
  
  <p>Okay, what I'm getting out of this is that the internal-most LinearLayouts need to be thus:</p>
  
  <pre><code>android:layout_width="match_parent"
  android:layout_height="wrap_content"
  </code></pre>
  
  <p>...and all others (above them) thus:</p>
  
  <pre><code>android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  </code></pre>
  
  <p>That works for the most part. But the EditTexts are too puny. If I remove the inner LinearLayouts completely, the EditTexts disappear (or have dimensions of 0?)</p>