Search code examples
androiddeclare-styleablestyleable

getTheme().resolveAttribute() alternative on pre-lollipop


I have been struggling to obtain the styled attributes with the pre-lollipop API.

With lollipop, I use

final TypedValue statusBarColor = new TypedValue();
getTheme().resolveAttribute(android.R.attr.colorPrimaryDark, statusBarColor, true);
STATUS_BAR_COLOR = ContextCompat.getColor(this, statusBarColor.resourceId);

This works flawlessly, I haven't found a similar way to do this below API version 21. (minAPI = 16)

I tried using the getTheme().obtainStyledAttributes(). But, I don't have the AttributeSet to provide to that method since I am using this inside an activity. Am I doing things completely wrong, or is resolving styled attributes not supported on API versions below 21?


Solution

  • Try this code

      TypedValue typedValue = new TypedValue();
      getTheme().resolveAttribute(R.attr.colorPrimaryDark, typedValue, true);
      STATUS_BAR_COLOR = ContextCompat.getColor(this, typedValue.resourceId);
    

    No need of android.R.attr.colorPrimaryDark, instead you should use R.attr.colorPrimaryDark Thats all :)