sagus_helgy
sagus_helgy

Reputation: 1437

Center alignment inside LinearLayout

I need to align my message and time to the center of screen. My code

<?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="wrap_content"
    android:orientation="horizontal"
    android:paddingTop="10dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:gravity="center_horizontal">
    <TextView
    android:id="@+id/tvMessage"
    android:gravity="center_vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:autoLink="web|email"
    android:textSize="@dimen/system_msg_text_size"
    android:textColor="@android:color/darker_gray"
    android:text="MESSAGE"/>
<TextView
    android:id="@+id/tvTime"
    android:gravity="center_vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="10dp"
    android:layout_marginLeft="5dp"
    android:text="TIME"
    android:singleLine="true"
    android:textSize="@dimen/system_msg_text_size"
    android:textColor="@android:color/darker_gray"/>
</LinearLayout>

I see

enter image description here

And it's OK

But when the message is longer I see ellipsize

enter image description here

I tried to set android:ellipsize="none" for second TextView

enter image description here

Now we see cropped text "TIME" - no 'E' character

I want to see wrapping first TextView without cropping second TextView with alignment both to center.

enter image description here

Upvotes: 1

Views: 852

Answers (5)

Hareshkumar Chhelana
Hareshkumar Chhelana

Reputation: 24848

Try this way,hope this will help you to solve your problem.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingTop="10dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:gravity="center">

    <TextView
        android:id="@+id/tvMessage"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:autoLink="web|email"
        android:textColor="@android:color/darker_gray"
        android:text="MESSAGE long long long long long long long long"/>

    <TextView
        android:id="@+id/tvTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp"
        android:layout_marginLeft="5dp"
        android:text="TIME"
        android:singleLine="true"
        android:textColor="@android:color/darker_gray"/>
</LinearLayout>

Upvotes: 0

sagus_helgy
sagus_helgy

Reputation: 1437

As usually happens - as soon as question is asked, the answer is coming. Before this i tried to do it two days) Solution is to use RelativeLayout and set first TextView to right of left TextView and set second TextView's alignment to right of parent.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingTop="10dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:gravity="center_horizontal">
    <TextView
        android:id="@+id/tvMessage"
        android:gravity="center_vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:autoLink="web|email"
        android:textSize="@dimen/system_msg_text_size"
        android:textColor="@android:color/darker_gray"
        android:layout_toLeftOf="@+id/tvTime"
        android:text="MESSAGE hk jhg jhg hghgjhgghhhgjkh gjghg jhg j gjg j gjh"/>
    <TextView
        android:id="@+id/tvTime"
        android:gravity="center_vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp"
        android:layout_marginLeft="5dp"
        android:text="TIME"
        android:singleLine="true"
        android:ellipsize="none"
        android:layout_alignParentRight="true"
        android:textSize="@dimen/system_msg_text_size"
        android:textColor="@android:color/darker_gray"/>

</RelativeLayout>

Upvotes: 0

Farouk Touzi
Farouk Touzi

Reputation: 3456

I suggest to you to use RelativeLayout instead of LinearLayout. And then, give layout_alignParentLeft as true to the Message TextView, and layout_alignParentRight as true to the Time TextView, like this :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp" >

<TextView
    android:id="@+id/tvMessage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:autoLink="web|email"
    android:layout_alignParentLeft="true"
    android:layout_toLeftOf="@+id/tvTime"
    android:gravity="center_vertical"
    android:text="MESSAGE long long long long long long long long"
    android:textColor="@android:color/darker_gray"
    android:textSize="15sp" />

<TextView
    android:id="@+id/tvTime"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="10dp"
    android:gravity="center_vertical"
    android:text="TIME"
    android:textColor="@android:color/darker_gray"
    android:textSize="15sp" />
</RelativeLayout>

The result is :

enter image description here

Upvotes: 1

Shivansh
Shivansh

Reputation: 906

I Hope this will help

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:orientation="horizontal"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="10dp"
    android:weightSum="2" >

    <TextView
        android:id="@+id/tvMessage"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:autoLink="web|email"
        android:gravity="center_vertical"
        android:text="MESSAGEasdas asdsadasdsadsadsadsad"
        android:textColor="@android:color/darker_gray" />

    <TextView
        android:id="@+id/tvTime"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="10dp"
        android:layout_weight="1"
        android:gravity="center_vertical"
        android:singleLine="true"
        android:text="TIME"
        android:textColor="@android:color/darker_gray" />

</LinearLayout>

Upvotes: 2

Eric Brynsvold
Eric Brynsvold

Reputation: 3090

Try switching your Message TextView to have a width of 0dp and a weight of 1, so that its width will be set to the remaining space left after measuring the time TextView. If that doesn't work, try switching the containing layout to a RelativeLayout and positioning the inner TextViews appropriately (using alignParentRight="true" for the Time TextView and layout_toLeftOf="@+id/tvTime" on the Message TextView).

Upvotes: 0

Related Questions