4

I have a tablet application which I am rebranding so there are multiple themes, based on the type of user.

I'd like to find the name of the theme that is currently being applied, and based on that theme then I can do some back-end functionality changes.

I have to dynamically set some image resources, which is fine as long as I pass in the correct theme resource (the R.style.redtheme), but I'd like to set this dynamically.

TypedArray a = getTheme().obtainStyledAttributes(R.style.redtheme, new int[] {aTabResource.mDrawableAttrId}); 

To do the styling I'm creating custom attributes and then overriding them in the styles.

If there is no simple way to get the theme, I will just save a preference.

1 Answer 1

9

The package manager has access to quite a bit of metadata.

It can be accessed like this:

int theme = 0; //0==not set try { String packageName = getClass().getPackage().getName(); PackageInfo packageInfo = getPackageManager().getPackageInfo(packageName, PackageManager.GET_META_DATA); theme = packageInfo.applicationInfo.theme; } catch (Exception e) { e.printStackTrace(); } 

After this runs, theme will have the style resource.

Sign up to request clarification or add additional context in comments.

3 Comments

Had a bit of a problem with this, it gets the wrong package name. Works if you change the first line to String packageName = mContext.getPackageName(); where mContext is the context of the Activity ie private void createActivity(Bundle savedInstanceState){ setContentView(R.layout.welcome_layout); mContext = this;
getClass().getPackage() is not the package we are looking for. mContext.getPackageName() should be used
the getClass().getPackage() returns the application's package. mContext.getPackageName() returns the package name corresponding to the current file.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.