To create a TextView
in the middle of a layout in Android, you can use the LinearLayout
or RelativeLayout
container and set the layout_gravity
attribute to center
.
Here is an example of how to create a TextView
in the middle of a LinearLayout
using XML layout file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello, World!" /> </LinearLayout>
This creates a LinearLayout
with vertical orientation and gravity
set to center
, and a TextView
with the text "Hello, World!" inside the LinearLayout
. The TextView
will be centered vertically and horizontally in the layout.
You can also create a TextView
in the middle of a RelativeLayout
using the layout_centerInParent
attribute. For example:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="Hello, World!" /> </RelativeLayout>