Martin Nemeth
Martin Nemeth

Reputation: 679

Android: Positioning buttons when turned from portrait to landscape

I have a menu-activity, in which I have several positioned buttons. It fits my phone screen perfectly when in portrait mode, but in landscape mode it's destroyed. I know that when I have fixed positioning this will be the result. Can anyone tell me how to position my buttons so my activity keeps its form also in landscape mode? Thank you.

Here is my xml file:

  <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >


<Button
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:background="@drawable/bezpecnost_over"
     android:layout_marginLeft="30dp"
     android:layout_marginTop="50dp"


      />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/kurenie_over"
    android:layout_marginLeft="200dp"
    android:layout_marginTop="50dp"

     />

  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/osvetlenie_over"
    android:layout_marginLeft="200dp"
    android:layout_marginTop="200dp" 
    android:onClick="obrOsv"    />


<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:background="@drawable/pohodlie_over"
    android:layout_marginLeft="30dp"
    android:layout_marginTop="200dp"
    />

 </RelativeLayout>

Upvotes: 2

Views: 3710

Answers (2)

A--C
A--C

Reputation: 36449

It fits my phone screen perfectly when in portrait mode, but in landscape mode it's destroyed.

This is expected for relative-based layouts. What developers usually do in this scenario:

  • Make a folder under res called layout-land
  • Make an xml with the same name as the layout in portrait mode (contained in res/layout)
  • Position for landscape mode.

Since the xml file name is the same, when it is time for the activity to set the content view

setContentView (R.layout.my_layout);

The my_layout.xml file from the layout-land folder will be used instead.

Also read Layouts & Supporting Multiple Screens from the Android docs, they both have useful information with the latter talking about the different qualifiers (such as layout-land).

Upvotes: 3

Gabe Sechan
Gabe Sechan

Reputation: 93559

So you're using a relative layout to space out buttons horizontally by using increasing amounts of margin? That's totally not understanding how layouts work. Start by replacing the RelativeLayout with a LinearLayout with orientation set to horizontal, and get rid of all the margins- margins should just be a few pixels between elements. I'd suggest you read the Android tutorials on how layouts work.

Upvotes: 0

Related Questions