title: Android set max width in percentage of a TextView in LinearLayout of vertical orientation
body: <p>I'd like to set the <strong>layout_weight</strong> of the <code>TextView</code> with the <strong>tv_long_text</strong> to 80% in the following <code>LinearLayout</code> of <strong>vertical</strong> orientation.</p>
  
  <pre><code>&lt;LinearLayout
          android:layout_height="match_parent"
          android:layout_width="match_parent"
          android:orientation="vertical"&gt;
      &lt;TextView
              android:id="@+id/tv_short_text"
              android:layout_height="wrap_content"
              android:layout_width="wrap_content"
              tools:text="short text" /&gt;
      &lt;TextView
              android:id="@+id/tv_long_text"
              android:layout_height="wrap_content"
              android:layout_width="0dp"
              android:layout_weight="0.8"
              tools:text="a pretty long text" /&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>The above is not working because the <strong>orientation of the textview's parent is vertical.</strong> </p>
  
  <p>So, I tried to set the <code>android:layout_width="match_parent"</code> in the xml and then <strong>set the width at run time by getting the measured width</strong> and then sets the width to 80% but the <code>getMeasuredWidth</code> is giving me 0.</p>
  
  <pre><code>int measuredWidth = longTextView.getMeasuredWidth();
  LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) longTextView.getLayoutParams();
  params.width = (int) (measuredWidth * 0.8);
  longTextView.setLayoutParams(params);
  </code></pre>
  
  <p>I also tried to set the <strong>layout_weight</strong> at run time but it didn't work either and it's probably because it's parent view is in vertical orientation.</p>
  
  <pre><code>longTextView.setLayoutParams(
          new LinearLayout.LayoutParams(
                  LinearLayout.LayoutParams.WRAP_CONTENT,
                  LinearLayout.LayoutParams.MATCH_PARENT,
                  0.8f)
  );
  </code></pre>
  
  <p>The one that worked for me is by adding some <strong>extra views</strong> for the long text view. But it's 2 more extra views added for just trying to set the width of this view in percentage. <strong>Is there any other efficient way to do this?</strong></p>
  
  <pre><code>&lt;LinearLayout
          android:layout_height="match_parent"
          android:layout_width="match_parent"
          android:orientation="vertical"&gt;
      &lt;TextView
              android:id="@+id/tv_short_text"
              android:layout_height="wrap_content"
              android:layout_width="wrap_content"
              tools:text="short text" /&gt;
      &lt;LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="horizontal"&gt;
          &lt;TextView
              android:id="@+id/tv_long_text"
              android:layout_height="wrap_content"
              android:layout_width="0dp"
              android:layout_weight="0.8"
              android:textStyle="bold"
              tools:text="a pretty long text" /&gt;
          &lt;View
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_weight="0.2"/&gt;
      &lt;/LinearLayout&gt;
  &lt;/LinearLayout&gt;
  </code></pre>