59

I have theme that specifies textColor for TextView as red.

I am using LayoutInflater to instantiate TextView. The problem is that styles are not applied to TextView when inflater created using ApplicationContext - the color is not red. All works fine when LayoutInflater created using activity.

Why this happens, and how can be fixed?

/res/values/styles.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MyTheme">
        <item name="android:textViewStyle">@style/MyTextView</item>
    </style>

    <style name="MyTextView" parent="@android:style/Widget.TextView">
        <item name="android:textColor">#f00</item>
    </style>
</resources>

AndroidManifest.xml:

<application 
    android:icon="@drawable/icon" 
    android:label="@string/app_name"
    android:theme="@style/MyTheme"
    >

Code:

public class A extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_a);

        final LayoutInflater goodInflater = getInflater((Activity)this); 
        final LayoutInflater badInflater = getInflater(getApplicationContext());
        final LinearLayout container = (LinearLayout)findViewById(R.id.container);

        findViewById(R.id.add_with_appContext).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                add(container, badInflater); // Creates gray TextView
            }            
        });

        findViewById(R.id.add_with_activity).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                add(container, goodInflater); // Creates red TextView
            }            
        });
    }

    private LayoutInflater getInflater(Context context) {
        return (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    private void add(LinearLayout container, LayoutInflater inflater) {
        inflater.inflate(R.layout.my_template, container, true);
    }
}

/res/layout/test_a.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <Button 
        android:text="Add with AppContext" 
        android:id="@+id/add_with_appContext" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        />

    <Button 
        android:text="Add with Activity" 
        android:id="@+id/add_with_activity" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        />

    <LinearLayout
        android:id="@+id/container"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"  
        />

</LinearLayout>

/res/layout/my_template.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    >

    <TextView
        android:id="@+id/text"
        android:text="Some text..."
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
    />

</LinearLayout>
alex2k8
  • 42,496
  • 57
  • 170
  • 221

4 Answers4

58

Solution # 1

The inflate method accepts optional 'ViewGroup root' argument:

public View inflate (int resource, ViewGroup root, boolean attachToRoot)

If we have value to pass as 'root' parameter, than hence we can use it to get 'activity context' from where we can get correct LayoutInflater:

ViewGroup root > activity context > LayoutInflater

So my code could be:

private void add(LinearLayout container) {
    LayoutInflater inflater = getInflater(container.getContext());
    inflater.inflate(R.layout.my_template, container, true);
}

Solution # 2

Just tried to set Application Context theme programmatically, and it works:

getApplicationContext().setTheme(R.style.MyTheme);

I think it was logical to expect this markup:

<application 
    android:icon="@drawable/icon" 
    android:label="@string/app_name"
    android:theme="@style/MyTheme"
    >

to set it automatically, but it does not.

alex2k8
  • 42,496
  • 57
  • 170
  • 221
  • 2
    This is so wrong. There's a reason it doesn't work the way you're think it works: it's not supposed to work that way. The theme attribute on the tag is simply the default that gets applied to all activities. A lot of other things don't really work with the app context, for instance, you will leak resources that change in response to configuration changes, etc. – dcow Mar 29 '17 at 08:18
  • 1
    Solution #2 worked like a charm. My application looks gorgeous now. Thanks! – Mauker May 16 '17 at 23:59
  • 4
    **Never use an Application Context to inflate views** ( http://stackoverflow.com/a/25116293/570168) – Tobias May 17 '17 at 12:41
  • worked superb in custom created Dialogs .. you're the hero of my week – kilian eller Jan 19 '18 at 12:01
40

Never use an Application Context to inflate views, because styling doesn't work with this context. Always use an Activity's context when playing with views. The only exception is when you need to create RemoteViews from a Service.

More info about the different types of Contexts and their capabilities can be found in this excellent article.

BladeCoder
  • 12,779
  • 3
  • 59
  • 51
  • 2
    That's not really true, using Application Context for creating views is also recommended to avoid memory leaks with which I'm dealing right now. – David Sep 30 '14 at 08:50
  • 3
    Depends on how your views are used. If your views are not part of an Activity then you should not use the Activity's context of course. – BladeCoder Oct 02 '14 at 09:18
  • Wow. This did the trick. I had a problem with the Spinner items not honoring the theme and turns out that I was making this mistake and also in the ArrayAdapter I was using `getContext()` to inflate the child-views instead of using the parent view's context by calling `parent.getContext()` where `parent` is of type `ViewGroup`. – praneetloke Dec 31 '16 at 02:56
  • 1
    @David NO NO dear god NO. What are you doing?! You may be avoiding leaking resources but you're still leaking those views or at best a bunch of views are being created that you never use if you're holding onto the same view references all the time. There is no world in which using the application context to avoid memory leaks due to shitty code you don't understand is a good thing let alone _recommended_. – dcow Mar 29 '17 at 08:22
14

You probably use a context that isn't one that has a theme.

To solve it, use something as such:

val inflater= LayoutInflater.from(context).cloneInContext(ContextThemeWrapper(context, R.style.some_activity_theme))

You can read more about this here

android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • solved my problem with this E / ThemeUtils: View class com.airbnb.lottie.LottieAnimationView is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant) – AllanRibas Oct 15 '20 at 21:04
  • Also working with RemoteViews, inflated from Service. Thank you. – masoomyf Sep 19 '21 at 20:31
  • This looks like a better approach than applying the theme to the applicationContext, which might have other side-effects. – Tharkius Jun 09 '23 at 11:50
0

I usually come across this issue on inflating a custom view. Here is what I personally do to keep the same theme of the activity in the CustomView

public class CustomView extends ViewGroup{

public CustomView (Context context) {
    super(context);
    init(context);
}

public CustomView (Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context);
}

public CustomView (Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init(context);
}

@TargetApi(21)
public CustomView (Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    init(context);
}

private void init(Context context) {
    LayoutInflater  mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = mInflater.inflate(R.layout.review_list_item, this, true);
    //rest of view initialization       
}   
}
abedfar
  • 1,989
  • 24
  • 21