How to right-align text in an Android TextView or EditText?

AndroidAlignmentAndroid EdittextLayout Gravity

Android Problem Overview


I have an EditText in my application. I want to align the text in it to the right instead of the default left. I tried adding

android:layout_gravity="right"

but this doesn't seem to work. any other suggestions please?

Android Solutions


Solution 1 - Android

You should use android:gravity="right". layout_gravity is for the view (EditText) alignment against the container.

Solution 2 - Android

You may use android:layout_alignParentRight="true" to align the EditText's right edge to its parent's right edge. Also, if you really want to use the layout_gravity or gravity attributes, check out this article that discusses the proper use of them.

Solution 3 - Android

You can jst set property for edit text in property window i.e. Gravity to right or by adding code of lin e in xml file of UI : android:gravity="right"

Solution 4 - Android

layout_gravity means that you are specifying the layouts attribute, not the element inside. In some conditions, such as layout_width and layout_height is wrap_content, layout specifications can give what you want about the element inisde your layout, since the boundries of the layout and the element (not elements) are the same..

Solution 5 - Android

use layout width and height as wrap content inside relative layout :

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

    <ImageView
        android:id="@+id/icon"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:layout_alignParentStart="true"
        android:paddingEnd="16dp"
        android:paddingStart="8dp"
        tools:src="@mipmap/ic_launcher"
        android:layout_alignParentLeft="true"
        android:paddingRight="16dp"
        android:paddingLeft="8dp"
        android:contentDescription="@string/todo" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toEndOf="@id/icon"
        android:maxLines="1"
        android:lines="1"
        android:paddingTop="8dp"
        tools:text="Google"
        android:layout_toRightOf="@id/icon" />

    <TextView
        android:id="@+id/last_used"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:paddingTop="8dp"
        tools:text="Last used: yesterday at 18.54" />
</RelativeLayout>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsherryView Question on Stackoverflow
Solution 1 - AndroidRandy Sugianto 'Yuku'View Answer on Stackoverflow
Solution 2 - AndroidErickView Answer on Stackoverflow
Solution 3 - Androidkrupa parekhView Answer on Stackoverflow
Solution 4 - AndroidAlpaslanView Answer on Stackoverflow
Solution 5 - AndroidthatView Answer on Stackoverflow