Marcel Marcel - 3 months ago 21
Java Question

Android - restrict space to take match_parent and views on both sides

I have a horizontal linearLayout including a TextView, an EditText and another EditText.
Both TextViews are set to wrap_content in width and heigth. The EditText has wrap_content in height and match_parent in width.

My goal is to get those three views in one line filling the whole line.
But with the above written settings, I have the first TextView taking up the needed space and the EditText filling the rest, not keeping free space for the other TextView.

I achieved a temporary solution by setting layout_weight of both TextViews to 0 and of the EditText to 500 which I'm sure is not the real solution.

Edit: current layout:

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayoutWeight"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gewicht:"
android:id="@+id/lblWeight"
android:layout_alignParentStart="true"
android:textColor="@color/mainForeground"
android:textSize="24dp"
android:layout_weight="1"
android:textStyle="bold" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtWeight"
android:layout_alignBottom="@+id/textView"
android:layout_toEndOf="@+id/textView"
android:textAlignment="center"
android:hint="60"
android:textSize="24dp"
android:textColor="@color/mainForeground"
android:textColorHint="@color/hintColor"

android:layout_weight="500"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/lblUnitWeight"
android:textColor="@color/mainForeground"
android:textSize="24dp"
android:layout_weight="1"
android:text="g." />
</LinearLayout>

Answer

I'm a little bit consufed, but is this what you want?

enter image description here

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/leftTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <EditText
        android:id="@+id/editText"
        android:layout_toEndOf="@+id/leftTextView"
        android:layout_toStartOf="@+id/rightTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content/>

    <TextView
        android:id="@+id/rightTextView"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</RelativeLayout>

Try this, it should works fine.

Comments