0

I am writing an app that works very well on 720p screens. I want to make this app run on different screen sizes. I read this article: http://developer.android.com/guide/practices/screens_support.html

But I am confused. I made 3 folders for different layouts (layout-normal, layout-large, layout-xlarge) and placed a different XML file in each folder. But there are some screens that have layout-normal and my apps look good on some of them and look bad on others. For example, these sizes are normal layout: (4.7" WXGA, 4" WVGA, 3.7" WVGA, ...) But my app looks good on 4" and 3.7" and on other types looks very bad.

My activity-main.xml in the layout-normal folder is:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/backasli"
    android:orientation="vertical" >

    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="100dp"
        android:paddingTop="140dp"
        android:stretchColumns="3" >

        <TableRow
            android:id="@+id/tableRow6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center" >

            <TextView
                android:id="@+id/txt1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="pray :"
                android:textColor="#031a7b"
                android:textSize="15sp" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center" >

            <TextView
                android:id="@+id/namaztxt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:textColor="#031a7b"
                android:textSize="20sp" />
        </TableRow>

        <TableRow android:gravity="center" >

            <Button
                android:id="@+id/buttonchange"
                android:layout_width="fill_parent"
                android:layout_height="50dp"
                android:background="@drawable/ubtn"
                android:text="change"
                android:textColor="#ffffff"
                android:textSize="20sp" />
        </TableRow>
    </TableLayout>

</LinearLayout>
JDJ
  • 4,298
  • 3
  • 25
  • 44
user2855778
  • 137
  • 3
  • 19
  • I think android:paddingBottom="100dp" android:paddingTop="140dp" this will makes problem for your device alignment?? Bad in the sense how it displayed?? – Lokesh Mar 05 '14 at 07:11
  • @Lokesh . exactly!!! these pads make problem – user2855778 Mar 05 '14 at 07:15
  • Then change padding as per your screens in all your layout.. – Lokesh Mar 05 '14 at 07:21
  • @Lokesh, many screen sizes are in layout-normal and these code is good for some of them. should I add layout for each screen? like layout-normal-3.7" ,layout-normal-4" , etc? – user2855778 Mar 05 '14 at 07:25
  • That's not a good idea by maintain huge layouts... Make three layouts enough.. layout(normal mobile),layoutsw600dp(7inchTab), layoutw700dp(10InchTab) – Lokesh Mar 05 '14 at 07:28
  • @Lokesh . I said that I make three layouts but my problem is many screens are in layout-normal. 3.7" and 4" and 4.65" are normal. but this code works good for some of them – user2855778 Mar 05 '14 at 07:31
  • 1
    Just one layout file is needed. No more. Code smartly. Atmost, use styles to alter weights for really big screens. – Siddharth Mar 05 '14 at 09:42

1 Answers1

2

Android provides a very good way to support multiple screen sizes. It's called weights. You should use LinearLayout weights and check out your Graphical View to figure out how you want to resize.

Completely avoid:

  1. Calculating your resolutions and screen sizes. You will end up in a mess.
  2. Multiple layouts across dpi's.

Trust me, all the apps I have developed until now (12) have been done using weights with 1 layout file across all platforms. It works, and works like wonder.

Edit:

I also never use more than 5dp for padding and 10dp for margins. This way I keep the code clean and Android weights take care of my free space.

For more about weights, check out this question. It is really well explained to start with.

Edit Again:

Think about it. You are assuming dp to be the "same" in terms of physical mm (millimeters) for all phones. But it is not. In fact X and Y 1 dp may mean something different in "mm". dp is "not" consistent. Be careful when using code like 100dp and setting hardcoded widths and heights. And even worse, multiple layout files. You will get into a lot of code maintenance nightmares with those.

Community
  • 1
  • 1
Siddharth
  • 9,349
  • 16
  • 86
  • 148
  • ohk @siddhath :) actually a i also not use hardcode and earlier i explain it but i didnt like thn i give this trick – Bhanu Sharma Mar 05 '14 at 07:41
  • can you explain more? for example should I change padding from 100dp to `weight*100dp` and control weight in java code? – user2855778 Mar 05 '14 at 07:41
  • OK.**weights** is best idea...thanks alot. i found using this in :[link](http://www.coderanch.com/t/610485/Android/Mobile/set-size-elements-layout-percent) – user2855778 Mar 05 '14 at 09:37
  • Think about it. You are assuming dp to be "same" in terms of physical mm (millimeter) for all phones. But it is not. Infact X and Y 1 dp, may mean different in "mm". dp is "not" consistent. Becareful when using code like 100dp and setting hardcoded width, height. And even worse multiple layout files, you will get into a lot of code maintenance nightmares. – Siddharth Mar 05 '14 at 09:41