Search code examples
androidxmllayoutandroid-linearlayout

How to set Textview , Edittext and button height's to be same on Linearlayout


How can I set the LinearLayout items' heights to be the same? I tried with RelativeLayout but didn't have any success. Any idea how I can do it?

<LinearLayout
    android:background="@color/red"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:weightSum="11">

    <TextView
        android:id="@+id/lblParca"

        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="5.5"
        android:text="Parça Kodu"
        android:textSize="15dp" />

    <EditText
        android:id="@+id/txthldMalzemeKodu"
        style="@style/DefaultEditTextSmall"
        android:layout_width="0dip"
        android:layout_weight="4.5"
        android:layout_height="wrap_content"
        android:inputType="textCapCharacters"
        android:singleLine="true" />

    <ImageButton
        android:id="@+id/btnhldgetMalzeme"
        style="@style/btnStyleBreakerBay"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@mipmap/ic_ara" />
</LinearLayout>

That's what I got:

enter image description here


Solution

  • If you are happy with Aspicas answer, the layout can be simplified to just

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:background="@color/red"
        android:orientation="horizontal">
    
        <TextView
            android:id="@+id/lblParca"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_gravity="center_vertical"
            android:layout_weight="0.40"
            android:text="Parça Kodu" />
    
        <EditText
            android:id="@+id/txthldMalzemeKodu"
            style="@style/DefaultEditTextSmall"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.40"
            android:inputType="textCapCharacters"
            android:singleLine="true"/>
    
        <ImageButton
            android:id="@+id/btnhldgetMalzeme"
            style="@style/btnStyleBreakerBay"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.20"
            android:scaleType="centerInside"
            android:src="@mipmap/ic_ara"/>
    </LinearLayout>
    

    There is no need to have all of the wrappers. Also notice the change to your TextView utilizing gravity instead of layout_gravity to center your content. I am actually surprised this works, from my understanding this should be stretching the height of the views, apparently I was wrong in that thinking.