Can't align view to the right

GyRo

I am trying to display one view in the left side and one to the right side of the parent layout as following:

<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:text="left"
        android:layout_gravity="left" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="right"
        android:layout_gravity="right" />
</LinearLayout>

But as a result I see both textViews aligned to the left only. I don't understand why. I manage to set the verticaly layout_gravity but not the horizontal one. In addition, I could add:

android:gravity="right"

to the parent layout and than see both views on the right side but couldnt find a way to display one in the left side and one in the right side.
Please help.

Faruk Yazici

Another trick here;

Place an invisible View between the TextViews. And it will occupy the whole remaining area with android:layout_weight="1", so TextViews will remain on both ends. No gravity needed. Try the code below :)

<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:text="left" />

    <View
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="right" />
</LinearLayout>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Bootstrap 4, can't align button to right

I can't align an image to the right

Can't get the right view in laravel 5.6

Ext JS 6 can't right align my button xtype

Can't get the footer to align right vs left

Can't align bootstrap navbar links all the way to the right

Align a View to Right of TabLayout in android

Align a view to the right (appcelerator alloy)

Auto Layout iOS 7 - Can't align right of subview to right of UIScrollView

Can't get the included view to align to the parent bottom in a layout

Can't get Products div align in Mobile View with CSS

Divs won't align to the right

How to align view to right of another without squashing

iOS app shifted right and can't view full table

Laravel and view caching in development -- can't see changes right away

Can't align text

Can't align "a" tag

Align a link/button to the right and bottom of a container that have also other elements - can't use flex

React Native ,why can't I align picker item to right in android platform?

How can I align the text that didn't fit the first line to right in CSS, like they do in poetry?

Align view to left and right position of another view, constraint layout?

Flex div won't align to the left but to the right

How to align Image View and TexView Right and Left of each other?

Programmatically RelativeLayout.ALIGN_RIGHT with a given View.getId() not working

How to align and anchor SwiftUI horizontal list at right edge of view?

Can't align picture and text

LinearLayout can't align properly

Can't align table properly

can't align list correctly