26

I started using a ContextThemeWrapper to apply a style dynamically to an ImageButton; based an answer to another question of mine, and answers to other similar questions.

ContextThemeWrapper wrapper = new ContextThemeWrapper(getContext(), mStyleRes);
mImageButton = new AppCompatImageButton(wrapper, null, 0);

But recently a lint error started appearing on the ContextThemeWrapper constructor stating:

ContextThemeWrapper can only be called from within the same library group (groupId=com.android.support)

I noticed that the class marked with the @RestrictTo(LIBRARY_GROUP) annotation, which is causing the lint error to appear. But I cannot find any information as to why it is restricted to the com.android.support library group.

As far as I can tell, this is the only way to apply a style, theme or theme overlay to a View programmatically; other than setting a default style attribute as the third argument in the constructor. So I am wondering why its use would be restricted at all; is there some issue with using the class outside of the support libraries? Could there be side-effects that I am not aware of?


The only similar question that I have come across is one about a (now fixed) bug; that caused this lint error to be displayed on the onCreate method of a subclass of AppCompatActivity. I do not think this occurrence is a bug, rather a deliberate restriction; which I would like to know the reasoning behind.

I should note; this restriction (as of now) actually seems to have no effect on the code using a ContextThemeWrapper. It compiles and runs fine, and works as I would expect it to.

Shirish Herwade
  • 11,461
  • 20
  • 72
  • 111
Bryan
  • 14,756
  • 10
  • 70
  • 125

1 Answers1

59

android.view.ContextThemeWrapper != android.support.v7.view.ContextThemeWrapper.

The support library one is annotated @RestrictTo(LIBRARY_GROUP), and also @hide - it's not meant to be a public API.

The first one is public.

ephemient
  • 198,619
  • 38
  • 280
  • 391
  • I see the same issue in `import android.support.v7.view.ViewPropertyAnimatorCompatSet` but I can't find an alternative, any ideas? – TheHebrewHammer Aug 22 '17 at 02:29
  • @TheHebrewHammer `android.support.v4.view.ViewPropertyAnimatorCompat` – ephemient Aug 22 '17 at 04:37
  • 5
    just changing the import from 'support.v7.view' to 'android.view', sometimes solution is too easy than you think :) ha ha – Shirish Herwade Nov 30 '17 at 06:53
  • I had to change it from `android.view.ContextThemeWrapper` to `androidx.appcompat.view.ContextThemeWrapper` to make a warning about unavailable on low API level disappear. The warning was **Call requires API level 23 (current min is 16): 'new android.view.ContextThemeWrapper'** – gregn3 Apr 08 '20 at 01:41