1zz0
1zz0

Reputation: 371

Android: Center two elements in a Linear Layout

I want that both elements are centered and without that stretching.

Also, it would be great if the weight was not that wide.

This is how it looks like:

LinearLayout

Heres the code:

android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="@color/listColor"
android:layout_weight="1"
android:orientation="vertical"

>

    <TextView


        android:layout_width="match_parent"
        android:layout_height="fill_parent"

        android:layout_weight="1"

        android:background="@drawable/layout_item_value"

        android:text="163,00"

        android:textSize="18.2dp"

        android:gravity="center_vertical|center_horizontal"

        android:id="@+id/txtListValue"
        android:textColor="@color/back"



        />


    <ImageView
        android:id="@+id/image_order"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_weight="1"
        android:scaleX="0.4"
        android:scaleY="0.4"
        android:gravity="center_vertical|center_horizontal"

        />

Thank You in advance!

Upvotes: 1

Views: 2158

Answers (3)

Viswa Sundhar
Viswa Sundhar

Reputation: 305

Its very simple, you can maintain it with the parent layout itself

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical"
android:gravity="center">

<TextView
    android:id="@+id/txtListValue"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#FF1493"
    android:text="163,00"
    android:textColor="#000000"
    android:textSize="30sp" />


<ImageView
    android:id="@+id/image_order"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:src="@mipmap/ic_launcher" />

</LinearLayout>

Upvotes: 3

Narendra Sorathiya
Narendra Sorathiya

Reputation: 3830

You can use below API without weightsum...

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical">

    <TextView
         android:layout_width="match_parent"
         android:layout_height="0dp"
         android:layout_weight="1"
         android:background="#FC226E"
         android:gravity="center"
         android:text="1,00"
         android:textColor="#FFFFFF"
         android:textSize="28sp" />

    <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:padding="15dp">

        <ImageView
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:gravity="center"
             android:src="@mipmap/ic_launcher" />

   </LinearLayout>
</LinearLayout>

Upvotes: 0

rafsanahmad007
rafsanahmad007

Reputation: 23881

try this layout:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="vertical"
    android:weightSum="2">

    <TextView
        android:id="@+id/txtListValue"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#FF1493"
        android:gravity="center_vertical|center_horizontal"
        android:text="163,00"
        android:textColor="#000000"
        android:textSize="30sp" />


    <ImageView
        android:id="@+id/image_order"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center_vertical|center_horizontal"
        android:scaleX="0.4"
        android:scaleY="0.4"
        android:src="@mipmap/ic_launcher" />

</LinearLayout>

Output:

enter image description here

Upvotes: 0

Related Questions