Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 354
Faheem Avatar asked May 29 '14 10:05

Faheem


People also ask

What is horizontal linear layout?

LinearLayout is a view group that aligns all children in a single direction, vertically or horizontally. You can specify the layout direction with the android:orientation attribute. Note: For better performance and tooling support, you should instead build your layout with ConstraintLayout.

How do I center align in relative layout?

If you want to make it center then use android:layout_centerVertical="true" in the TextView. my Relative Layout has fill_parent for both height and width.

How do you center text in layout?

android:gravity="center" for text center in TextView. android:gravity="center_horizontal" inner text if you want horizontally centered. android:gravity="center_vertical" inner text if you want vertically centered. android:layout_centerInParent="true" if you want TextView in center position of parent view.

How do you center everything in a linear layout?

To center align LinearLayout, assign android:gravity attribute of this LinearLayout with the value “center”. Let us create an Android application with LinearLayout containing two Button widgets as children. We shall center align these children using gravity attribute.


2 Answers

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

like image 128
Ahmad Raza Avatar answered Oct 31 '22 15:10

Ahmad Raza


<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

like image 42
Qadir Hussain Avatar answered Oct 31 '22 15:10

Qadir Hussain