Héctor
Héctor

Reputation: 26084

Adjust width with respect to another element

I have this layout:

...
    <LinearLayout
                android:layout_alignParentBottom="true"
                android:layout_width="match_parent"
                android:orientation="horizontal"
                android:gravity="center"
                android:layout_height="60dp">

            <TextView
                android:text="@string/n_alb"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"/>

            <EditText 
                android:layout_width="wrap_content" <!-- ?? -->
                android:layout_height="match_parent"/>

    </LinearLayout>
...

I want the TextView's width to be wrap_content whereas the EditText's width fills the rest of the space in the parent.

I know I can use weightSum and layout_weight but isn't there another way?

Thanks in advance

Upvotes: 1

Views: 122

Answers (2)

Mehul Santoki
Mehul Santoki

Reputation: 1208

If you have to longer text in textview then you can use it simple way.

 <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Android Development Android Development"
            android:background="#fff000"
            android:id="@+id/textView2" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/editText2"
            android:singleLine="true"
            android:text="hello developer How are you"
            android:background="#ff0000"
            />
    </LinearLayout>

Upvotes: 0

Abhishek Patel
Abhishek Patel

Reputation: 4328

Try This

 <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:layout_alignParentBottom="true"
    android:gravity="center"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/yourtextview"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="@string/n_alb" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_toRightOf="@+id/yourtextview" >
    </EditText>
</RelativeLayout>

Upvotes: 3

Related Questions