Center View or Layout in Android
To center view/Layout which is inside RelativeLayout (parent Layout)
Center Horizontally
android:layout_centerHorizontal="true"
Example
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello"
android:textSize="18dp"
android:layout_centerHorizontal="true"
android:textStyle="italic"/>
</RelativeLayout>
Center Vertically
android:layout_centerVertical="true"
Example
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add"
android:textSize="18dp"
android:layout_centerVertical="true"
android:textStyle="italic"/>
</RelativeLayout>
Center both Horizontally & Vertically
android:layout_centerInParent="true"
Example
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Add"
android:textSize="18dp"
android:layout_centerInParent="true"
android:textStyle="italic"/>
</RelativeLayout>
To center view/Layout which is inside LinearLayout, ScrollView, TextInputLayout and other layouts
Center Horizontally
android:layout_gravity="center_horizontal"
Example
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/add"
android:textSize="18dp"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>
Center Vertically
android:layout_gravity="center_vertical"
<ScrollView
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"
android:textSize="18dp"
android:layout_gravity="center_vertical"
android:textStyle="italic"/>
</ScrollView>
Center both Horizontally & Vertically
android:layout_gravity="center"
Example
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"
android:textSize="18dp"
android:layout_gravity="center"
android:textStyle="italic"/>
</LinearLayout>
To center the elements inside View/Layout
android:gravity="center"
android:gravity="center_vertical"
android:gravity="center_horizontal"
Example
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"
android:textSize="18dp"
android:gravity="center"
android:textStyle="italic"/>