1
votes

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

4
use android:layout_centerInParent = "true" - USKMobility
you mean on the parent or the children? - dec0yable
what exactly do u want ?? - Bajirao Shinde
Post your screen shot and explain what your needs. - Jay Rathod RJ

4 Answers

3
votes

try this:

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

<RelativeLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true">

<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>
</RelativeLayout>
1
votes

Try something like this.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


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

  <LinearLayout 
      android:orientation="vertical"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center"
      android:gravity="center"
      android:layout_centerInParent="true">

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

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

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

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

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

  </LinearLayout>

</RelativeLayout>

UPDATE:

If setting the xml in an AlertDialog, it's possible that there is a space allotted at the bottom. I google and found this alert_dialog.xml for reference. It seems that there is a buttonPanel at the bottom with a minimum height of 54dip.

<LinearLayout android:id="@+id/buttonPanel"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="54dip"
    android:orientation="vertical" >
    <LinearLayout
        style="?android:attr/buttonBarStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingTop="4dip"
        android:paddingStart="2dip"
        android:paddingEnd="2dip"
        android:measureWithLargestChild="true">
        <LinearLayout android:id="@+id/leftSpacer"
            android:layout_weight="0.25"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:visibility="gone" />
        <Button android:id="@+id/button1"
            android:layout_width="0dip"
            android:layout_gravity="start"
            android:layout_weight="1"
            style="?android:attr/buttonBarButtonStyle"
            android:maxLines="2"
            android:layout_height="wrap_content" />
        <Button android:id="@+id/button3"
            android:layout_width="0dip"
            android:layout_gravity="center_horizontal"
            android:layout_weight="1"
            style="?android:attr/buttonBarButtonStyle"
            android:maxLines="2"
            android:layout_height="wrap_content" />
        <Button android:id="@+id/button2"
            android:layout_width="0dip"
            android:layout_gravity="end"
            android:layout_weight="1"
            style="?android:attr/buttonBarButtonStyle"
            android:maxLines="2"
            android:layout_height="wrap_content" />
        <LinearLayout android:id="@+id/rightSpacer"
            android:layout_width="0dip"
            android:layout_weight="0.25"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:visibility="gone" />
    </LinearLayout>
 </LinearLayout>
1
votes
I think this may help you

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:gravity="center">
    <RelativeLayout
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Login"
        android:textStyle="bold"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/textMessage"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />
    <EditText
        android:layout_margin="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editUserSplash"
        android:hint="Username"
        android:layout_below="@+id/textMessage"
        android:gravity="center" />
    <EditText
        android:layout_margin="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editPasswordSplash"
        android:hint="Password"
        android:layout_below="@+id/editUserSplash"
        android:gravity="center" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Login"
        android:id="@+id/btnSplash"
        android:layout_below="@+id/editPasswordSplash"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

1
votes

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.