Faheem
Faheem

Reputation: 509

Horizontally center align two textviews in a layout

I need to horizontally align two textview in the center of the screen. Both textviews have different font size.

enter image description here

Here is my code:

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="top|center_horizontal"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/progressstatus"
        android:layout_gravity="center_horizontal"
        android:textColor="#FFFFFF"
        android:textSize="50sp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/percent"
        android:textColor="#CCCCCC"
        android:textSize="20sp" />

</LinearLayout>

Right now my textviews are aligned left and both are showing same font size.

Upvotes: 5

Views: 16190

Answers (8)

user
user

Reputation: 245

you can do it using one textview

            <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="match_parent"
                android:background="@android:color/black"
                tools:context="com.example.demo.MainActivity$PlaceholderFragment" >

                <TextView android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/tv"
                    android:text="75%"
                    android:textSize="20sp"
                    android:gravity="top"
                    android:layout_centerHorizontal="true"/>
              </RelativeLayout>


        TextView textView = (TextView) findViewById(R.id.tv);
                Spannable span = new SpannableString(textView.getText());
                span = new SpannableString(textView.getText());
                span.setSpan(new CustomCharacterSpan(), 2, 3,
                        SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);//u can use ur own ratio using another constructor
                textView.setText(span, TextView.BufferType.SPANNABLE);
                span.setSpan(new RelativeSizeSpan(3f), 0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                textView.setText(span);



    package com.example.demo;

    import android.text.TextPaint;
    import android.text.style.MetricAffectingSpan;

    public class CustomCharacterSpan extends MetricAffectingSpan {
    double ratio = 1.5;

    public CustomCharacterSpan() {
    }

    public CustomCharacterSpan(double ratio) {
        this.ratio = ratio;
    }

    @Override
    public void updateDrawState(TextPaint paint) {
        paint.baselineShift += (int) (paint.ascent() * ratio);
    }

    @Override
    public void updateMeasureState(TextPaint paint) {
        paint.baselineShift += (int) (paint.ascent() * ratio);
    }}

for further reference you can check this link Two different styles in a single textview with different gravity and hieght

Upvotes: 0

Qadir Hussain
Qadir Hussain

Reputation: 8856

<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="match_parent"
android:background="#31BBF9" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="top|center_horizontal"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/progressstatus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="75"
        android:textColor="#FFFFFF"
        android:textSize="50sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="%"
        android:textColor="#FFFFFF"
        android:gravity="center_vertical"
        android:textSize="20sp" />

</LinearLayout>

Output

enter image description here

change testview's (%) android:gravity="center_vertical" to your choice

Upvotes: 4

Ahmad Raza
Ahmad Raza

Reputation: 2850

Try It:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/progressstatus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_vertical|right"
        android:text="75"
        android:textColor="#FFFF00"
        android:textSize="50sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center_vertical"
        android:text="%"
        android:textColor="#CCCCCC"
        android:textSize="20sp" />
</LinearLayout>

Explanation: I set the parent layout to wrap_content, means parent will wrapped its height according to child heights. There are 2 textviews in parent. One has bigger font size and other has smaller font size. Bigger font size view would definitely has more height then smaller one. So, i set the bigger height to wrap_content. Now, the smaller view's height is match_parent, means smaller one would expand itself to max height of parent that would be equal to the height of bigger font's view.. So, both would be center_aligned.

enter image description here

Upvotes: 12

Toppers
Toppers

Reputation: 549

check out with this

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">                 

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="20dp" >

        <TextView
            android:id="@+id/progressstatus"
            android:layout_gravity="center_horizontal"
            android:textColor="#FFFFFF"
            android:textSize="50sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/percent"
            android:textColor="#CCCCCC"
            android:gravity="center"
            android:textSize="20sp" />

    </LinearLayout>

 </LinearLayout>

Upvotes: 0

naran z
naran z

Reputation: 486

Use this code in xml file

  <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white_color" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/progressstatus"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:textColor="#FFFFFF"
                android:textSize="50sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/percent"
                android:textColor="#CCCCCC"
                android:textSize="20sp" />
        </LinearLayout>
    </RelativeLayout>

Upvotes: 0

Shrikant
Shrikant

Reputation: 1580

You could use Relative layout to accomplish the above. Please check the below code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

 <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:gravity="center_horizontal"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/progressstatus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="75"
        android:textSize="20sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/percent"
        android:textSize="20sp" />
  </LinearLayout>

</RelativeLayout>

Upvotes: 0

Farouk Touzi
Farouk Touzi

Reputation: 3456

Try this please

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="horizontal" >

<TextView
    android:id="@+id/progressstatus"
    android:textColor="#FFFFFF"
    android:textSize="50sp"
    android:gravity="center_vertical"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"/>

<TextView
    android:gravity="center_vertical"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:text="@string/percent"
    android:textColor="#CCCCCC"
    android:textSize="20sp" />

</LinearLayout>

Upvotes: 0

Giru Bhai
Giru Bhai

Reputation: 14398

Try this

<?xml version="1.0" encoding="utf-8"?>
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="top|center_horizontal"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/progressstatus"
            android:textColor="#FFFFFF"
            android:textSize="50sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
    android:text="@string/percent"
            android:textColor="#FFFFFF"
            android:textSize="20sp" />

    </LinearLayout>

Upvotes: 0

Related Questions