5

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.

Faheem
  • 509
  • 2
  • 7
  • 23

8 Answers8

12

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

Ahmad Raza
  • 2,850
  • 1
  • 21
  • 37
  • Its vertically centred. I need horizontally centred. Vertically it will be top aligned. – Faheem May 29 '14 at 10:54
  • @Ahmed: Everything is fine except my "%" sign i.e. second text view is still centred vertically. How will I make it superscript like in the picture I attached? – Faheem May 29 '14 at 11:13
  • @Faheem: try to understand the code. remove android:gravity="center_vertical" from percentage textview. It will work for you :) – Ahmad Raza May 29 '14 at 11:15
  • layout_weight serves no purpose here. – faizal Dec 24 '14 at 08:23
  • This was Exactly it. And No faizal, layout_weight is necessary. – Ruan Mar 02 '17 at 14:32
4
<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

Qadir Hussain
  • 8,721
  • 13
  • 89
  • 124
0

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>
Farouk Touzi
  • 3,451
  • 2
  • 19
  • 25
  • you are giving android:layout_height="match_parent" ..i think it is wrong. – Zar E Ahmer May 29 '14 at 10:31
  • you should test it before comment ;) – Farouk Touzi May 29 '14 at 10:31
  • ok sure :) You made a LinearLayout horizontal, inside of it you made 2 textViews that fills the space they needed. But the problem you had is the text inside the TextViews are positioned on the top left of it. By adding a gravity center_vertical in Textview, you force the text inside to be centered. The match_parent, is set to put layout of textView to fit height of its parent. Hope this helps you – Farouk Touzi May 29 '14 at 10:35
  • and why are you using android:gravity="center_vertical" in both text view and also in parent – Zar E Ahmer May 29 '14 at 10:41
  • The one on TextViews is needed, but in the parent i can say it's useless – Farouk Touzi May 29 '14 at 10:42
0

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>
Giru Bhai
  • 14,370
  • 5
  • 46
  • 74
0

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>
Shrikant
  • 1,560
  • 1
  • 15
  • 32
0

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>
naran z
  • 486
  • 3
  • 13
0

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>
Toppers
  • 559
  • 3
  • 11
0

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

Community
  • 1
  • 1
user
  • 245
  • 1
  • 4
  • 12