Program different layouts for different versions in android
Asked Answered
G

2

9

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?

Gwin answered 18/1, 2012 at 10:24 Comment(0)
E
17

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.

Engross answered 18/1, 2012 at 12:20 Comment(0)
I
6

You can find out the Android version looking at Build.VERSION.

The documentation recommends you check Build.VERSION.SDK_INT against the values in Build.VERSION_CODES.

And based on that version you can set your layouts in if else conditions

Idiot answered 18/1, 2012 at 10:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.