Title: Views created progmatically aren't inheriting theme
Tags: android;android-theme;android-styles
Question: I am trying to create a view pragmatically and then add it to my activity. This bit is working fine, however the theme for the view group isn't inherited by my new view
My theme:
```<style name=&quot;CustomButtonTheme&quot; parent=&quot;@style/Widget.AppCompat.Button&quot;&gt;
  <item name=&quot;android:textColor&quot;&gt;#FF0000</item&gt;
  <item name=&quot;android:background&quot;&gt;#00FF00</item&gt;
</style&gt;
```
My layout:
```<?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
<LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    android:id=&quot;@+id/buttonArea&quot;
    android:layout_width=&quot;wrap_content&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:orientation=&quot;vertical&quot;
    android:theme=&quot;@style/CustomButtonTheme&quot;&gt;

    <Button
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:text=&quot;This button inherits CustomButtonTheme&quot; /&gt;
</LinearLayout&gt;
```
Java code
```AppCompatButton button = new AppCompatButton(getContext());
button.setText(&quot;This button does not inherit CustomButtonTheme&quot;);

LinearLayout buttonArea = findViewById<LinearLayout&gt;(R.id.buttonArea);
buttonArea.addView(button);
```

Comment: This is perfect! The second method has the added benefit that I don't need to hard code the theme in the context wrapper. If you add this as an answer I'll mark it on here to give you the well deserved credit. Thanks :)

Comment: That `theme` attribute in your layout will only have effect during inflation. It won't be applied to your `Activity`'s overall theme. All it does, however, is cause the `LayoutInflater` wrap its current `Context` with a `ContextThemeWrapper`. You can do the same; e.g., `ContextThemeWrapper wrapper = new ContextThemeWrapper(getContext(), R.style.CustomButtonTheme);`, `... new AppCompatButton(wrapper);`.

Comment: I just realized that there's a simpler way. Use the `LinearLayout`'s `Context` to create the `AppCompatButton`; i.e., `... new AppCompatButton(buttonArea.getContext());`. No need for your own separate `ContextThemeWrapper`.

Here is the accepted answer: An ```android:theme``` attribute in a layout will only have effect during inflation, and only on that particular subtree. It won't be applied to the ```Activity```'s overall theme.
All that attribute does, though, is cause the ```LayoutInflater``` to wrap its current ```Context``` with the specified theme in a ```ContextThemeWrapper```. We could do something similar ourselves, and just to illustrate the basic usage:
```ContextThemeWrapper wrapper = new ContextThemeWrapper(getContext(), R.style.CustomButtonTheme);
AppCompatButton button = new AppCompatButton(wrapper);
```
However, this has already been done for us, basically, when the ```LayoutInflater``` created a ```ContextThemeWrapper``` internally, for that ```android:theme``` attribute. That ```ContextThemeWrapper``` is the ```Context``` that the ```LinearLayout``` will have been created with, so we can simply use its ```Context``` to instantiate our ```AppCompatButton```:
```AppCompatButton button = new AppCompatButton(buttonArea.getContext());
```
As the OP points out, this has the added benefit of working in pretty much every similar setup without having to know the exact theme needed.



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2020:00:07/01/20', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<android><android-theme><android-styles>', 'Title': "Views created progmatically aren't inheriting theme", 'ViewCount': 117, 'id': 62686302, 'user_AboutMe': '<p>Software engineer living in Scotland</p>\n', 'user_AccountId': 2976550.0, 'user_DisplayName': 'MrK', 'user_Reputation': 652.0, 'user_Views': 55.0, 'user_id': 2527493.0}}