Search code examples
androidandroid-layoutandroid-imageviewandroid-image

How to apply gravity to the items in Linear Layout, whose orientation is Horizontal?


I have a Parent linear layout, whose orientation is vertical, under this i have another layout whose orientation is vertical, and it has a child layout whose orientation is Horizontal.

This layout has 2 Image Views. , I want to apply gravity to these Image views.1 With left-gravity, and 1 with right-gravity.

When i apply gravity to this layout it does not make any effect, but if i remove the last horizontal layout it perfectly works, but shows 1 image up and 1 down.

This is my code, Kindly guide me

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#b9000000"
    android:id="@+id/ll_black_overlay"
    >

   //There are 2 More layouts, whith weights 20 and 50

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="30"
        android:orientation="vertical"
        android:paddingTop="20dp"
        android:paddingLeft="70dp"
        android:paddingRight="70dp"
        >

        // Layout for displaying text messages Vertically

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_gravity="center"

            >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Text View 1 , "
                android:textColor="@color/white"
                android:layout_gravity="center"
                android:gravity="center"
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Text View 002 , "
                android:textColor="@color/white"
                android:layout_gravity="center"
                android:gravity="center"
                />


 </LinearLayout>

  // --Problem area Layout for displaying Images side by side,
      1 image at left corner, 1 at right corner, if i remove Linear layout,
      gravity works, but id is not working with Linear Layout 

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"

            >

            <ImageView
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:src="@drawable/icn_tour_arrow_left"
                android:layout_gravity="left"
                />


            <ImageView
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:src="@drawable/icn_tour_arrow_right"
                android:layout_gravity="right"
                />

        </LinearLayout>

    </LinearLayout>




</LinearLayout>

Solution

  • <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
    
            >
    
            <ImageView
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:layout_weight="1"
                android:src="@drawable/icn_tour_arrow_left"
                android:layout_gravity="left"
                />
    
    
            <ImageView
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:layout_weight="1"
                android:src="@drawable/icn_tour_arrow_right"
                android:layout_gravity="right"
                />
    
        </LinearLayout>
    

    it will display both images two equal with in screen if you want both images at corner add another textview at between two images with layout_weight