Program different layouts for different versions in android

10ff picture 10ff · Jan 18, 2012 · Viewed 10.2k times · Source

I have to program my Android app with the best compatibility to different screen sizes (tablets and smartphones) and to the versions 2.3, 3.2 and 4.0. I know about the possibliy to name the folders of the layout like res/layout-sw600dp/. to match the layout exactly to different screen sizes.

So, cause I will use completely different layouts for Tablets / Smartphones, I would start with a "Load Screen", which tests which Android-version is used and if there is used a tablet-or smartphone-device, cause for example in the layout for the Tablet, there will be Buttons on some screens that won't be there on the smartphone.

So I will have to separate all this in packages, like this:

  • Common
  • 2.3
    • Tablet
    • Smartphone
  • 3.2
    • Tablet
    • Smartphone
  • 4.0
    • Tablet
    • Smartphone

I would choose this separation, cause it's the best way, in my opinion, to be prepared for upcoming *updates*, if, let's say there should be added a Button in the ToolBar in Android 4.0.

Is this separation a good thing in your opinion or is this a complete overload?

Are there any other new features in 4.0 that I should be aware of?

Answer

curioustechizen picture curioustechizen · Jan 18, 2012

You can also name your resource folders to indicate version number (like /res/layout-v11) or (/res/values-v13)

Regarding your question about separation - it really depends on your particular requirement. A common approach to this problem is a fall-back mechanism: You provide a few specific layouts (for example, an xlarge landscape v11 layout), and a few generic ones to fall back to.

Of course, this is an "idealistic" solution; and it doesn't always work that way. In other words, it is mighty difficult to practically implement an app that is compatible with so many versions of Android solely by providing alternative resources. Even if you use the compatibility libraries (which helps a bit in reducing version-specific code); you will still have to adapt your code based on the version at run-time.