Site icon C1CTech

Android Styles and Themes

&NewLine;<p>This article is about Android Styles and Themes and how to use them in Android application with the help of simple examples&period;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<h3 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;000080&semi;"><strong>Style<&sol;strong><&sol;span><&sol;h3> &NewLine; &NewLine; &NewLine; &NewLine;<p class&equals;"p1">A <span style&equals;"color&colon; &num;008000&semi;"><strong>style<&sol;strong><&sol;span> is a collection of attributes that specify the appearance for a single <span style&equals;"color&colon; &num;0000ff&semi;"><strong><span class&equals;"s1">View<&sol;span><&sol;strong><&sol;span>&period; A style can specify attributes such as font color&comma; font size&comma; background color&comma; and much more&period;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<h4 class&equals;"wp-block-heading"><strong><span style&equals;"color&colon; &num;0000ff&semi;">Create and apply a style<&sol;span><&sol;strong><&sol;h4> &NewLine; &NewLine; &NewLine; &NewLine;<p>To create a new style&comma; open your project&&num;8217&semi;s <span style&equals;"color&colon; &num;0000ff&semi;"><strong>res&sol;values&sol;styles&period;xml<&sol;strong><&sol;span> file&period; For each style you want to create&comma; follow these steps&colon;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<ol class&equals;"wp-block-list"> &NewLine;<li>Add a <span style&equals;"color&colon; &num;008000&semi;"><strong>&lt&semi;style&gt&semi; <&sol;strong><&sol;span>element with a name that uniquely identifies the style&period;<&sol;li> &NewLine;<li>Add an<strong> <span style&equals;"color&colon; &num;008000&semi;">&lt&semi;item&gt&semi;<&sol;span><&sol;strong> element for each style attribute you want to define&period;<&sol;li> &NewLine;<&sol;ol> &NewLine; &NewLine; &NewLine; &NewLine;<p>For example&comma; if you define the following style&colon;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<pre class&equals;"wp-block-preformatted">&lt&semi;<strong>style name&equals;"LargeRedFont"<&sol;strong>&gt&semi; &NewLine; &lt&semi;<strong>item name&equals;"android&colon;textColor"<&sol;strong>&gt&semi;&num;C80000&lt&semi;&sol;<strong>item<&sol;strong>&gt&semi; &NewLine; &lt&semi;<strong>item name&equals;"android&colon;textSize"<&sol;strong>&gt&semi;40sp&lt&semi;&sol;<strong>item<&sol;strong>&gt&semi; &NewLine;&lt&semi;&sol;<strong>style<&sol;strong>&gt&semi; &NewLine;<&sol;pre> &NewLine; &NewLine; &NewLine; &NewLine;<p>You can apply the style with the <span style&equals;"color&colon; &num;0000ff&semi;"><strong><span class&equals;"s1">style<&sol;span><&sol;strong><&sol;span> attribute to a view as follows&colon;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<pre class&equals;"wp-block-preformatted">&lt&semi;<strong>TextView<br &sol;><&sol;strong> <strong>android<&sol;strong><strong>&colon;layout&lowbar;width<&sol;strong><strong>&equals;"match&lowbar;parent"<br &sol;><&sol;strong> <strong>android<&sol;strong><strong>&colon;layout&lowbar;height<&sol;strong><strong>&equals;"wrap&lowbar;content"<br &sol;> <span style&equals;"color&colon; &num;0000ff&semi;"> style<&sol;span><span style&equals;"color&colon; &num;0000ff&semi;">&equals;"&commat;style&sol;LargeRedFont"<&sol;span><br &sol;><&sol;strong> <strong>android<&sol;strong><strong>&colon;text<&sol;strong><strong>&equals;"TEXTVIEW STYLE"<br &sol;><&sol;strong> <strong>android<&sol;strong><strong>&colon;gravity<&sol;strong><strong>&equals;"center"<br &sol;><&sol;strong> &sol;&gt&semi;<&sol;pre> &NewLine; &NewLine; &NewLine; &NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>OUTPUT&colon;<&sol;strong><&sol;span><&sol;h5> &NewLine;<p><img class&equals;"alignnone wp-image-2318" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2020&sol;12&sol;Screenshot&lowbar;1608704555-e1608704784965&period;png" alt&equals;"" width&equals;"495" height&equals;"261" &sol;><&sol;p> &NewLine;<p> &NewLine; &NewLine;<&sol;p> &NewLine;<p><strong style&equals;"color&colon; &num;0000ff&semi; font-size&colon; 1&period;25em&semi;">Extend and customize a Style<&sol;strong><&sol;p> &NewLine;<p> &NewLine; &NewLine;<&sol;p> &NewLine;<p>To extend a style&comma; specify the style you want to extend with the <span style&equals;"color&colon; &num;008000&semi;"><strong>parent<&sol;strong><&sol;span> attribute in the <span style&equals;"color&colon; &num;0000ff&semi;"><strong>&lt&semi;style&gt&semi;<&sol;strong><&sol;span> element&period; You can then override the inherited style attributes and can add new ones&period;<&sol;p> &NewLine;<p> &NewLine; &NewLine;<&sol;p> &NewLine;<p><strong>For example&colon;<&sol;strong><&sol;p> &NewLine;<p> &NewLine; &NewLine;<&sol;p> &NewLine;<pre class&equals;"wp-block-preformatted"><span style&equals;"color&colon; &num;008000&semi;"><strong>&lt&semi;&excl;--extending the default button style &NewLine;and modify its attributes--&gt&semi;<&sol;strong><&sol;span> &NewLine;&lt&semi;<strong>style name&equals;"CustomButtonStyle" parent&equals;"&commat;android&colon;style&sol;Widget&period;Button"<&sol;strong>&gt&semi; &NewLine; &lt&semi;<strong>item name&equals;"android&colon;gravity"<&sol;strong>&gt&semi;center&lt&semi;&sol;<strong>item<&sol;strong>&gt&semi; &NewLine; &lt&semi;<strong>item name&equals;"android&colon;textColor"<&sol;strong>&gt&semi;&num;6200EA&lt&semi;&sol;<strong>item<&sol;strong>&gt&semi; &NewLine; &lt&semi;<strong>item name&equals;"android&colon;textStyle"<&sol;strong>&gt&semi;bold&lt&semi;&sol;<strong>item<&sol;strong>&gt&semi;<&sol;pre> &NewLine;<p> &NewLine; &NewLine;<&sol;p> &NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Extending own styles<&sol;strong><&sol;span><&sol;h5> &NewLine;<p> &NewLine; &NewLine;<&sol;p> &NewLine;<pre class&equals;"wp-block-preformatted"><span style&equals;"color&colon; &num;008000&semi;"><strong>&lt&semi;&excl;--defining own style--&gt&semi;<&sol;strong><&sol;span> &NewLine;&lt&semi;<strong>style name&equals;"LargeFont"<&sol;strong>&gt&semi; &NewLine; &lt&semi;<strong>item name&equals;"android&colon;textSize"<&sol;strong>&gt&semi;40sp&lt&semi;&sol;<strong>item<&sol;strong>&gt&semi; &NewLine;&lt&semi;&sol;<strong>style<&sol;strong>&gt&semi;<&sol;pre> &NewLine;<p> &NewLine; &NewLine;<&sol;p> &NewLine;<pre class&equals;"wp-block-preformatted"><strong><span style&equals;"color&colon; &num;008000&semi;">&lt&semi;&excl;--extending own style LargeFont using parent attribute--&gt&semi;<&sol;span><&sol;strong><br &sol;>&lt&semi;<strong>style <&sol;strong><strong>name<&sol;strong><strong>&equals;"LargeBlueFont" <&sol;strong><strong>parent<&sol;strong><strong>&equals;"&commat;style&sol;LargeFont"<&sol;strong>&gt&semi;<br &sol;> &lt&semi;<strong>item <&sol;strong><strong>name<&sol;strong><strong>&equals;"android&colon;textColor"<&sol;strong>&gt&semi;&num;00007f&lt&semi;&sol;<strong>item<&sol;strong>&gt&semi;<br &sol;>&lt&semi;&sol;<strong>style<&sol;strong>&gt&semi;<&sol;pre> &NewLine;<p> &NewLine; &NewLine;<&sol;p> &NewLine;<p>You can also inherit styles &lpar;only own styles&rpar; by extending a style&&num;8217&semi;s name with a dot notation&comma; instead of using the <span style&equals;"color&colon; &num;0000ff&semi;"><strong><span class&equals;"s1">parent<&sol;span><&sol;strong><&sol;span> attribute&period;<&sol;p> &NewLine;<pre><strong><span style&equals;"color&colon; &num;008000&semi;">&lt&semi;&excl;--extending own style LargeFont using dot notation--&gt&semi;<&sol;span><&sol;strong><br &sol;>&lt&semi;style name&equals;"LargeFont&period;LargeBlueFont"&gt&semi;<br &sol;> &lt&semi;item name&equals;"android&colon;textColor"&gt&semi;&num;00007f&lt&semi;&sol;item&gt&semi;<br &sol;>&lt&semi;&sol;style&gt&semi;<&sol;pre> &NewLine;<pre><strong><span style&equals;"color&colon; &num;008000&semi;">&lt&semi;&excl;--creating style Bold and<br &sol;>extending own style LargeBlueFont using dot notation--&gt&semi;<&sol;span><&sol;strong><br &sol;>&lt&semi;style name&equals;"LargeFont&period;LargeBlueFont&period;Bold"&gt&semi;<br &sol;> &lt&semi;item name&equals;"android&colon;textStyle"&gt&semi;bold&lt&semi;&sol;item&gt&semi;<br &sol;>&lt&semi;&sol;style&gt&semi;<&sol;pre> &NewLine;<pre><span style&equals;"color&colon; &num;008000&semi;"><strong>&lt&semi;&excl;--using the above style LargeBlueFont in xml--&gt&semi;<&sol;strong><&sol;span><br &sol;>&lt&semi;TextView<br &sol;> style&equals;"&commat;style&sol;LargeFont&period;LargeBlueFont"&sol;&gt&semi;<br &sol;><br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong>&lt&semi;&excl;--using the above style Bold in xml--&gt&semi;<&sol;strong><&sol;span><br &sol;>&lt&semi;TextView<br &sol;>style&equals;"&commat;style&sol;LargeFont&period;LargeBlueFont&period;Bold"&sol;&gt&semi;<&sol;pre> &NewLine;<p> &NewLine; &NewLine;<&sol;p> &NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Note&colon;<&sol;strong><&sol;span> To reference a built-in style you must use the <span style&equals;"color&colon; &num;000080&semi;"><strong>parent<&sol;strong><&sol;span> attribute&period;<&sol;p> &NewLine;<p> &NewLine; &NewLine;<&sol;p> &NewLine;<h3 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;000080&semi;"><strong>Themes<&sol;strong><&sol;span><&sol;h3> &NewLine;<p> &NewLine; &NewLine;<&sol;p> &NewLine;<p class&equals;"p1">A theme is a collection of attributes that&&num;8217&semi;s applied to an entire app&comma; activity&comma; or view hierarchy &lpar;not just an individual view&rpar;&period; Themes can also apply styles to non-view elements&comma; such as the <span style&equals;"color&colon; &num;008000&semi;"><strong>status bar<&sol;strong> <&sol;span>and <span style&equals;"color&colon; &num;008000&semi;"><strong>window background<&sol;strong><&sol;span>&period;<&sol;p> &NewLine;<p> &NewLine; &NewLine;<&sol;p> &NewLine;<h4 class&equals;"wp-block-heading"><strong><span style&equals;"color&colon; &num;0000ff&semi;">Create and apply a <&sol;span><&sol;strong><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Theme<&sol;strong><&sol;span><&sol;h4> &NewLine;<p> &NewLine; &NewLine;<&sol;p> &NewLine;<p class&equals;"p2">You can create a theme the same way you create styles&period; <&sol;p> &NewLine;<p> &NewLine; &NewLine;<&sol;p> &NewLine;<p>For example&comma; if you define the following theme&colon;<&sol;p> &NewLine;<p> &NewLine; &NewLine;<&sol;p> &NewLine;<pre class&equals;"wp-block-preformatted"><span style&equals;"color&colon; &num;008000&semi;"><strong>&lt&semi;&excl;--creating new theme&lpar;AppBaseTheme&rpar; by extending built-in theme<&sol;strong><&sol;span><br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong><span style&equals;"color&colon; &num;0000ff&semi;"><span style&equals;"color&colon; &num;008000&semi;">Theme&period;AppCompat&period;Light&period;DarkActionBar--&gt&semi;<&sol;span><&sol;span><&sol;strong><&sol;span><br &sol;>&lt&semi;style name&equals;"AppBaseTheme" parent&equals;"Theme&period;AppCompat&period;Light&period;DarkActionBar"&gt&semi;<br &sol;>&period;&period;&period;&period;<br &sol;>&lt&semi;&sol;style&gt&semi;<&sol;pre> &NewLine;<p> &NewLine; &NewLine;<&sol;p> &NewLine;<p class&equals;"p2">You can apply the theme with the <span style&equals;"color&colon; &num;0000ff&semi;"><strong><span class&equals;"s1">android&colon;theme<&sol;span><&sol;strong><&sol;span> attribute to your application&comma; specific activity or to a view in your layout file &lpar;beginning with Android 5&period;0 &lpar;API level 21&rpar;&rpar;&period; <&sol;p> &NewLine;<p> &NewLine; &NewLine;<&sol;p> &NewLine;<pre class&equals;"wp-block-preformatted"><strong><span style&equals;"color&colon; &num;008000&semi;">&lt&semi;&excl;--set AppBaseTheme for a particular activity &NewLine;in AndroidManifest&period;xml--&gt&semi;<&sol;span><&sol;strong> &NewLine;&lt&semi;<strong>manifest <&sol;strong>&period;&period;&period; &gt&semi; &NewLine;&lt&semi;<strong>application <&sol;strong>&period;&period;&period; &gt&semi; &NewLine;&lt&semi;<strong>activity android&colon;theme&equals;"&commat;style&sol;AppBaseTheme" <&sol;strong>&period;&period;&period; &gt&semi; &NewLine; &lt&semi;&sol;<strong>activity<&sol;strong>&gt&semi; &NewLine; &lt&semi;&sol;<strong>application<&sol;strong>&gt&semi; &NewLine; &lt&semi;&sol;<strong>manifest<&sol;strong>&gt&semi;<&sol;pre> &NewLine;<p> &NewLine; &NewLine;<&sol;p> &NewLine;<pre class&equals;"wp-block-preformatted"><span style&equals;"color&colon; &num;008000&semi;"><strong> &lt&semi;&excl;--set AppBaseTheme for all the activities in android application<&sol;strong><&sol;span><br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong> in AndroidManifest&period;xml--&gt&semi;<&sol;strong><&sol;span><br &sol;>&lt&semi;<strong>manifest <&sol;strong>&period;&period;&period; &gt&semi; &NewLine; &lt&semi;<strong>application android&colon;theme&equals;"&commat;style&sol;AppBase<strong>Theme<&sol;strong>" <&sol;strong>&period;&period;&period;&gt&semi; &NewLine; &lt&semi;&sol;<strong>application<&sol;strong>&gt&semi; &NewLine; &lt&semi;&sol;<strong>manifest<&sol;strong>&gt&semi;<&sol;pre> &NewLine;<p> &NewLine; &NewLine;<&sol;p> &NewLine;<h4 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Extend and customize a Theme<&sol;strong><&sol;span><&sol;h4> &NewLine;<p> &NewLine; &NewLine;<&sol;p> &NewLine;<p>To extend a theme&comma; specify the theme you want to extend with the <span style&equals;"color&colon; &num;008000&semi;"><strong>parent<&sol;strong><&sol;span> attribute in the <span style&equals;"color&colon; &num;0000ff&semi;"><strong>&lt&semi;style&gt&semi;<&sol;strong><&sol;span> element&period; You can then override the inherited attributes and add new ones&period;<&sol;p> &NewLine;<p> &NewLine; &NewLine;<&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine; &NewLine;<pre class&equals;"wp-block-preformatted">&lt&semi;resources&gt&semi;<br &sol;> <span style&equals;"color&colon; &num;008000&semi;"><strong>&lt&semi;&excl;-- Base application theme --&gt&semi;<&sol;strong><&sol;span><br &sol;> &lt&semi;style name&equals;"AppBaseTheme" parent&equals;"Theme&period;AppCompat&period;Light&period;DarkActionBar"&gt&semi;<br &sol;> &lt&semi;item name&equals;"colorPrimary"&gt&semi;&commat;color&sol;colorPrimary&lt&semi;&sol;item&gt&semi;<br &sol;> &lt&semi;item name&equals;"colorPrimaryDark"&gt&semi;&commat;color&sol;colorPrimaryDark&lt&semi;&sol;item&gt&semi;<br &sol;> &lt&semi;item name&equals;"colorAccent"&gt&semi;&commat;color&sol;colorAccent&lt&semi;&sol;item&gt&semi;<br &sol;> &lt&semi;&sol;style&gt&semi;<br &sol;><br &sol;> <strong><span style&equals;"color&colon; &num;008000&semi;">&lt&semi;&excl;-- customised app theme --&gt&semi;<&sol;span><&sol;strong><br &sol;> &lt&semi;style name&equals;"AppTheme" parent&equals;"&commat;style&sol;AppBaseTheme"&gt&semi;<br &sol;> <strong><span style&equals;"color&colon; &num;008000&semi;"> &lt&semi;&excl;-- custom properties --&gt&semi;<&sol;span><&sol;strong><br &sol;> &lt&semi;item name&equals;"android&colon;buttonStyle"&gt&semi;&commat;style&sol;Widget&period;Button&period;Custom&lt&semi;&sol;item&gt&semi;<br &sol;> &lt&semi;item name&equals;"android&colon;textViewStyle"&gt&semi;&commat;style&sol;Widget&period;TextView&period;Custom&lt&semi;&sol;item&gt&semi;<br &sol;> &lt&semi;&sol;style&gt&semi;<br &sol;><br &sol;> <strong><span style&equals;"color&colon; &num;008000&semi;"> &lt&semi;&excl;-- This is the custom button styles for this application --&gt&semi;<&sol;span><&sol;strong><br &sol;> &lt&semi;style name&equals;"Widget&period;Button&period;Custom" parent&equals;"android&colon;Widget&period;Button"&gt&semi;<br &sol;> &lt&semi;item name&equals;"android&colon;textColor"&gt&semi;&num;DD2C00&lt&semi;&sol;item&gt&semi;<br &sol;> &lt&semi;&sol;style&gt&semi;<br &sol;><br &sol;> <strong><span style&equals;"color&colon; &num;008000&semi;"> &lt&semi;&excl;-- This is the custom textview styles for this application --&gt&semi;<&sol;span><&sol;strong><br &sol;> &lt&semi;style name&equals;"Widget&period;TextView&period;Custom" parent&equals;"android&colon;Widget&period;TextView"&gt&semi;<br &sol;> &lt&semi;item name&equals;"android&colon;textColor"&gt&semi;&num;AA00FF&lt&semi;&sol;item&gt&semi;<br &sol;> &lt&semi;&sol;style&gt&semi;<br &sol;>&lt&semi;&sol;resources&gt&semi;<&sol;pre> &NewLine;<p> &NewLine; &NewLine;<&sol;p> &NewLine;<p>Now apply the theme<strong> &lpar;AppTheme&rpar;<&sol;strong> with the <span style&equals;"color&colon; &num;008000&semi;"><strong><span class&equals;"s1">android&colon;theme<&sol;span><&sol;strong><&sol;span> attribute on the <span style&equals;"color&colon; &num;0000ff&semi;"><strong><span class&equals;"s1">&lt&semi;application&gt&semi;<&sol;span><&sol;strong><&sol;span> tag in the <span style&equals;"color&colon; &num;0000ff&semi;"><strong><span class&equals;"s1">AndroidManifest&period;xml<&sol;span><&sol;strong><&sol;span> file&period; <&sol;p> &NewLine;<p> &NewLine; &NewLine;<&sol;p> &NewLine;<pre class&equals;"wp-block-preformatted">&lt&semi;application<br &sol;> <strong>android&colon;theme&equals;"&commat;style&sol;AppTheme"<&sol;strong>&gt&semi;<br &sol;>&lt&semi;&sol;application&gt&semi;<&sol;pre> &NewLine;<p> &NewLine; &NewLine;<&sol;p> &NewLine;<h5 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>OUTPUT&colon;<&sol;strong><&sol;span><&sol;h5> &NewLine;<p> &NewLine; &NewLine;<&sol;p> &NewLine;<p>Now&comma; In entire application&comma; all the buttons and textviews got set with the specific style&period;<&sol;p> &NewLine;<p> &NewLine; &NewLine;<&sol;p> &NewLine;<figure class&equals;"wp-block-image"><img class&equals;"wp-image-2322" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2020&sol;12&sol;Screenshot&lowbar;1608721730-e1608721810701-1024x792&period;png" alt&equals;"" width&equals;"470" height&equals;"364" &sol;><&sol;figure> &NewLine;<p> &NewLine; &NewLine;<&sol;p> &NewLine;<h3 class&equals;"wp-block-heading"> <&sol;h3> &NewLine;<p> &NewLine; &NewLine;<&sol;p> &NewLine;<h3 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;000080&semi;"><strong>Multiple Themes in app<&sol;strong><&sol;span><&sol;h3> &NewLine;<p> &NewLine; &NewLine;<&sol;p> &NewLine;<p>Android allow us to create multiple themes for our application&period; Let&&num;8217&semi;s understand how to create and apply multiple themes in app with simple example&period;<&sol;p> &NewLine;<p> &NewLine; &NewLine;<&sol;p> &NewLine;<p>1 &period; Create a layout file <span style&equals;"color&colon; &num;0000ff&semi;"><strong>attrs&period;xml<&sol;strong><&sol;span> inside <span style&equals;"color&colon; &num;000080&semi;"><strong>res&sol;values<&sol;strong><&sol;span> and add the below code&period; Here&comma; we can create custom attributes&period;<&sol;p> &NewLine;<p> &NewLine; &NewLine;<&sol;p> &NewLine;<p><span style&equals;"color&colon; &num;000080&semi;"><strong>attrs&period;xml<&sol;strong><&sol;span><&sol;p> &NewLine;<p> &NewLine; &NewLine;<&sol;p> &NewLine;<pre class&equals;"wp-block-preformatted">&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;<br &sol;>&lt&semi;resources&gt&semi;<br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong>&lt&semi;&excl;--custom attribute--&gt&semi;<&sol;strong><&sol;span><br &sol;> &lt&semi;attr name&equals;"backgroundColor" format&equals;"color"&sol;&gt&semi;<br &sol;>&lt&semi;&sol;resources&gt&semi;<&sol;pre> &NewLine;<p> &NewLine; &NewLine;<&sol;p> &NewLine;<p>2&period; Open <span style&equals;"color&colon; &num;0000ff&semi;"><strong>colors&period;xml<&sol;strong><&sol;span> inside <span style&equals;"color&colon; &num;000080&semi;"><strong>res&sol;values<&sol;strong><&sol;span> and add the below code&period;<&sol;p> &NewLine;<p> &NewLine; &NewLine;<&sol;p> &NewLine;<p><span style&equals;"color&colon; &num;000080&semi;"><strong>colors&period;xml<&sol;strong> <&sol;span><&sol;p> &NewLine;<p> &NewLine; &NewLine;<&sol;p> &NewLine;<pre class&equals;"wp-block-preformatted">&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;<br &sol;>&lt&semi;resources&gt&semi;<br &sol;> &lt&semi;color name&equals;"primaryColor&lowbar;blue"&gt&semi;&num;2196F3&lt&semi;&sol;color&gt&semi;<br &sol;> &lt&semi;color name&equals;"primaryColorDark&lowbar;blue"&gt&semi;&num;1976D2&lt&semi;&sol;color&gt&semi;<br &sol;> &lt&semi;color name&equals;"primaryAccent&lowbar;blue"&gt&semi;&num;E3F2FD&lt&semi;&sol;color&gt&semi;<br &sol;><br &sol;> &lt&semi;color name&equals;"primaryColor&lowbar;red"&gt&semi;&num;F44336&lt&semi;&sol;color&gt&semi;<br &sol;> &lt&semi;color name&equals;"primaryColorDark&lowbar;red"&gt&semi;&num;D32F2F&lt&semi;&sol;color&gt&semi;<br &sol;> &lt&semi;color name&equals;"primaryAccent&lowbar;red"&gt&semi;&num;FFEBEE&lt&semi;&sol;color&gt&semi;<br &sol;><br &sol;> &lt&semi;color name&equals;"primaryColor&lowbar;green"&gt&semi;&num;64DD17&lt&semi;&sol;color&gt&semi;<br &sol;> &lt&semi;color name&equals;"primaryColorDark&lowbar;green"&gt&semi;&num;00C853&lt&semi;&sol;color&gt&semi;<br &sol;> &lt&semi;color name&equals;"primaryAccent&lowbar;green"&gt&semi;&num;AEEA00&lt&semi;&sol;color&gt&semi;<br &sol;>&lt&semi;&sol;resources&gt&semi;<&sol;pre> &NewLine;<p> &NewLine; &NewLine;<&sol;p> &NewLine;<p>3&period; Open <span style&equals;"color&colon; &num;0000ff&semi;"><strong>style&period;xml<&sol;strong><&sol;span> inside <span style&equals;"color&colon; &num;000080&semi;"><strong>res&sol;values<&sol;strong><&sol;span> and add the below code&period;<&sol;p> &NewLine;<p> &NewLine; &NewLine;<&sol;p> &NewLine;<p><span style&equals;"color&colon; &num;000080&semi;"><strong>style&period;xml<&sol;strong> <&sol;span><&sol;p> &NewLine;<p> &NewLine; &NewLine;<&sol;p> &NewLine;<pre class&equals;"wp-block-preformatted">&lt&semi;style name&equals;"AppTheme&period;Blue" parent&equals;"Theme&period;AppCompat&period;Light&period;DarkActionBar"&gt&semi;<br &sol;> &lt&semi;item name&equals;"colorPrimary"&gt&semi;&commat;color&sol;primaryColor&lowbar;blue&lt&semi;&sol;item&gt&semi;<br &sol;> &lt&semi;item name&equals;"colorPrimaryDark"&gt&semi;&commat;color&sol;primaryColorDark&lowbar;blue&lt&semi;&sol;item&gt&semi;<br &sol;> &lt&semi;item name&equals;"colorAccent"&gt&semi;&commat;color&sol;primaryAccent&lowbar;blue&lt&semi;&sol;item&gt&semi;<br &sol;> &lt&semi;item name&equals;"backgroundColor"&gt&semi;&commat;color&sol;primaryColorDark&lowbar;blue&lt&semi;&sol;item&gt&semi;<br &sol;>&lt&semi;&sol;style&gt&semi;<br &sol;><br &sol;>&lt&semi;style name&equals;"AppTheme&period;Red" parent&equals;"Theme&period;AppCompat&period;Light&period;DarkActionBar"&gt&semi;<br &sol;> &lt&semi;item name&equals;"colorPrimary"&gt&semi;&commat;color&sol;primaryColor&lowbar;red&lt&semi;&sol;item&gt&semi;<br &sol;> &lt&semi;item name&equals;"colorPrimaryDark"&gt&semi;&commat;color&sol;primaryColorDark&lowbar;red&lt&semi;&sol;item&gt&semi;<br &sol;> &lt&semi;item name&equals;"colorAccent"&gt&semi;&commat;color&sol;primaryAccent&lowbar;red&lt&semi;&sol;item&gt&semi;<br &sol;> &lt&semi;item name&equals;"backgroundColor"&gt&semi;&commat;color&sol;primaryColorDark&lowbar;red&lt&semi;&sol;item&gt&semi;<br &sol;>&lt&semi;&sol;style&gt&semi;<br &sol;><br &sol;>&lt&semi;style name&equals;"AppTheme&period;Green" parent&equals;"Theme&period;AppCompat&period;Light"&gt&semi;<br &sol;> &lt&semi;item name&equals;"colorPrimary"&gt&semi;&commat;color&sol;primaryColor&lowbar;green&lt&semi;&sol;item&gt&semi;<br &sol;> &lt&semi;item name&equals;"colorPrimaryDark"&gt&semi;&commat;color&sol;primaryColorDark&lowbar;green&lt&semi;&sol;item&gt&semi;<br &sol;> &lt&semi;item name&equals;"colorAccent"&gt&semi;&commat;color&sol;primaryAccent&lowbar;green&lt&semi;&sol;item&gt&semi;<br &sol;> &lt&semi;item name&equals;"backgroundColor"&gt&semi;&commat;color&sol;primaryColorDark&lowbar;green&lt&semi;&sol;item&gt&semi;<br &sol;>&lt&semi;&sol;style&gt&semi;<&sol;pre> &NewLine;<p> &NewLine; &NewLine;<&sol;p> &NewLine;<p>4&period; The below layout file consist of three buttons &comma; <span style&equals;"color&colon; &num;008000&semi;"><strong>onClick<&sol;strong><&sol;span> of which we will apply different themes in our app&period;<&sol;p> &NewLine;<p> &NewLine; &NewLine;<&sol;p> &NewLine;<pre class&equals;"wp-block-preformatted">&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;<br &sol;>&lt&semi;LinearLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"<br &sol;> android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<br &sol;> android&colon;layout&lowbar;height&equals;"match&lowbar;parent"<br &sol;> <span style&equals;"color&colon; &num;0000ff&semi;"><strong>android&colon;background&equals;"&quest;attr&sol;backgroundColor"<&sol;strong><&sol;span><br &sol;> android&colon;orientation&equals;"vertical"&gt&semi;<br &sol;><br &sol;> &lt&semi;Button<br &sol;> android&colon;id&equals;"&commat;&plus;id&sol;blue&lowbar;theme&lowbar;btn"<br &sol;> android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<br &sol;> android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"<br &sol;> android&colon;text&equals;"BLUE" &sol;&gt&semi;<br &sol;><br &sol;> &lt&semi;Button<br &sol;> android&colon;id&equals;"&commat;&plus;id&sol;red&lowbar;theme&lowbar;btn"<br &sol;> android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<br &sol;> android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"<br &sol;> android&colon;text&equals;"RED" &sol;&gt&semi;<br &sol;><br &sol;> &lt&semi;Button<br &sol;> android&colon;id&equals;"&commat;&plus;id&sol;green&lowbar;theme&lowbar;btn"<br &sol;> android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<br &sol;> android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"<br &sol;> android&colon;text&equals;"GREEN" &sol;&gt&semi;<br &sol;><br &sol;>&lt&semi;&sol;LinearLayout&gt&semi;<&sol;pre> &NewLine;<p> &NewLine; &NewLine;<&sol;p> &NewLine;<p>4&period; Inside <span style&equals;"color&colon; &num;0000ff&semi;"><strong>MainActivity&period;java<&sol;strong><&sol;span>&comma; onClick of each button the application will set with different theme&period;<&sol;p> &NewLine;<p> &NewLine; &NewLine;<&sol;p> &NewLine;<pre class&equals;"wp-block-preformatted">package com&period;c1ctech&period;androidstylesandthemes&semi;<br &sol;><br &sol;>import android&period;content&period;Context&semi;<br &sol;>import android&period;os&period;Bundle&semi;<br &sol;>import android&period;view&period;View&semi;<br &sol;>import android&period;widget&period;Button&semi;<br &sol;><br &sol;>import androidx&period;appcompat&period;app&period;AppCompatActivity&semi;<br &sol;><br &sol;>public class MainActivity extends AppCompatActivity implements View&period;OnClickListener &lbrace;<br &sol;><br &sol;> Button btnBlueTheme&comma; btnRedTheme&comma; btnGreenTheme&semi;<br &sol;><br &sol;> &commat;Override<br &sol;> public void onCreate&lpar;Bundle savedInstanceState&rpar; &lbrace;<br &sol;> super&period;onCreate&lpar;savedInstanceState&rpar;&semi;<br &sol;><br &sol;> <strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;setTheme&lpar;&rpar; must call before setContentView&lpar;&rpar;<&sol;span><&sol;strong><br &sol;><strong> setTheme&lpar;getSharedPreferences&lpar;"AppData"&comma; Context&period;MODE&lowbar;PRIVATE&rpar;&period;getInt&lpar;"theme"&comma; R&period;style&period;AppTheme&lowbar;Blue&rpar;&rpar;&semi;<&sol;strong><br &sol;><br &sol;> setContentView&lpar;R&period;layout&period;activity&lowbar;main&rpar;&semi;<br &sol;><br &sol;> btnBlueTheme &equals; findViewById&lpar;R&period;id&period;blue&lowbar;theme&lowbar;btn&rpar;&semi;<br &sol;> btnRedTheme &equals; findViewById&lpar;R&period;id&period;red&lowbar;theme&lowbar;btn&rpar;&semi;<br &sol;> btnGreenTheme &equals; findViewById&lpar;R&period;id&period;green&lowbar;theme&lowbar;btn&rpar;&semi;<br &sol;> btnGreenTheme&period;setOnClickListener&lpar;this&rpar;&semi;<br &sol;> btnBlueTheme&period;setOnClickListener&lpar;this&rpar;&semi;<br &sol;> btnRedTheme&period;setOnClickListener&lpar;this&rpar;&semi;<br &sol;> &rcub;<br &sol;><br &sol;> &commat;Override<br &sol;> public void onClick&lpar;View v&rpar; &lbrace;<br &sol;> switch &lpar;v&period;getId&lpar;&rpar;&rpar; &lbrace;<br &sol;> case R&period;id&period;blue&lowbar;theme&lowbar;btn&colon;<br &sol;> changeTheme&lpar;R&period;style&period;AppTheme&lowbar;Blue&rpar;&semi;<br &sol;> break&semi;<br &sol;> case R&period;id&period;red&lowbar;theme&lowbar;btn&colon;<br &sol;> changeTheme&lpar;R&period;style&period;AppTheme&lowbar;Red&rpar;&semi;<br &sol;> break&semi;<br &sol;> case R&period;id&period;green&lowbar;theme&lowbar;btn&colon;<br &sol;> changeTheme&lpar;R&period;style&period;AppTheme&lowbar;Green&rpar;&semi;<br &sol;> break&semi;<br &sol;> &rcub;<br &sol;> &rcub;<br &sol;><br &sol;> private void changeTheme&lpar;int themeId&rpar; &lbrace;<br &sol;> getSharedPreferences&lpar;"AppData"&comma; Context&period;MODE&lowbar;PRIVATE&rpar;&period;edit&lpar;&rpar;&period;putInt&lpar;"theme"&comma; themeId&rpar;&period;apply&lpar;&rpar;&semi;<br &sol;> recreate&lpar;&rpar;&semi;<br &sol;> &rcub;<br &sol;>&rcub;<&sol;pre> &NewLine;<p> &NewLine; &NewLine;<&sol;p> &NewLine;<h5 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>OUTPUT&colon;<&sol;strong><&sol;span><&sol;h5> &NewLine;<p> &NewLine; &NewLine;<&sol;p> &NewLine;<figure class&equals;"wp-block-image"><img class&equals;"wp-image-2326" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2020&sol;12&sol;Screenshot&lowbar;1608730908&period;png" alt&equals;"" width&equals;"331" height&equals;"588" &sol;>    <img class&equals;"alignnone wp-image-2328" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2020&sol;12&sol;Screenshot&lowbar;1608730915&period;png" alt&equals;"" width&equals;"330" height&equals;"587" &sol;><&sol;figure> &NewLine;<p> &NewLine; &NewLine; &NewLine; &NewLine; &NewLine; &NewLine;<&sol;p> &NewLine;<p>&nbsp&semi;<&sol;p> &NewLine;<p><&sol;p>&NewLine;

Exit mobile version