Search code examples
androidandroid-layoutandroid-relativelayoutgravity

relative layout gravity center not aligning correctly


I'm trying to center the chilldren of my relative layout in the center of my screen but it's acting like it's aligned to the top of the parent and I can't figure out why.

my .XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:gravity="center"
    android:background="#f70909">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView2"
        android:adjustViewBounds="true"
        android:src="@drawable/dice"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:id="@+id/textMessage"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/imageView2" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editUserSplash"
        android:hint="Username"
        android:gravity="center"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/textMessage" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editPasswordSplash"
        android:hint="Password"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:layout_below="@+id/editUserSplash" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Login"
        android:id="@+id/btnSplash"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/editPasswordSplash" />

    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/prgSplash"
        style="@android:style/Widget.DeviceDefault.ProgressBar.Large"
        android:layout_centerHorizontal="true"
        android:indeterminate="true"
        android:layout_below="@+id/btnSplash" />

</RelativeLayout>

I've tried making the parent a relativelayout without success and it won't align to the bottom either. Initially I thought the layout wasn't filling the whole screen but since its height and width are match_parent I don't think that's the problem. in android studio it is displaying correctly though so I must be missing something small.

I also tried using the gravity and layoutgravity parameters and a combination of the two but without success

Edit:screenshot I need the views to stay in the same formation relative to each other but centered in the screen vertically.

Edit 2:I set the background of the RelativeLayout to red and got this:red background So the relativelayout isn't filling my screen.

Edit 3: archi rayan Edit 4: archi rayan


Solution

  • Thanks to ank I was able to figure out that the reason why the RelativeLayout didn't fill my screen is that I used it in an alertDialog. So RelativeLayouts parent isn't the screen but the alertDialog. Since an alertDialog wraps its content it doesn't fill the entire screen.