2 * Copyright (C) 2006 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 import android.content.Context;
20 import android.content.ContextWrapper;
21 import android.content.res.Resources;
24 * A ContextWrapper that allows you to modify the theme from what is in the
27 public class ContextThemeWrapper extends ContextWrapper {
28 private Context mBase;
29 private int mThemeResource;
30 private Resources.Theme mTheme;
31 private LayoutInflater mInflater;
33 public ContextThemeWrapper() {
37 public ContextThemeWrapper(Context base, int themeres) {
40 mThemeResource = themeres;
43 @Override protected void attachBaseContext(Context newBase) {
44 super.attachBaseContext(newBase);
48 @Override public void setTheme(int resid) {
49 mThemeResource = resid;
53 @Override public Resources.Theme getTheme() {
58 if (mThemeResource == 0) {
59 mThemeResource = com.android.internal.R.style.Theme;
66 @Override public Object getSystemService(String name) {
67 if (LAYOUT_INFLATER_SERVICE.equals(name)) {
68 if (mInflater == null) {
69 mInflater = LayoutInflater.from(mBase).cloneInContext(this);
73 return mBase.getSystemService(name);
77 * Called by {@link #setTheme} and {@link #getTheme} to apply a theme
78 * resource to the current Theme object. Can override to change the
79 * default (simple) behavior. This method will not be called in multiple
80 * threads simultaneously.
82 * @param theme The Theme object being modified.
83 * @param resid The theme style resource being applied to <var>theme</var>.
84 * @param first Set to true if this is the first time a style is being
85 * applied to <var>theme</var>.
87 protected void onApplyThemeResource(Resources.Theme theme, int resid, boolean first) {
88 theme.applyStyle(resid, true);
91 private void initializeTheme() {
92 final boolean first = mTheme == null;
94 mTheme = getResources().newTheme();
95 Resources.Theme theme = mBase.getTheme();
100 onApplyThemeResource(mTheme, mThemeResource, first);