Layout Gravity problem: android:layout_gravity="end"

Viewed 1754

I'm new at starting learning Android Studio and I'm having some trouble here. on this explanation of the code, I'm supposed to see the Hello world stick to the right but he doesn't wanna move from the left. Here is what is expected : result expected and what I'm having result at the moment.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="visible"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="end"
        android:gravity="center"
        android:text="Hello World!" />

</LinearLayout>
3 Answers

Add android:orientation="vertical" to your LinearLayout. Default orientation is horizontal.

you can set the TextView gravity to right and make the LinearLayout orientation horizontal as below

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
tools:context=".MainActivity"
orientation="horizontal">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="end"
    android:gravity="right"
    android:text="Hello World!" />

</LinearLayout>

Try this code :-

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
android:orientation="horizontal">

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="end"
    android:gravity="end"
    android:text="Hello World!" />

</LinearLayout>
Related