2

I have this RelativeLayout with two TextViews, One with center aligned and the other end aligned,

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="60dp"
                android:layout_marginEnd="@dimen/size_10dp"
                android:layout_marginStart="@dimen/size_10dp">

                <TextView
                    android:id="@+id/title_count_text"
                    fontPath="fonts/title_bold.ttf"
                    android:layout_width="wrap_content"
                    android:layout_height="60dp"
                    android:layout_alignParentEnd="true"
                    android:layout_gravity="center"
                    android:background="@color/background"
                    android:ellipsize="end"
                    android:gravity="center|center_vertical"
                    android:maxLines="1"
                    android:text="(0)"
                    android:paddingLeft="@dimen/size_5dp"
                    android:paddingStart="@dimen/size_5dp"
                    android:textColor="@color/textColorGrey"
                    android:textSize="@dimen/size_18dp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/title_text"
                    fontPath="fonts/title_bold.ttf"
                    android:layout_width="wrap_content"
                    android:layout_height="60dp"
                    android:layout_centerInParent="true"
                    android:layout_gravity="center"
                    android:ellipsize="end"
                    android:gravity="center"
                    android:maxLines="1"
                    android:text="This is a Sample Test"
                    android:textAlignment="center"
                    android:textColor="@color/textColorGrey"
                    android:textSize="@dimen/size_18dp"
                    android:textStyle="bold" />

            </RelativeLayout> 

Looks something like this, Please find the image below,

Its working fine with small text length, but if I increase the text length it will overlap the counter text, Obviously the title_text is rendered after the title_count_text,

But if I add android:layout_toLeftOf="@+id/title_count_text" property to the title_text and change the width to match parent to make this TextView center aligned. the title_text will loose the center gravity.

Will look something like this, Please find the second attachment below

Although it might not be visible, but yeah its now shifted about the width of counter text to the left,

What i want to achieve is to preserve the center alignment of the title_textand when the text length is greater and it reaches the start of counter text view, it should clip the text and append ... to the end,

Any kind of help will be totally appreciated,

Thanks

TL:DR Not only center aligned but also needs to clip when it meets the counter text view, if text length is greater.

Sanoop Surendran
  • 3,484
  • 4
  • 28
  • 49

7 Answers7

1
<TextView
                android:id="@+id/title_text"
                android:layout_toLeftOf="@+id/title_count_text"
                android:layout_alignParentLeft="true"
                fontPath="fonts/title_bold.ttf"
                android:layout_width="wrap_content"
                android:layout_height="60dp"
                android:layout_centerInParent="true"
                android:layout_gravity="center"
                android:ellipsize="end"
                android:gravity="center"
                android:maxLines="1"
                android:text="This is a Sample Test"
                android:textAlignment="center"
                android:textColor="@color/textColorGrey"
                android:textSize="@dimen/size_18dp"
                android:textStyle="bold" />

I added the following two lines to your second text view to get the behavior you described:

android:layout_toLeftOf="@+id/title_count_text"
android:layout_alignParentLeft="true"

You may have to play around with the ellipsize settings though.

Jon
  • 1,715
  • 12
  • 14
1

If you like then remove the padding of your Relative layout and add padding to title_text Textview

 <RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="60dp"
    >

    <TextView
        android:id="@+id/title_count_text"
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:layout_alignParentEnd="true"
        android:layout_gravity="center"
        android:ellipsize="end"
        android:gravity="center|center_vertical"
        android:maxLines="1"
        android:text="(0)"
        android:paddingLeft="@dimen/size_5dp"
        android:paddingStart="@dimen/size_5dp"
        android:textColor="@color/textColorGrey"
        android:textSize="@dimen/size_18dp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/title_text"
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:padding="16dp"
        android:layout_centerInParent="true"
        android:layout_gravity="center"
        android:ellipsize="end"
        android:gravity="center"
        android:maxLines="1"
        android:text="This is a Sample Test wehatb the  are u dongibng wjkl"
        android:textAlignment="center"
        android:textColor="@color/textColorGrey"
        android:textSize="@dimen/size_18dp"
        android:textStyle="bold" />

