The problem with the buttons that look normal in portrait mode

And in Landskape go to the side

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#242a30" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true"> <GridLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignParentTop="true"> <Button android:id="@+id/btn1" android:layout_width="125dp" android:layout_height="125dp" android:layout_column="0" android:layout_marginLeft="47dp" android:layout_marginTop="50dp" android:layout_row="0" android:background="#fb6d59" android:text="Button" android:textColor="@android:color/background_light" /> <Button android:id="@+id/btn3" android:layout_width="125dp" android:layout_height="125dp" android:layout_column="0" android:layout_marginLeft="47dp" android:layout_marginTop="15dp" android:layout_row="1" android:background="#3cc1a4" android:text="Button" android:textColor="@android:color/background_light" /> <Button android:id="@+id/btn2" android:layout_width="125dp" android:layout_height="125dp" android:layout_column="1" android:layout_marginLeft="17dp" android:layout_marginRight="47dp" android:layout_marginTop="50dp" android:layout_row="0" android:background="#79aa37" android:text="Button" android:textColor="@android:color/background_light" /> <Button android:id="@+id/btn4" android:layout_width="125dp" android:layout_height="125dp" android:layout_column="1" android:layout_marginLeft="17dp" android:layout_marginRight="47dp" android:layout_marginTop="15dp" android:layout_row="1" android:background="#1893fd" android:text="Button" android:textColor="@android:color/background_light" /> </GridLayout> </LinearLayout> </RelativeLayout> 

enter image description here enter image description here

    2 answers 2

    You give them a specific size. So they do not stretch to all available space. To use all available space, use the layout_weight attribute.

    • weight doesn't help - Anton
    • @Anton, So put the code with weight . - Rostislav Dugin

    If you wish, you can create a separate layout file with the same name, but in the layout-land folder (right click on resources -> new -> Android recource directory -> Recource type, select layout, in the Available qualifiers - orientetion column, click the arrow button, in the drop-down list, select landscape) and write in this file a separate description for landscape orientation. When you rotate the device - the desired layout will be selected automatically. But in fact, even in portrait orientation, the buttons are not centered. (try on different devices - this is very noticeable)

    In general, I would advise you to use ConstraintLayout on top: you can draw it, and not prescribe it with your hands. Here is an approximate code that will look fine in all orientations:

     <android.support.constraint.ConstraintLayout xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" xmlns:android="http://schemas.android.com/apk/res/android"> <Button android:id="@+id/button" android:layout_width="111dp" android:layout_height="111dp" android:text="Button" android:layout_marginBottom="8dp" app:layout_constraintBottom_toTopOf="@+id/guideline2" app:layout_constraintRight_toLeftOf="@+id/guideline" android:layout_marginRight="8dp"/> <Button android:id="@+id/button2" android:layout_width="111dp" android:layout_height="111dp" android:text="Button" app:layout_constraintBaseline_toBaselineOf="@+id/button" android:layout_marginLeft="8dp" app:layout_constraintLeft_toLeftOf="@+id/guideline"/> <Button android:id="@+id/button3" android:layout_width="111dp" android:layout_height="111dp" android:text="Button" app:layout_constraintTop_toTopOf="@+id/guideline2" android:layout_marginTop="8dp" app:layout_constraintRight_toLeftOf="@+id/guideline" android:layout_marginRight="8dp"/> <Button android:id="@+id/button4" android:layout_width="111dp" android:layout_height="111dp" android:text="Button" app:layout_constraintBaseline_toBaselineOf="@+id/button3" android:layout_marginLeft="8dp" app:layout_constraintLeft_toLeftOf="@+id/guideline"/> <android.support.constraint.Guideline android:layout_width="1dp" android:layout_height="1dp" android:id="@+id/guideline" android:orientation="vertical" app:layout_constraintGuide_percent="0.5"/> <android.support.constraint.Guideline android:layout_width="wrap_content" android:layout_height="0dp" android:id="@+id/guideline2" app:layout_constraintGuide_percent="0.5" android:orientation="horizontal"/> </android.support.constraint.ConstraintLayout>