Friday, December 27, 2024
Google search engine
HomeLanguagesJavaAndroid – Center Text in a TextView Horizontally and Vertically

Android – Center Text in a TextView Horizontally and Vertically

Many android applications use text view for displaying text within android applications. There are different ways used to align the text view within our XML layout. To align TextView for different screen sizes we cannot use margin from all sides. For aligning the text view for different screen sizes we have to align them centrally to vertically and centrally to the horizontal of the total height and width of the android device screen. In this article, we will take a look at How to center align TextView Horizontally and Vertically in the android applications. 

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.

Step 2: Working with the activity_main.xml file

Navigate to app > res > layout > activity_main.xml and add the code below. Comments are added in the code to get to know in detail. 

XML




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/idRLContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
 
    <!--on below line we are creating
        a text view for heading of our app-->
    <TextView
        android:id="@+id/idTVHeading1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/idTVMsg"
        android:layout_centerInParent="true"
        android:layout_margin="20dp"
        android:gravity="center"
        android:padding="10dp"
        android:text="Center Alignment of Text View"
        android:textAlignment="center"
        android:textColor="@color/black"
        android:textSize="20sp"
        android:textStyle="bold" />
 
    <!-- on below line we are creating a text view for center horizontally
          aligned text view. In the below text view we are specifying
          layout_centerHorizontal to true to align text view center
         horizontally to center of screen and we are specifying
         layout_centerVertical to true to align text view center
         vertically to center of screen -->
    <TextView
        android:id="@+id/idTVMsg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:padding="4dp"
        android:text="Welcome to Geeks for Geeks"
        android:textColor="@color/black"
        android:textSize="20sp"
        android:textStyle="bold" />
     
</RelativeLayout>


Now run your application to see the output of it. 

Output:

Output

 

RELATED ARTICLES

Most Popular

Recent Comments