Theme/Style Is Not Applied When Inflater Used with Applicationcontext

Theme/Style is not applied when inflater used with ApplicationContext

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.

LayoutInflater not displays the xml properly

Maybe you have to change your res/values/styles.xml params.
With Android Studio 2.0 you can use the Theme Editor. Pretty nice if you don't need special buttons etc.

Or you can add custom colos for each view. For example add

android:textColor="@color/yourcolor1"
android:background="@color/yourcolor2"

to your text view. res/values/colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="yourcolor1">#FFFFFF</color>
<color name="yourcolor2">#CCCCCC</color>
</resources>

Change your button attributes too like this (just examples):

android:textColor="@android:color/black"
android:background"@android:color/darker_gray"

ListView doesn't get styled with theme when using custom adapter

Arghh, I just found the answer, with a hint from Use android.R.layout.simple_list_item_1 with a light theme

The trick is to remove getActivityContext() and to pass in the activity object directly when creating the adapter. Sad but effective.

LayoutInflater not displays the xml properly

Maybe you have to change your res/values/styles.xml params.
With Android Studio 2.0 you can use the Theme Editor. Pretty nice if you don't need special buttons etc.

Or you can add custom colos for each view. For example add

android:textColor="@color/yourcolor1"
android:background="@color/yourcolor2"

to your text view. res/values/colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="yourcolor1">#FFFFFF</color>
<color name="yourcolor2">#CCCCCC</color>
</resources>

Change your button attributes too like this (just examples):

android:textColor="@android:color/black"
android:background"@android:color/darker_gray"

Android: Do I just have one shot per property through the use of themes? Is there nothing like CSS in Android?

In response to your comments you might find what your looking for with the property textAppearance.

Open the themes.xml file here:

https://android.googlesource.com/platform/frameworks/base/+/froyo-release/core/res/res/values/themes.xml

If you do a find for textAppearance you notice various entries such as:

<style name="Theme">
<item name="textAppearanceButton">@android:style/TextAppearance.Widget.Button</item>
...
<item name="panelTextAppearance">?android:attr/textAppearanceInverse</item>
...

This allows you to some extent to style Components throughout your application as shown in the Maragues example.

So whilst its not possible to fully control the style of all child elements withint say a listview, scroll view etc, there is some limited support for controlling the styles some Text based components such as the TextView and or Button.



Related Topics



Leave a reply



Submit