Search code examples
androidxmlandroid-layout

android - button not getting centered vertically


After I run this it displays this on the top not the center. But in xml preview it's centered .

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_gravity="center_vertical"
    tools:context=".MainActivity"

    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/_1"
        android:textSize="25sp"
        android:layout_gravity="center"
        />
    <Button
        android:id="@+id/roll_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/roll"
        android:layout_gravity="center"
        />
</LinearLayout>

what changes it was running fine but after adding Toast it started happening IG.


Solution

  • use this

    android:layout_gravity="center_vertical|center_horizontal"
    

    or use this for center vertical

    android:layout_gravity="center_vertical"