Android handling different screen resolutions


Handling Multiple Screen Dimensions, Part Two

Android handling different screen resolutions1 Android handling different screen resolutions

This is actually the first a part of a several part series on handling multiple screen dimensions inside your Android projects. These components is modified from the chapter within the Busy Coder’s Help guide to Android Development, Version 3..

The easiest method of handling multiple screen dimensions in Android would be to create your user connects so that they instantly scale for that display size, with no size-specific code or assets. Quite simply, &ldquoit just works&rdquo.

This suggests, though, that all you use within your interface could be beautifully scaly by Android which everything will fit, even on the QVGA screen.

Below are great tips for accomplishing this &ldquoall in a single&rdquo solution:
Don’t Consider Positions, Consider Rules

Some designers, possibly individuals from the &ldquodrag-and-drop&rdquo school of UI development, think first of all concerning the positions of icons. They believe they want certain icons to make certain fixed dimensions at certain fixed locations. They get frustrated with Android layout manager (containers) and could gravitate towards the deprecated AbsoluteLayout in an effort to design UIs they way they accustomed to.

That rarely is effective even on desktop computers, as possible seen by programs that don’t handle window resizing perfectly. Similarly, it won’t focus on mobile products, particularly Android, using its selection of screen dimensions and resolutions.

Rather than considering positions, consider rules. You have to train Android the &ldquobusiness rules&rdquo about where icons ought to be sized and placed, with Android then interpretation individuals rules based on exactly what the device’s screen really supports when it comes to resolution.

The easiest rules would be the fill_parent and wrap_content values for android:layout_width and android:layout_height. Individuals don’t specify specific dimensions, but instead adjust to the area available.

The wealthiest atmosphere for easily indicating rules is by using RelativeLayout. While complicated at first glance, RelativeLayout does a great job of allowing you to take control of your layout while still adapting it with other screen dimensions. For instance, you are able to:

Clearly anchor icons towards the bottom or right side from the screen, instead of wishing they’ll wind available online for thanks to another layout
Control the distances between icons which are &ldquoconnected&rdquo (e.g., a label for any area ought to be to the left from the area) without needing to depend on padding or margins

The finest control for indicating rules would be to make your own layout class. For instance, suppose you’re creating a number of programs that implement games. You might want to possess a layout class discussion about handmade cards: the way they overlap, that are face-up versus face lower, how large to become to deal with different quantity of cards, etc. When you could attain the preferred look with, say, a RelativeLayout, you might be better offered applying a PlayingCardLayout or perhaps a HandOfCardsLayout or something like that that’s more clearly customized for the application. Regrettably, creating custom layout classes is under-recorded at this era.
Consider Physical Dimensions

Android offers an array of available models of measure for dimensions. Typically the most popular continues to be the pixel (px), because you can easily &ldquowrap your mind around&rdquo the idea. In the end, all Android products may have screens with your-and-so quantity of pixels in every direction.

However, pixels become difficult as screen density changes. As the amount of pixels inside a given display size increases, the pixels effectively shrink. A 32px icon on the traditional Android device may be finger-friendly, but on the high-density device (say, WVGA inside a cell phone form factor), 32px might be a little small to be used having a finger.

For those who have something inherently scalable (e.g., a control button) that provided been indicating a size in pixels, you may consider switching to presenting millimeters (mm) or inches (in) because the unit of measure. 10mm is 10mm no matter the screen resolution or even the display size. By doing this, you are able to make sure that your widget is sized to become finger-friendly, no matter the amount of pixels that may take.
Avoid &ldquoReal&rdquo Pixels

In certain circumstance using millimeters for dimensions won’t seem sensible. Then, you may decide to think about using other models of measure while still staying away from &ldquoreal&rdquo pixels.

Android offers dimensions measured in density-independent pixels (dip). These map 1:1 to pixels for any 160dpi screen (e.g., a vintage HVGA Android device) and scale after that. For instance, on the 240dpi device (e.g., a telephone-sized WVGA device), the ratio is 2:3, so 50dip = at 160dpi = 75px at 240dpi. The benefit towards the user of choosing dip would be that the actual size the dimension stays exactly the same, so noticeably there’s no distinction between 50dip at 160dpi and 50dip at 240dpi.

Android also provides dimensions measured in scaly pixels (sp). Scaly pixels, theoretically, are scaly in line with the user’s selection of font size (FONT_SCALE value in System.Configurations).
Choose Scalable Drawables

Classic bitmaps &mdash PNG, Digital, Presen &mdash aren’t inherently scalable. If you’re not running in &ldquocompatibility mode&rdquo, Android won’t even attempt to scale them for you personally according to screen resolution and size. Whatever size bitmap you supply may be the size it will likely be, even when which makes the look too big or not big enough on some screens.

One method to address this really is to try and avoid static bitmaps, using nine-patch bitmaps and XML-defined drawables (e.g., GradientDrawable) as options. A nine-patch bitmap is really a PNG file specifically encoded to possess rules showing how that image could be extended to consider more room. XML-defined drawables make use of a quasi-SVG XML language to define shapes, their strokes and fills, and so forth.

The following publish within the series will appear at approaches for once the above techniques fail and you have to provide specific support for particular screen dimensions and densities.