0

I have been trying to figure this out but I really dont get it.. If I want to create one app with one layout thats supposed to fit all android devices (by scaling).. how can I achive this?.. For instance.. I might have an editText thats 300dp, that will look great on my Samsung Galaxy S2.. but if I run the same app at my nexus 10, it will look very small, and I whould then like the editText to scale up on both width and height to fit my nexus 10.. is there any simple way to achive this?

I noticed that there is something called weight.. but if i created three editTexts where the first one had a weight of 10,the 2:nd a weight of 20 and then 3:rd a weight of 80, the last one never showed up...

Anyway.. how do you handle this stuff by best practice?

Kara
  • 6,115
  • 16
  • 50
  • 57
Inx
  • 2,364
  • 7
  • 38
  • 55
  • Use convention in the resources folder, the system will use high res etc. as needed for images, layouts, etc. – adrian Feb 21 '13 at 09:13

4 Answers4

0

Generally this situation can be handled by adding

<supports-screens android:normalScreens="true"
    android:smallScreens="true" android:largeScreens="true"
    android:anyDensity="true" android:resizeable="true" />

in the androidmanifest.xml

Sushma
  • 66
  • 1
  • 5
  • Could you please explain abit more what this actually does?.. Will this try to scale my views and elements automatically? – Inx Feb 21 '13 at 12:48
  • This tag in the manifest file only tells which screens your application supports. This would resize your application to the screen size. This works fine for most of the cases. In your case if the application doesn't look good in large screens even after adding this tag , you can consider having different layouts for different screen sizes – Sushma Feb 22 '13 at 05:46
0

Use the xhdpi resources and try to set all the layouts at the runtime using the layout params according to the device.

or let the android decide what is best for the device create following folders

  1. layout-large
  2. layout-small
  3. layout-sw600
  4. layout-sw720

and similar folders to organize your layouts better

Aashish Bhatnagar
  • 2,595
  • 2
  • 22
  • 37
  • so basically this is the way to go according to the android docs? Whouldnt this increse the .apk-size compared to only having one layout-file?... – Inx Feb 21 '13 at 21:05
  • well yes this is suggested approach the latter one first one is what I use to do earlier. you will be using different drawables as it is so different layouts won't cost much – Aashish Bhatnagar Feb 22 '13 at 05:20
  • how do I test on every resolution screen? – Harsh Shah May 31 '19 at 07:28
0

This document would be great help

http://developer.android.com/guide/practices/screens_support.html

If you your layout to look good on both phone and tablet, you will need to have separate layout files, one in layout and another in layout-xlarge (for the 10" tablet), put in different size for the edittext.

Another approach is to keep a single layout file but extract all the size related value to separate xml files. Put them in values and values-xlarge folders respectively.

Ethan
  • 1,616
  • 14
  • 8
  • Will try this later during this week, seems like a good approach... but if I want to target all the android devices.. then would I need to create one folder for each device/screen-size?.. and when is the break point for the different value-folders (when and how does android choose which folder to use?) – Inx Feb 21 '13 at 12:51
  • Usually I break all the devices in 4 major categories, small (phone less than 3.2"), large (small tablet around 7") and x-large (big tablet 8.9-10"), normal handle the rest. It works well for me so far, the only hiccup happened to me last time is the galaxy note which thinks it is a small tablet, lucky that since the last upgrade Samsung made it use the normal layout – Ethan Feb 25 '13 at 14:14
0

Here is how I do it,

Create a function that you can call that returns device width dependent size. The "32" is arbitrary which gives you a relative font size proportional to the screen.

textView.setTextSize(getFontSize(activity));


public static int getFontSize (Activity activity) { 

    DisplayMetrics dMetrics = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(dMetrics);

    // lets try to get them back a font size realtive to the pixel width of the screen
    final float WIDE = activity.getResources().getDisplayMetrics().widthPixels;
    int valueWide = (int)(WIDE / 32.0f / (dMetrics.scaledDensity));
    return valueWide;
}
Mark JW
  • 476
  • 3
  • 8