0

I have a horizontal LinearLayout with several TextViews. As user types some digits into result view, it expands and other TextViews are getting smaller. I want all TextViews to have fixed width and hide the first digits when the content is too big. I experimented with surrounding result view with ScrollView or setMovementMethod(new ScrollingMovementMethod()) or TableLayout without luck. SO question

    <LinearLayout
    android:id="@+id/assignment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="5"
    style="@style/Formula">

    <TextView
        android:id="@+id/operandFirst"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="50"
        style="@style/FormulaValue" />

    <TextView
        android:id="@+id/operator"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:text="+"
        style="@style/FormulaOperator" />

    <TextView
        android:id="@+id/operandSecond"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="51"
        style="@style/FormulaValue" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:text="="
        style="@style/FormulaEqualSign" />

    <TextView
        android:id="@+id/result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="101"
        style="@style/FormulaValue" />

    <Space
        android:layout_weight="1"
        android:layout_width="20dp"
        android:layout_height="20dp" />
</LinearLayout>

Updated image

Community
  • 1
  • 1
Leos Literak
  • 8,805
  • 19
  • 81
  • 156

1 Answers1

1

It can be done by defining weight to all textView, what you already are doing but also set android:layout_width="0dp" when you are giving weight.

Edit:

If you don't want grow your textView vertically than add android:singleLine="true" attribute to every textView of your layout.

To remove the unnecessary white space at the right end, remove android:layout_weight="1" attribute from <space... />

So it would be,

<Space
    android:layout_width="20dp"
    android:layout_height="20dp" />
Apurva
  • 7,871
  • 7
  • 40
  • 59
  • One problem is fixed with your answer - other TextViews have fixed size, great! On the other hand there is unused space after the Space widget (even if I remove its weight attribute). Second issue is that the TextView grows vertically (another lines). I prefer it to hide some digits. – Leos Literak Mar 08 '15 at 16:18
  • It is used, there are multiple lines and I need a space at the end of line. I may try to experiment with right margin instead. – Leos Literak Mar 08 '15 at 18:23
  • When we removed the Space, weightSum="5" became incorrect. When I removed it, it works like a charm. – Leos Literak Mar 09 '15 at 05:42
  • @SanjeetAjnabee Lets remove unneccessary comments to clean up your answer. – Leos Literak Mar 09 '15 at 05:43
  • @LeosLiterak weightsum is not essential, if you write it though then also make sure that sum of weight of all child components equal the weightsum. – Apurva Mar 09 '15 at 07:09