Skip to content Skip to sidebar Skip to footer

Get The Theme Value Applied For An Activity Programmatically

I want to know which theme is applied for an Activity in an application. Normally we are setting the theme by using setTheme(android.R.style.Theme_Light); Here we are specifying s

Solution 1:

Context class has a nice method called getThemeResId, however it's private thus you need to use reflection.

Here's an example:

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);

    Log.e("TAG", "Def theme: " + R.style.AppTheme);
    Log.e("TAG", "Light theme: " + android.R.style.Theme_Light);
    Log.e("TAG", "Current theme id: " + getThemeId());

    setTheme(android.R.style.Theme_Light);
    Log.e("TAG", "Current theme id: " + getThemeId());
}

intgetThemeId() {
    try {
        Class<?> wrapper = Context.class;
        Methodmethod= wrapper.getMethod("getThemeResId");
        method.setAccessible(true);
        return (Integer) method.invoke(this);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return0;
}

Post a Comment for "Get The Theme Value Applied For An Activity Programmatically"