0

I am trying to achieve multi-screen support on my android app, so I provided several layout folders "layout-sw480dp", "layout-sw600dp", "layout-sw720dp" and same named xml files. I use 720dp layout as my main and everything looks nice on 10.1" tablet, but 4.3" phone fails to load corresponding layout. I have read tons of articles and different questions, but still can't figure out solution. Could anyone help me to solve this out?

Example of default xml layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/blackboard"
android:orientation="vertical" >

<TextView
    android:id="@+id/welcomeTextView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_marginTop="40dp"
    android:text="@string/welcomeLabel"
    android:textColor="@color/white"
    android:textSize="56sp" />

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginLeft="55dp"
    android:layout_marginTop="30dp"
    android:weightSum="100" >

    <TextView
        android:id="@+id/nameLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="5"
        android:text="@string/nameLabel"
        android:textColor="@color/white"
        android:textSize="26sp" />

    <EditText
        android:id="@+id/nameEditText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_weight="20"
        android:background="@color/white"
        android:textColor="@color/blue"
        android:textSize="26sp" />
</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingLeft="55dp"
    android:paddingTop="20dp"
    android:weightSum="100" >

    <TextView
        android:id="@+id/eqNumberLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="5"
        android:text="@string/eqNumberLabel"
        android:textColor="@color/white"
        android:textSize="26sp" />

    <EditText
        android:id="@+id/eqNumberEditText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_weight="10"
        android:background="@color/white"
        android:inputType="number"
        android:textColor="@color/blue"
        android:textSize="26sp" />
</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" 
    android:layout_marginTop="20dp">

    <CheckBox
        android:id="@+id/reducedCheckBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="55dp"
        android:layout_marginRight="10dp"
        android:button="@drawable/checkbox"
        android:layout_gravity="center" >
    </CheckBox>

    <TextView
        android:id="@+id/reducedLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/reducedLabel"
        android:textColor="@color/white"
        android:textSize="36sp" />
</LinearLayout>

<Button
    android:background="@drawable/begin_button"
    android:layout_marginTop="20dp"
    android:id="@+id/beginButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:enabled="false"
    android:text="@string/beginButton"
    android:textColor="@color/black" 
    android:textSize="36sp"/>

Moisej Braver
  • 317
  • 3
  • 14
  • What about just read carefully this article - http://developer.android.com/guide/practices/screens_support.html ? It will answer all your questions better than somebody can do it here. – Divers Mar 21 '15 at 18:51
  • I have read it at least 3 times and did all the actions as I said in the question, but still don't get the result. That's why I am asking here, not because I am lazy to read. – Moisej Braver Mar 21 '15 at 18:58
  • if you write "4.3 inch screen" it already means that you didn't get the whole idea of that article. Better don't follow advises like @Dave Wall gave you - it will make ruin all apps that you will do. – Divers Mar 21 '15 at 21:36
  • Would you mind give some brief explanation then? As I understood all screens are classified into four groups and in my case I have a normal screen device which mean if I support xml files for normal screen, the device should recognise and load them instead of the default xlarge layout. Am I right? And what's wrong with @Dave Wall approach to programmaticaly determine screen size and load appropriate layout? – Moisej Braver Mar 21 '15 at 22:16

2 Answers2

0

I had same problem. Now I just resize my layouts in onCreate, onConfigurationChanged, and at other times in code. This works for ANY size screen. Code below gives the devices actual pixels. Can you provide your default xml?

DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    final float width = dm.widthPixels;
    final float height = dm.heightPixels;
  • I added it to the original question above and thanks for the answer, I'll give it a try! – Moisej Braver Mar 21 '15 at 20:47
  • From your xml, it looks like you just need to adjust your text sizes, depending on the devices screen size. Also using Relative Layout will give you more control over the location spacing between your views. –  Mar 21 '15 at 21:18
  • Thank you very much, I used your method and it worked! Don't understand, why @Divers claims it will ruin my apps – Moisej Braver Mar 23 '15 at 08:42
0

Keep your resources for tablets in different folders according to their size and resolution: drawable-large : for 7' tablet drawable-xlarge: for 10' tablet to support phones, use following folders: drawable-mdpi drawable-hdpi drawable-xhdpi drawable-xxhdpi

P.S: there is difference screen size and resolution.

rajesh
  • 199
  • 1
  • 11