Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Program different layouts for different versions in android

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?

like image 511
10ff Avatar asked Jan 18 '12 10:01

10ff


People also ask

How can use multiple layouts in one activity in Android?

You can use as many layouts as possible for a single activity but obviously not simultaneously. You can use something like: if (Case_A) setContentView(R. layout.

What are different types of layouts used in Android?

Android Layout TypesTableLayout is a view that groups views into rows and columns. AbsoluteLayout enables you to specify the exact location of its children. The FrameLayout is a placeholder on screen that you can use to display a single view. ListView is a view group that displays a list of scrollable items.

Can layouts be nested Android?

In Android all layout can be nested one another. In this example we create a Registration Form with multiple fields using Nested Linear Layouts. For that we set vertical orientation for parent Linear Layout and horizontal orientation for child Linear Layout.


2 Answers

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.

like image 137
curioustechizen Avatar answered Oct 12 '22 05:10

curioustechizen


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

like image 34
bindal Avatar answered Oct 12 '22 04:10

bindal