Home > DeveloperSection > Forums > How to Apply theme activity in android by programmatically?
Hugh Jackman
Hugh Jackman

Total Post:52

Points:366
Posted on    November-07-2014 1:36 AM

 Android Android  Android Styles  Android Activity 
Ratings:


 1 Reply(s)
 637  View(s)
Rate this:

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 style, As like this can we able to get the specific style type exactly applied for an activity programmatically.



David Miller

Total Post:30

Points:210
Posted on    November-07-2014 3:14 AM

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

Here's an example:

@Override
protected void onCreate(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());
}

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

Modified On Apr-05-2016 03:58:05 AM

Don't want to miss updates? Please click the below button!

Follow MindStick