Garima Mathur
Garima Mathur

Reputation: 3352

how to get theme attributes (name and id) are used in for every single activity in android

I want to get current theme id and name for every single activity. I have used this code

int theme = 0;
        String themeName = null;

        try {
            String packageName = context.getPackageName();
            PackageInfo packageInfo = getPackageManager().getPackageInfo(packageName, PackageManager.GET_META_DATA);
            theme = packageInfo.applicationInfo.theme;
            themeName = getResources().getResourceEntryName(theme);
        } catch (Exception e) {
            e.printStackTrace();
        }

Manifest File code

<application
        android:name="com.ext.MyApp"
        android:allowBackup="true"
        android:icon="@drawable/launcher"
        android:theme="@style/AppSoundOnTheme" >

        <activity
            android:name="com.ext.SplashActivity"
            android:screenOrientation="portrait" >
        </activity>

    <activity
            android:name="com.ext.HomeActivity"
            android:screenOrientation="portrait" >
        </activity>
 </application>

styles.xml code

<style name="AppSoundOffTheme" parent="AppBaseTheme">

        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowBackground">@android:color/white</item>
        <item name="android:soundEffectsEnabled">false</item>
    </style>

     <style name="AppSoundOnTheme" parent="AppBaseTheme">

        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowBackground">@android:color/white</item>
        <item name="android:soundEffectsEnabled">true</item>
    </style>

and it returns same theme id and name for all activities. I think it gives application class's theme attributes. How can i find different theme attributes for all activities separately.

Upvotes: 1

Views: 2130

Answers (1)

SlashG
SlashG

Reputation: 680

Here's a quick way I found:

String packageName = getPackageName();
        PackageInfo packageInfo = getPackageManager().getPackageInfo(packageName, PackageManager.GET_ACTIVITIES|PackageManager.GET_META_DATA);          //Changed flags to fetch activies as well
        ActivityInfo[] activities = packageInfo.activities;                                                                                             //retrieve activities in the app
        for (ActivityInfo activityInfo : activities) {
            try {                                                                                                                                       //Traverse for every activity
                Log.d("theme", "Checking for " + activityInfo.name);
                int theme = activityInfo.getThemeResource();                                                                                            //retrieve 'int' resource ID
                String themeName = getResources().getResourceEntryName(theme);
                Log.d("theme", themeName);                                                                                                              //Retrieve theme name
            } catch (Exception e) {     
                e.printStackTrace();
            }
        }

It pretty much uses the block of code you've used, with a little tweak. Seemed to work for me, let me know if you still have trouble.

PS: You may want to put this block in a try-catch block.

Upvotes: 3

Related Questions