0
    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/dice_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        app:srcCompat="@drawable/empty_dice"
        tools:src="@drawable/dice_1" />

    <Button
        android:id="@+id/roll_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="@string/roll" />

</LinearLayout>

on design time i got the linearlayout centered verticaly , but when i test it on emulator or my device the linear layout is displayed at the top of the screen

Design Time

Run Time

Sh.Raai
  • 119
  • 1
  • 10

3 Answers3

3

In your LinearLayout, change layout_gravity to just gravity

Mike
  • 1,313
  • 12
  • 21
2

Don't

 android:layout_gravity="center_vertical"

You should use android:gravity="center_vertical"

Place object in the vertical center of its container, not changing its size

IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
  • 1
    android:gravity sets the gravity of the contents (i.e. its subviews) of the View it's used on. android:layout_gravity sets the gravity of the View or Layout relative to its parent. so that is not true ???!!! – Sh.Raai Mar 31 '20 at 06:00
  • 1
    @Sh.Raai exactly. – IntelliJ Amiya Mar 31 '20 at 06:02
  • @Sh.Raai read https://stackoverflow.com/questions/3482742/what-is-the-difference-between-gravity-and-layout-gravity-in-android – IntelliJ Amiya Mar 31 '20 at 06:12
0

In the layout:

change android:layout_height="wrap_content"

to

android:layout_height="match_parent"

Nikos Hidalgo
  • 3,666
  • 9
  • 25
  • 39