1

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?

Rao
  • 20,781
  • 11
  • 57
  • 77
tim687
  • 2,256
  • 2
  • 17
  • 28

1 Answers1

2

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 :)

Darish
  • 11,032
  • 5
  • 50
  • 70
  • That's a good one, I haven't even spotted that one, but this doesn't answer my question. The resolveAttribute method is not available on API 16 until 21 – tim687 Feb 15 '16 at 19:35
  • Are you using AppCompatActivity, then "resolveAttribute" is available there :) – Darish Feb 15 '16 at 19:38
  • Hmm, I don't know why Android Studio is complaining about this, but it isn't anymore now. Thanks! – tim687 Feb 15 '16 at 19:41