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 create the style in code rather than xml?
This is the example XML, need to create this programmatically in code:
<resources>
<style name="AppTheme" parent="android:Theme.Material">
<item name="android:colorPrimary">@color/primary</item>
<item name="android:colorPrimaryDark">@color/primary_dark</item>
<item name="android:colorAccent">@color/accent</item>
</style>
</resources>
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 ofContextWrapper
, which at the end callsAssetManager
with resource id pointer,AssetManager
class holds the method for applying application theme, which is JNI callAs above we can only pass a resource id to apply the themestyle. But possible options are
Window
class feature constants. We can use setFeatureDrawable & feature constants to set some drawables like,FEATURE_ACTION_BAR
,FEATURE_CONTEXT_MENU
etc..AjaySharma
&Nathan