Align Textview to the right in a Linearlayout in Android Studio

Asked

Viewed 226 times

-1

Hello, I’m a beginner in Android Studio, and I had a problem with alignment, I already took a look at similar questions but could not solve the problem. My layout is like this: inserir a descrição da imagem aqui

I need to leave Altitude, Latitude and Longitude to the left and the numbers, latitudeTest and longitudeTest to the right, more or less like this:

Altitude: 10.0
Latitude: latitudeTest
Longitude: longitudeTest

My code is like this

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/item_horizontal_margin"
            android:layout_marginEnd="@dimen/item_horizontal_margin"
            android:orientation="vertical">

            <TextView
                android:id="@+id/name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                <!--Altitude:-->
                android:text="@string/lb_altitude" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                <!--10.0:-->
                android:text="@={``+waypoint.altitude}" />

            <TextView
                android:id="@+id/lb_latitude"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                <!--Latitude:-->
                android:text="@string/lb_latitude" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/lb_latitude"
                android:text="latitudeTest" />

            <TextView
                android:id="@+id/lb_longitude"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                <!--Longitude:-->
                android:text="@string/lb_longitude" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/lb_longitude"
                android:text="longitudeTeste" />
        </LinearLayout>

I appreciate suggestions and help! Thank you

  • 1

    There are many ways to do this... You can use each line within one LinearLayout with horizontal orientation, you can use RelativeLayout, I guess you can do with ConstraintLayout also... have you tried to do any of these things?

1 answer

2

There are several ways to get the result you want:

Using only Linearlayout

The simplest way, following the approach you have already started to implement, would be the following:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="TextView" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="end"
        android:text="TextView" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="TextView" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="end"
        android:text="TextView" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="TextView" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="end"
        android:text="TextView" />

</LinearLayout>

In the code snippet above, a Linearlayout is used with the orienteering vertical (so that your child items are arranged one below the other), where within it are placed X lines, composed by a Linearlayout with horizontal orientation (this orientation is the default, so you do not need to declare it), with two Textviews inside, each with the attribute Weight = 1, which makes both Textviews have the same weight within the parent layout.

There are other forms, with Relativelayout or Constraintlayout, but the idea is this from above.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.