-1

I am working on a project. I am required to make a TextView to be fit into 1 line (as shown below).

Click to show the original view and required view

But after I tried adjusting layout_weight, it fails.

Here is my code (with original layout_weight):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tool="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include layout="@layout/view_toolbar" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/appbar"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center_horizontal"
            android:orientation="vertical"
            android:padding="25dp">

            (omitted)

            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@android:id/text1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/padding_small"
                android:gravity="center_vertical"
                android:orientation="horizontal"
                android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
                android:paddingStart="?android:attr/listPreferredItemPaddingStart"
                android:textAppearance="?android:attr/textAppearanceListItemSmall">

                <TextView
                    android:id="@+id/hose_expired_count"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="@dimen/padding_x_larger"
                    android:layout_weight="1"
                    android:textColor="@color/textColor_dark"
                    android:textSize="@dimen/font_size_medium" />

                <TextView
                    android:id="@+id/hose_expired_count_amount"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="@dimen/padding_larger"
                    android:layout_weight=".5"
                    android:textColor="@color/textColor_dark"
                    android:textSize="@dimen/font_size_medium" />
            </LinearLayout>

            (omitted)

        </LinearLayout>

        (omitted)

    </LinearLayout>
</RelativeLayout>

How can I fix the layout_weight (and possibly width, height, etc.) so that I can get the TextView fitted?

Tam Huynh
  • 2,026
  • 1
  • 16
  • 20
hycwong
  • 13
  • 1
  • 6
  • Possible duplicate of [prevent undesired line wrapping in TextView](https://stackoverflow.com/questions/17604777/prevent-undesired-line-wrapping-in-textview) – denvercoder9 May 21 '18 at 07:57
  • I have checked that question " prevent undesired line wrapping in TextView", it wanted lines to be ellipsised, but I need my line "Hose Expired" to fill the gap between itself and the digit "5" instead. – hycwong May 21 '18 at 08:10

2 Answers2

1

How about you don't use weights for the hose_expired_count_amount TextVIew. Just use wrap_content for the width and don't add any weight. Your "Hose Expired" text will fill the space.

 <TextView
                android:id="@+id/hose_expired_count"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginRight="@dimen/padding_x_larger"
                android:layout_weight="1"
                android:textColor="@color/textColor_dark"
                android:textSize="@dimen/font_size_medium" />

            <TextView
                android:id="@+id/hose_expired_count_amount"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/padding_larger"
                android:textColor="@color/textColor_dark"
                android:textSize="@dimen/font_size_medium" />
        </LinearLayout>

If you want the text to be directly beside each other, you'd need to right-align your hose_expired_count TextView by setting the gravity as right/end

owsega
  • 96
  • 7
0

Just use wrap_content for width of hose_expired_count_amount