0

I'm developing an Android library that extends android.view.View. The library must adapt the theme choosen by developer whether it is DarkTheme or LightTheme. For example in LightTheme, the TextView color is black, on the contrary in DarkTheme the TextView color is white.

The problem is how to detect the theme choosen by developer? Is it dark or light? Is there any function like AppTheme instanceof DarkTheme?

Thanks in advance

Afrig Aminuddin
  • 772
  • 1
  • 9
  • 22
  • Your question has been already answered there : https://stackoverflow.com/questions/26301345/get-the-theme-value-applied-for-an-activity-programmatically . – magiccus Aug 03 '17 at 09:36

1 Answers1

0

The following method gives you theme name. From this theme name you can find whether it is a light theme or dark theme.

public String getThemeName(Context context){
   PackageInfo packageInfo;
   try{
    packageInfo = context.getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_META_DATA);
    int themeResId = packageInfo.applicationInfo.theme;
    return getResources().getResourceEntryName(themeResId);
   }
   catch (NameNotFoundException e){
    return null;
   }
}
Suneesh Ambatt
  • 1,347
  • 1
  • 11
  • 41