Reputation: 943
I want to put a layout in the center of the screen.
Upvotes: 83
Views: 132692
Reputation: 1
Use constraint layout. Please refer below code for the same.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:textSize="18sp"
android:textColor="@android:color/holo_orange_dark"
android:text="Centered TextView"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
Upvotes: 0
Reputation: 52366
Use ConstraintLayout. Here is an example that will center the view according to the width and height of the parent screen:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#FF00FF"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHeight_percent=".6"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent=".4"></LinearLayout>
</android.support.constraint.ConstraintLayout>
You might need to change your gradle to get the latest version of ConstraintLayout:
dependencies {
...
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
}
Upvotes: 2
Reputation: 511626
It seems that the trend in Android now is to use a Constraint layout. Although it is simple enough to center a view using a RelativeLayout
(as other answers have shown), the ConstraintLayout
is more powerful than the RelativeLayout
for more complex layouts. So it is worth learning how do do now.
To center a view, just drag the handles to all four sides of the parent.
Upvotes: 9
Reputation: 1006604
Step #1: Wrap whatever it is you want centered on the screen in a full-screen RelativeLayout
.
Step #2: Give that child view (the one, which you want centered inside the RelativeLayout
) the android:layout_centerInParent="true"
attribute.
Upvotes: 160
Reputation: 373
It will work for that code sometimes need both properties
android:layout_gravity="center"
android:layout_centerHorizontal="true"
Upvotes: 4
Reputation: 129
Add android:layout_centerInParent="true" to element which you want to center in the RelativeLayout
Upvotes: 2
Reputation: 151
I was able to center a view using
android:layout_centerHorizontal="true"
and
android:layout_centerVertical="true"
params.
Upvotes: 15
Reputation: 6209
If you want to center one view, use this one. In this case TextView must be the lowermost view in your XML because it's layout_height is match_parent.
<TextView
android:id="@+id/tv_to_be_centered"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:gravity="center"
android:text="Some text"
/>
Upvotes: 6
Reputation: 207838
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<ProgressBar
android:id="@+id/ProgressBar01"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_height="wrap_content"></ProgressBar>
<TextView
android:layout_below="@id/ProgressBar01"
android:text="@string/please_wait_authenticating"
android:id="@+id/txtText"
android:paddingTop="30px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
</RelativeLayout>
Upvotes: 19
Reputation: 3495
You can apply a centering to any View, including a Layout, by using the XML attribute android:layout_gravity". You probably want to give it the value "center".
You can find a reference of possible values for this option here: http://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html#attr_android:layout_gravity
Upvotes: 31