Center Text in a TextView Horizontally and Vertically of android

Many android applications use text view for displaying text of android application There are different ways used to align the text view of XML layout. For aligning the textview for different sizes we have to align center, left and right. In this article , we will take a look at  how to center align text in the android applications.



Step-By-Step Implementation


Step 1:Create a new project in Android or you can use an already created project.  



OR



Step 2: Create an XML file named acitivity_main.xml of the layout folder.




Step 3: acitivity_main.xml file continent Design of screen like button, text view, etc.
 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
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">

<TextView
android:id="@+id/idHeading1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@id/idMsg"
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" />


<TextView
android:id="@+id/idMsg"
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 Candidroot solutions"
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold" />

</RelativeLayout>

Happy coding!















365Bloggy May 21, 2024
Share this post
Tags
SUBSCRIBE THIS FORM


Archive