CSS "float:right" property equivalent in LinearLayout on android?
Asked Answered
O

4

11

On CSS we can write :

<div style="float:right"> Text1 </div>
<div style="float:right"> Text2 </div>

by this way Text1 will appear on the right ..

I'm trying to do the same with LinearLayout , the View should appear from right to left :

<LinearLayout android:id="@+id/linearLayout1" android:layout_gravity="right" android:gravity="right"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:layout_weight="1" android:weightSum="2" android:orientation="horizontal">
        <!-- First Column should be on the right : Text1-->
        <LinearLayout android:id="@+id/linearLayout2"
            android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_gravity="right" android:gravity="right"
            android:layout_weight="1">...</LinearLayout>
        <!-- Second Column should be on the left : Text2 -->
        <LinearLayout android:id="@+id/linearLayout3"
            android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_gravity="right" android:gravity="right"
            android:layout_weight="1">...</LinearLayout>
</LinearLayout>

Thanks

Orpington answered 13/10, 2011 at 19:29 Comment(2)
does it appear from top to bottom?Grandiloquent
@LAS_VEGAS mmm,the layout in general does not work , so I'm just trying to simulate float:right using LinearLayout ? Thanks .Orpington
D
0

This may be it

<LinearLayout android:id="@+id/linearLayout1"
    android:gravity="right"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:weightSum="2"
    android:orientation="horizontal"
    android:layout_gravity="right"
    >
    <!-- Second Column should be on the left : Text2 -->
    <LinearLayout android:id="@+id/linearLayout3"
        android:layout_width="wrap_content" android:layout_height="fill_parent" 
        android:layout_weight="1">...</LinearLayout>
    <!-- First Column should be on the right : Text1-->
    <LinearLayout android:id="@+id/linearLayout2"
        android:layout_width="wrap_content" android:layout_height="fill_parent"
        android:layout_weight="1">...</LinearLayout>

Dickman answered 13/10, 2011 at 21:18 Comment(3)
I think you have just swap Text2 with Text1 physically ? I can do that , but I don't want ? in CSS we can let the element float to right even if it is written first !Orpington
I also put a gravity on the parentDickman
you have layout_gravity="right" repeated twice in the parent's XMLViridescent
I
0

Don't know if it is possible with LinearLayout, but you can achieve what you need with a RelativeLayout like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <LinearLayout
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:text="Text1"
            android:textAppearance="@android:style/TextAppearance.Large" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_toLeftOf="@+id/text1"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:text="Text1"
            android:textAppearance="@android:style/TextAppearance.Large" />
    </LinearLayout>

</RelativeLayout>

In relative layout you can align the "text1" layout container to right side relative to parent (android:layout_alignParentEnd="true" or android:layout_alignParentRight="true" depending on SDK version compatibility), then you place the "Text2" container LinerLayout at the left side of the Text1 container (android:layout_toLeftOf="@+id/text1"). If you want to add a 3rd container align right just use this last attribute relative to Text2 container (android:layout_toLeftOf="@+id/text2") and so on.

Hope this can help you. It looks like:

Example of right aligned columns layout

Intinction answered 13/12, 2015 at 20:44 Comment(2)
How can I do a line break? I want a lot of buttons behave like css-float property :)Luminescence
@StefanBrendle You can use this structure inside an horizontal LinerLayout. <LinearLayout ··· android:orientation="horizontal"> <RelativeLayout> ··· </RelativeLayout> <RelativeLayout> ··· </RelativeLayout> </LinearLayout>Intinction
S
0

Just add:

android:layout_gravity="right"

gravity="right" is for the text to float right, like text alignment.

Seventieth answered 1/3, 2021 at 19:13 Comment(0)
A
-6

Just set the LinearLayout orientation to Horizontal

android:orientation="horizontal"
Ashram answered 13/10, 2011 at 21:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.