osimer pothe
osimer pothe

Reputation: 2897

How to align textview in right corner?

I have the following xml .

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/MainLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/smsItemContainerRelativeLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerInParent="true"
            android:layout_gravity="center_horizontal|top"
            android:text="SMS Inbox"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/unread_count"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="-20dp"
            android:layout_toRightOf="@+id/textView"
            android:background="@drawable/notification_bg"
            android:gravity="center"
            android:text="88"
            android:textColor="@android:color/background_light"
            android:textSize="20sp"
            android:textStyle="bold"    
            android:visibility="visible" />
    </RelativeLayout>

    <ListView
        android:id="@+id/SMSList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp" />


</LinearLayout>

For this , I have the following layout :

enter image description here

I want to shift textview of id unread_count to the right of parent . How can I do this ?

If I set android:layout_alignParentRight="true" then I have the following picture :

enter image description here

I simply want to get the following picture :

enter image description here

Upvotes: 0

Views: 3682

Answers (5)

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75788

Just set android:layout_alignParentRight="true"

and try with this

android:gravity="right"
android:textAlignment="gravity"

https://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html#attr_android:layout_alignParentRight

Upvotes: 3

Nanda
Nanda

Reputation: 31

Remove the following lines:

android:layout_marginLeft="-20dp"  android:layout_toRightOf="@+id/textView"

and include this line

android:layout_alignParentRight="true"

Upvotes: 2

Rajan Kali
Rajan Kali

Reputation: 12953

 <TextView
            android:id="@+id/unread_count"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginLeft="20dp"
            android:layout_toRightOf="@+id/textView"
            android:background="@drawable/notification_bg"
            android:gravity="center"
            android:text="88"
            android:layout_alignBottom="@+id/textView"
            android:textColor="@android:color/background_light"
            android:textSize="16sp"
            android:textStyle="bold"    
           />

Upvotes: 0

Rustam
Rustam

Reputation: 6515

add this in your textview of id unread_count

 android:layout_alignParentRight="true"

and remove this

android:layout_toRightOf="@+id/textView"

don't give negative margin

add some right margin to adjust position of your textview

Upvotes: 4

Ashish Shukla
Ashish Shukla

Reputation: 1047

Just Remove this Line

  android:layout_toRightOf="@+id/textView"

and add this Line

  android:layout_alignParentRight="true"

Upvotes: 1

Related Questions