Set TextView text alignment center(middle of screen) inside textview using layout xml.
TextView inside text by default starts from left side of textView area bu some times application developer want to start text from middle of area like setting up textview alignment center. So developer can starts text from center using gravity attribute. So here is the complete step by step tutorial for Center textview text horizontally and vertically inside TextView android.
How to Center textview text horizontally and vertically inside TextView android.
Code for MainActivity.java file.
package com.textview_aligncenter_android_examples.com; import android.app.Activity; import android.os.Bundle; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
Code for activity_main.xml layout file.
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.textview_aligncenter_android_examples.com.MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="This is TextView Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center"
android:background="#01f8d8"
/>
</RelativeLayout>
Screenshot: