Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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> 
like image 200
Mercury Avatar asked Sep 26 '15 12:09

Mercury


People also ask

How do I design my own mobile app style?

Create and apply a style To create a new style or theme, open your project's res/values/styles. xml file. For each style you want to create, follow these steps: Add a <style> element with a name that uniquely identifies the style.

How do I create a style folder in Android?

Right click on res folder, choose New --> Android resource file, set the same name for the new file "styles", in Available qualifiers: choose the last item "Version" and finally set "Platform API level" 21. Show activity on this post.

Where is Android styles XML?

Defining Styles This XML file resides under res/values/ directory of your project and will have <resources> as the root node which is mandatory for the style file.


1 Answers

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

native static final void applyThemeStyle(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
like image 122
Renjith Thankachan Avatar answered Oct 10 '22 21:10

Renjith Thankachan