</RelativeLayout>
Mohit Suthar
  • 8,725
  • 10
  • 37
  • 67
0

As I mentioned above in above comment , use LinearLayout with width matchparent , orientation horizontal. Inside linear layout take 2 text views ,1st TextView width 0dp and weight 1, 2nd TextView width wrap content

Manohar
  • 22,116
  • 9
  • 108
  • 144
0

Update title_text as following:

               <TextView
                android:id="@+id/title_text"
                fontPath="fonts/title_bold.ttf"
                android:layout_width="match_parent"
                android:layout_toLeftOf="@+id/title_count_text"
                android:ellipsize="end"
                android:layout_height="60dp"
                android:gravity="center"
                android:maxLines="1"
                android:text="This is a Sample Test"
                android:textColor="@color/textColorGrey"
                android:textSize="@dimen/size_18dp"
                android:textStyle="bold" />

The following 3 attributes are main to added:

                    android:layout_width="match_parent"
                    android:layout_toLeftOf="@+id/title_count_text"
                    android:ellipsize="end"

Also, remove :

                android:layout_centerInParent="true"
                android:layout_gravity="center"
iMDroid
  • 2,108
  • 1
  • 16
  • 29
0

Try this :-

<?xml version="1.0" encoding="utf-8"?>

<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">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_marginEnd="@dimen/size_10dp"
        android:layout_marginStart="@dimen/size_10dp">

        <TextView
            android:id="@+id/title_count_text"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:background="@color/background"
            android:textAlignment="center"
            android:ellipsize="end"
            android:maxLines="1"
            android:gravity="center"
            android:text="(0)"
            android:paddingLeft="@dimen/size_5dp"
            android:paddingStart="@dimen/size_5dp"
            android:textColor="@color/textColorGrey"
            android:textSize="@dimen/size_18dp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/title_text"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:ellipsize="end"
            android:layout_gravity="end"
            android:maxLines="1"
            android:gravity="center"
            android:text="This is a Sample Test"
            android:textAlignment="center"
            android:textColor="@color/textColorGrey"
            android:textSize="@dimen/size_18dp"
            android:textStyle="bold" />
    </LinearLayout>
</LinearLayout>
Ankita
  • 1,129
  • 1
  • 8
  • 15
  • Noo.. @Ankita aa .. it doesnt work, what this bascially do i assign the allotted space for both textViews, it wont clip the textview. :( :( Thanks for the effort. – Sanoop Surendran Sep 27 '17 at 05:19
-1

Try this and let me know,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:background="#ff0000"
    android:weightSum="1"
    >
    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.8"
        android:text="test"
        android:gravity="center"/>

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.2"
        android:text="0"
        android:gravity="center"/>

</LinearLayout>
Radhey
  • 2,139
  • 2
  • 24
  • 42
-1

Try this:-

Add android:layout_toStartOf="@+id/title_count_text" and android:maxLines="2" to the TextView with id "@+id/title_text". It worked for me.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_marginEnd="@dimen/dp10"
android:layout_marginStart="@dimen/dp10">

<TextView
    android:id="@+id/title_count_text"
    android:layout_width="wrap_content"
    android:layout_height="60dp"
    android:layout_alignParentEnd="true"
    android:ellipsize="end"
    android:gravity="center|center_vertical"
    android:maxLines="1"
    android:paddingLeft="@dimen/dp5"
    android:paddingStart="@dimen/dp5"
    android:text="(0)"
    android:textSize="@dimen/dp18"
    android:textStyle="bold" />

<TextView
    android:id="@+id/title_text"
    android:layout_width="wrap_content"
    android:layout_height="60dp"
    android:layout_gravity="center"
    android:layout_toStartOf="@+id/title_count_text"
    android:gravity="center"
    android:maxLines="2"
    android:text="This is a Sample Test This is a Sample Test This is a Sample Test This is a Sample Test"
    android:textAlignment="center"
    android:textSize="@dimen/dp18" />

</RelativeLayout>
Community
  • 1
  • 1
Yogesh
  • 111
  • 1
  • 9