Eclipse Layout: How to support multiple screen sizes in landscape mode?

73 views Asked by At

If I want to support multiple screen sizes I work with the sizes small, normal, large and xlarge. That works fine in portrait-modus, but not in landscape Modus. For instance If I create a layout for a 3,2" device in landscape-modus, the same code doesn't fit for a 3,3" device in landscape Modus. Because it Looks not symetric in a 3,3" device then. What am I doing wrong?!

As I said: I use the same code for 3,2" and 3,3" device (RelativeLayout):

<Button
    android:id="@+id/Button01"
    android:layout_width="150dp"
    android:layout_height="75dp"
    android:layout_alignLeft="@+id/button1"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="50dp"
    android:text="Button" />

<Button
    android:id="@+id/Button02"
    android:layout_width="150dp"
    android:layout_height="75dp"
    android:layout_alignBaseline="@+id/button1"
    android:layout_alignBottom="@+id/button1"
    android:layout_marginLeft="25dp"
    android:layout_toRightOf="@+id/button1"
    android:text="Button" />

<Button
    android:id="@+id/Button03"
    android:layout_width="150dp"
    android:layout_height="75dp"
    android:layout_alignBaseline="@+id/Button01"
    android:layout_alignBottom="@+id/Button01"
    android:layout_alignLeft="@+id/Button02"
    android:text="Button" />

<Button
    android:id="@+id/button1"
    android:layout_width="150dp"
    android:layout_height="75dp"
    android:layout_above="@+id/Button01"
    android:layout_alignParentLeft="true"
    android:layout_marginBottom="46dp"
    android:layout_marginLeft="78dp"
    android:text="Button" />

2

There are 2 answers

0
Harsh Patel On BEST ANSWER

For supporting multiple screen you have to add Fragments in your activity. You did simple programming.You need to implement Fragments.

For that visit this and try out the tutorials

http://developer.android.com/guide/components/fragments.html

and

https://developer.android.com/training/basics/fragments/index.html

Here you can find the sample of Fragments.

All the Best.

0
Suman Dey On

I did it by catching runtime window height and width size and suggest how to do it with out any complexcity 1. Use Relative layout and maintain reference id. 2. Apply percentage of height or width to the component exeplictly. Example:

WindowManager wm = (WindowManager) context
                .getSystemService(context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        DisplayMetrics metrics = new DisplayMetrics();
        display.getMetrics(metrics);
        int width = metrics.widthPixels;
        int height = metrics.heightPixels;

In Activity handle UI component :

textView.setWidth(getSize(screenWidth, 30));
    textView.setHeight(getSize(screenHeight, 13));
    textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, getSize(screenHeight, 6));


public static int getSize(int screenSize, double percentage) {
    return (int) ((screenSize * percentage) / 100);
}