2

I want to align text and an image in a LinearLayout in the same line. I thought of using layout_gravity option for the TextView and ImageView within a LinearLayout with orientation=horizontal but it doesn't seem to work. Any reason why is that?

Apparently, layout_gravity works only with orientation=vertical.

I can do it use multiple linear layouts within the main linear layout but was looking at an elegant way to do it.

Here is the code for the same:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:gravity="center_horizontal"
            android:background="#e3e2ad"
            android:orientation="vertical" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:textSize="24sp"
                    android:text="Layout's Vertical Orientation" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="#bcf5b1"
                    android:layout_gravity="left"
                    android:text="left" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:src="@drawable/pigeon"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="#d6c6cd"
            android:orientation="vertical" >
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:textSize="24sp"
                    android:text="Layout's Horizontal Orientation" />
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal">
                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:background="#bcf5b1"
                            android:layout_gravity="left"
                            android:text="left" />

                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="right"
                            android:src="@drawable/pigeon"/>
                </LinearLayout>

        </LinearLayout>

</LinearLayout>

And here is the output: enter image description here

I have already checked the answer to this question here but it didn't solve this problem. Difference between gravity and layout_gravity on Android

Shivam Kumar
  • 1,892
  • 2
  • 21
  • 33
Neelabh
  • 31
  • 6

4 Answers4

1

replace this layout with your code and change weight according to your requirement you have to pass android:layout_weight to your child view

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_gravity="center_horizontal"
        android:layout_weight="0.33"
        android:text="Layout's Vertical Orientation"
        android:textSize="24sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_gravity="left"
        android:layout_weight="0.33"
        android:background="#bcf5b1"
        android:text="left" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_gravity="right"
        android:layout_weight="0.33"
        android:src="@drawable/pigeon" />

</LinearLayout>
Milan Pansuriya
  • 2,521
  • 1
  • 19
  • 33
  • I think the weights are doing the trick here irrespective of using layout_gravity. My question was targeted more at why layout_gravity doesn't work in the horizontal orientation of linear layout. – Neelabh Dec 28 '18 at 10:32
0

Use Another Relative layout inside the linear layout and add your textview and imageview under it. Then you will get them aligned in the same line.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:gravity="center_horizontal"
            android:background="#e3e2ad"
            android:orientation="vertical" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:textSize="24sp"
                    android:text="Layout's Vertical Orientation" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="#bcf5b1"
                    android:layout_gravity="left"
                    android:text="left" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:src="@drawable/pigeon"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="#d6c6cd"
            android:orientation="vertical" >
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:textSize="24sp"
                    android:text="Layout's Horizontal Orientation" />
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal">
                 <RelativeLayout
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:background="#bcf5b1"
                            //android:layout_weight="0.5"

                            android:layout_gravity="left"
                            android:text="left" />

                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="right"
                            //android:layout_weight="0.5"

                            android:src="@drawable/pigeon"/>

                           </RelativeLayout>
                </LinearLayout>

        </LinearLayout>

</LinearLayout>

Also try adding weights that i mentioned above in comments. That would help

Prateek Aggarwal
  • 166
  • 1
  • 2
  • 14
0

you can use RelativeLayout, android:layout_alignParentRight="true" ontrol is to the right of the parent control。

 <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#bcf5b1"
            android:text="left" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:src="@drawable/ic_launcher_background"/>
    </RelativeLayout>
H.qy
  • 1
  • 1
  • Yeah, that will work. Was wondering why it doesn't work with linear layout in horizontal orientation? – Neelabh Dec 28 '18 at 10:31
0

From what I have researched, you have already told it by specifying horizontal that you want the contents to be aligned left-to-right, so the only options layout_gravity can specify is vertical alignment like top or bottom. I'd highly recommend the first answer to this question. The short answer may be that yes, you have to specify vertical orientation even though (I think) you want to specify a layout for landscape screen mode.

If you want different properties for vertical vs. horizontal orientation, you need to create two separate files, one in /res/layout-land.

This is all the more confusing considering some questions ask about vertical vs. horizontal layout instead of portrait (vertical) vs. landscape (horizontal) screen orientation so I don't blame you for not having a straight answer.

In addition, I would suggest using RelativeLayout or the purportedly more efficient ConstraintLayout which may take some time to get used to but may be the elegant solution you're looking for.

A13X
  • 409
  • 1
  • 6
  • 27