I suffered a lot from all software solutions without any results. Finally, I made it from an XML file without additional code in the Java class.
All I did was set the parent height match_parent and set its gravity to center
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" <!--The following two lines--> android:layout_height="match_parent" android:gravity="center" android:orientation="vertical"> <Button android:id="@+id/button_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="20dp" android:background="@android:color/white" android:gravity="center" android:padding="10dp" android:text="@string/hide" android:textColor="@android:color/holo_blue_dark" /> <Button android:id="@+id/button_2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:background="@android:color/white" android:gravity="center" android:padding="10dp" android:text="@string/cancel" android:textColor="@android:color/holo_blue_dark" />
source share