Skip to content Skip to sidebar Skip to footer

How To Programmatically Create An Android Theme Style?

There are lots of docs and tutorials on creating or customising an Android theme style via XML, but have not been able to find a way to create it in code. Any ideas on how to creat

Solution 1:

short answer: Its not possible as programmatically create a theme & set as application theme ( even if we achieved to create a Theme object) without a theme resource id.

Details:

when you call setTheme the function ineffect a method of ContextWrapper, which at the end calls AssetManager with resource id pointer, AssetManager class holds the method for applying application theme, which is JNI call

nativestaticfinalvoidapplyThemeStyle(long theme, int res, boolean force);

As above we can only pass a resource id to apply the themestyle. But possible options are

  1. Though its limited to Window class feature constants. We can use setFeatureDrawable & feature constants to set some drawables like, FEATURE_ACTION_BAR, FEATURE_CONTEXT_MENU etc..
  2. Using setTheme function from activity, we can set the theme from style resource, that will solve the problem mentioned in comments by AjaySharma & Nathan

Solution 2:

TL;DR: Just no, you can't. That's how Android works and will never be changed.


There is a plain and simple reason why programmatically creating Android themes will never be possible. That is, when an app is starting, Android creates a dummy window that shows the app's android:windowBackground. At this time, the app process is still being initialized, and when app execution starts and Activity.onCreate method of the activity being launched returns, the dummy window is replaced with the app window, which shows that activity. Therefore, the fact is, since android:windowBackground is set by themes but Android has to access it before the app is even started, themes have to be resources, so that they can be accessed from any process (including the system process, of course.)

Moreover, themes are resources, and as such they're completely immutable. That's how Android works. It cannot just be changed all of a sudden, and it's very unlikely it will ever. An additional reason why resources will never be able to be dynamically modified is due to the direct implication such that the APK itself would need to be modified as well — and that's not the way APKs are meant, either.

Someone may argue “everything must be done programmatically, under the hood.” Well, that's correct. However, the android.content.res.Resources.Theme class is final for a reason, which is to make sure that nothing be overridden, so that its behavior is guaranteed to reflect what resources say, which is fundamental for the system process accessing the android:windowBackground of the activity theme to be coherent with the way the app will behave once started. The same holds when it comes to the android.content.Context.obtainStyledResources method, which is final too. Indeed, if the app could override that method, it would be able to return values that do not match resources, which is a problem as that would happen only once the app process is started, when the original, real android:windowBackground had already been shown.

Post a Comment for "How To Programmatically Create An Android Theme Style?"