Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use obtainStyledAttributes(int []) with internal Themes of Android

Tags:

So I have looked around and found out that android.R.styleable is no longer part of the SDK even though it is still documented here.

That wouldn't really be an issue if it was clearly documented what the alternative is. For example the AOSP Calendar App is still using the android.R.styleable

// Get the dim amount from the theme    TypedArray a = obtainStyledAttributes(com.android.internal.R.styleable.Theme); lp.dimAmount = a.getFloat(android.R.styleable.Theme_backgroundDimAmount, 0.5f); a.recycle(); 

So how would one get the backgroundDimAmount without getting the int[] from android.R.styleable.Theme?

What do I have to stick into obtainStyledAttributes(int []) in order to make it work with the SDK?

like image 909
AGrunewald Avatar asked Jan 24 '10 13:01

AGrunewald


People also ask

What is obtainStyledAttributes in Android?

obtainStyledAttributes. Return a TypedArray holding the attribute values in set that are listed in attrs . In addition, if the given AttributeSet specifies a style class (through the "style" attribute), that style will be applied on top of the base attributes it defines.

What is typed array in Android?

Container for an array of values that were retrieved with Resources. Theme#obtainStyledAttributes(AttributeSet, int[], int, int) or Resources#obtainAttributes .


2 Answers

The CustomView API demo shows how to retrieve styled attributes. The code for the view is here:

https://github.com/android/platform_development/blob/master/samples/ApiDemos/src/com/example/android/apis/view/LabelView.java

The styleable array used to retrieve the text, color, and size is defined in the <declare-styleable> section here:

https://github.com/android/platform_development/blob/master/samples/ApiDemos/res/values/attrs.xml#L24

You can use <declare-styleable> to define any list of attributes that you want to retrieve as a group, containing both your own and ones defined by the platform.

As far as these things being in the documentation, there is a lot of java doc around the styleable arrays that makes them useful to have in the documentation, so they have been left there. However as the arrays change, such as new attributes being added, the values of the constants can change, so the platform ones can not be in the SDK (and please do not use any tricks to try to access them). There should be no need to use the platform ones anyway, because they are each there just for the implementation of parts of the framework, and it is trivial to create your own as shown here.

like image 108
hackbod Avatar answered Nov 09 '22 01:11

hackbod


In the example, they left out the reference to the Context 'c':

public ImageAdapter(Context c) {     TypedArray a = c.obtainStyledAttributes(R.styleable.GalleryPrototype);     mGalleryItemBackground = a.getResourceId(             R.styleable.GalleryPrototype_android_galleryItemBackground, 0);     a.recycle();     return mGalleryItemBackground; } 

Changing obtainStyledAttributes to c.obtainStyledAttributes should work

like image 44
kpasgma Avatar answered Nov 09 '22 01:11

kpasgma