Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically find the theme name in Android application

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.

like image 674
Carrie Hall Avatar asked Jan 17 '23 08:01

Carrie Hall


1 Answers

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.

like image 79
Shellum Avatar answered Jan 20 '23 14:01

Shellum