Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing themed applications in Android

I am writing an application in Android and want users to be able to download themes and/or create ones themselves. The app would come with some default images and either themes could be installed from Market/as other APKs or could be put on sdcard - either would be fine.

What I want is: - allow themes to override images in buttons - right now I have XML styles to do things like highlighting when selected ( for button background + for combining button bg with actual button images) - allow themes to override background and text colors for certain things (I'm not using styles there for now, but I could if that would make things easier)

I am wondering if it's simply possible to override style definitions in my Application/Activity's onCreate based on configuration - I wouldn't mind user having to restart the app after style change.

Also, if it would be another APK, how would my main APK get it - by enumerating packages and filtering by package name?

Is there perhaps a good tutorial about it?

like image 426
wojciechka Avatar asked Aug 04 '11 06:08

wojciechka


1 Answers

This kind of thing isn't really supported as a standard facility in Android, so you are heading towards a lot of work to construct it yourself.

The first thing you need to know about is Context.createPackageContext(), which allows you to create a Context for another .apk (and thus load its resources). You can then use that Context to load the images and such. Note that these resources are completely distinct from your own app's resources, so you can't use standard facilities like themes or such to do the replacement. You will just need to manually put code everywhere you want to load a "themed" resources that explicitly retrieves it from this Context.

To discover the available .apks (and thus the package name to use with createPackageContext()), you can use the PackageManager facilities to do a query for components. For example, you can say that each theme .apk will have a like this:

<receiver android:name="something">
    <intent-filter>
        <action android:name="com.mydoman.action.THEME" />
    </intent-filter>
</receiver>

Now you can find all of the .apks with such receivers through PackageManager.queryBroadcastReceivers():

http://developer.android.com/reference/android/content/pm/PackageManager.html#queryBroadcastReceivers(android.content.Intent, int)

To select those with the above intent filters, use a Intent made with:

Intent intent = new Intent("com.mydoman.action.THEME");
like image 106
hackbod Avatar answered Sep 30 '22 17:09

hackbod