I suffered a lot trying all the programmatically solutions with no results. Finally I've done it from the XML file without any extra code within the Java class.
All I've done is Make the parent height match_parent
and set a gravity
for it with value 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" />