2

Suppose there is a view that performs problematically when using the GPU acceleration on it, yet only up to a specific Android version.

I wish to use android:layerType="software" for before this problematic version (including), and android:layerType="hardware" for after this version, all inside the resources.

I know how to do it with strings and simple resources (using simple qualifiers and multiple files), but how do I do it with this kind of resource? Where should I look?

Should I just use "integer" as a resource? or is there a better, more official way?

NOTE: I know how to do it programmatically. I wish to know how to do it within the resources.

android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • The manifest is application dependent unlike resources, therefore you can't make API specific manifest. Just stick to the programmatic way. – Simas Oct 12 '14 at 14:10
  • @user3249477 I didn't talk about the manifest. I talked about a view attribute: http://developer.android.com/reference/android/view/View.html#attr_android:layerType – android developer Oct 12 '14 at 14:18
  • Oh, my bad. What's wrong with qualifiers and multiple files then? – Simas Oct 12 '14 at 14:24
  • @user3249477 I'm just not sure if using constant integers (instead of their names) is considered the best way to do it. What's the correct way to do it? – android developer Oct 12 '14 at 14:30

1 Answers1

0

Since you can't access the static constants of the View class from xml, you should use API specific layouts. For example:

res/layout/layout.xml

<View
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layerType="software"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

res/layout-v11/layout.xml:

<View
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layerType="hardware"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

You could also <include layout="@layout/layout"/> in your bigger layouts so you don't have to copy everything.

However a much cleaner solution (especially if you need to set the layerType multiple times) would be to set it programmatically.

Simas
  • 43,548
  • 10
  • 88
  • 116
  • I wrote multiple resources for the value itself. not the layout. using multiple layouts can cause bad programming. The example of a view was just an example. Think what would happen if I had multiple views... – android developer Oct 12 '14 at 14:56
  • @androiddeveloper I understand what you mean, sort of. However since xml can't access class constants, you are left with setting the layer type programmatically, because something like an integer resource IMO would be considered *bad programming practice* also. – Simas Oct 12 '14 at 15:15
  • That's why I asked this question. I ask for the best practice. I also think there is sometimes a difference between setting it in code than XML. I've tried helping with this post : http://stackoverflow.com/q/26206324 . I've found out the issue there occurs on Android 4.0-4.2, and that you can solve it by using layerType="software" in XMl and reset it in code, but I think it should be better to set it in XML so that it will occur only for this problematic range of Android versions. – android developer Oct 12 '14 at 15:27