By Mohamad Wael

Posted :

Android styles and themes a tutorial

A View has a set of attributes , such as its android:layout_width , or its android:layout_height . Some of these attributes are used to customize the appearance of a view , this is called styling the view , or applying a style to a given view .

This being said , a style can be defined inline , as in the following example :

  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <!-- activity_main.xml -->
  4.  
  5. <LinearLayout ... >
  6.  
  7. <Button
  8. android:background="@android:color/holo_red_light"
  9. android:textColor="@android:color/white"
  10. android:focusable="false"
  11. android:clickable="false"
  12. ...
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content"
  15. android:text="Button styled" />
  16.  
  17. </LinearLayout>
Button styled inline

Or if maybe it is to create a model , like a button model , which will be applied to multiple button views , it can be defined in a resource file , located in the res/values folder .

The res/values folder just contain some files , which contain the definition of some values , such as colors , or strings , or arrays of strings , or dimensions , or styles .

res values folder

Typically styles are defined in a file named style.xml , but the file containing style definitions , can be named anything .

The styling resource file , starts with the resources tag , and contain style tags , where the style tags contain item tags , used to define values for some styling attributes . The next example defines a text appearance style , and a button style :

  1. <!--res/values/styles.xml-->
  2.  
  3. <resources>
  4.  
  5. <style name="eccentric_appearance_text">
  6. <item name="android:textColor">@android:color/white</item>
  7. <item name="android:textColorHighlight">@android:color/black</item>
  8. <item name="android:textColorHint">@android:color/holo_blue_bright</item>
  9. <item name="android:textSize">22sp</item>
  10. <item name="android:textStyle">italic</item>
  11. </style>
  12.  
  13. <style name="eccentric_button_style" >
  14. <item name="android:background">@android:color/holo_red_light</item>
  15. <item name="android:focusable">false</item>
  16. <item name="android:clickable">false</item>
  17. <item name="android:gravity">center_vertical|center_horizontal</item>
  18. <item name="android:textAppearance">@style/eccentric_appearance_text</item>
  19. </style>
  20.  
  21. </resources>

To apply styling to the button , this can be done where the button is defined like so :

  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <!-- activity_main.xml -->
  4.  
  5. <LinearLayout ... >
  6.  
  7. <Button
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. style="@style/eccentric_button_style"
  11. android:text="Button styled" />
  12.  
  13. </LinearLayout>

What will happen , is that the view will parse the attributes specified using xml , and apply the specified styling .

Button styled

A theme has some standard attributes , which it must provide values for , they are defined in the sdk/platforms/android-{version-numer}/data/res/values/attrs.xml file . For example , for android-30 , an excerpt of the attrs.xml file , is the following :

  1. <!-- excerpt from sdk/platforms/android-30/data/res/values/attrs.xml -->
  2.  
  3. <declare-styleable name="Theme">
  4. <!-- ============== -->
  5. <!-- Generic styles -->
  6. <!-- ============== -->
  7.  
  8. <!-- Specifies that a theme has a light background with dark text on top. -->
  9. <attr name="isLightTheme" format="boolean" />
  10.  
  11. <!-- Default color of background imagery, ex. full-screen windows. -->
  12. <attr name="colorBackground" format="color" />
  13.  
  14. <!-- Color used for error states and things that need to be drawn to
  15. the users attention.. -->
  16. <attr name="colorError" format="reference|color" />
  17.  
  18. <!-- Default background dim amount when a menu, dialog, or something similar pops up. -->
  19. <attr name="backgroundDimAmount" format="float" />
  20.  
  21. <!-- =========== -->
  22. <!-- Text styles -->
  23. <!-- =========== -->
  24.  
  25. <!-- Default appearance of text: color, typeface, size, and style. -->
  26. <attr name="textAppearance" format="reference" />
  27.  
  28. <!-- The most prominent text color. -->
  29. <attr name="textColorPrimary" format="reference|color" />
  30. <!-- Secondary text color. -->
  31. <attr name="textColorSecondary" format="reference|color" />
  32. <!-- Tertiary text color. -->
  33. <attr name="textColorTertiary" format="reference|color" />
  34.  
  35. <!-- The underline thickness -->
  36. <attr name="textUnderlineThickness" format="reference|dimension" />
  37.  
  38. <!-- ============= -->
  39. <!-- Button styles -->
  40. <!-- ============= -->
  41.  
  42. <!-- Normal Button style. -->
  43. <attr name="buttonStyle" format="reference" />
  44.  
  45. <!-- Small Button style. -->
  46. <attr name="buttonStyleSmall" format="reference" />
  47.  
  48. <!-- Button style to inset into an EditText. -->
  49. <attr name="buttonStyleInset" format="reference" />
  50.  
  51. <!-- ToggleButton style. -->
  52. <attr name="buttonStyleToggle" format="reference" />
  53.  
  54. <!-- ============== -->
  55. <!-- Gallery styles -->
  56. <!-- ============== -->
  57.  
  58. <!-- The preferred background for gallery items. This should be set
  59. as the background of any Views you provide from the Adapter. -->
  60. <attr name="galleryItemBackground" format="reference" />
  61.  
  62. <!-- =========== -->
  63. <!-- List styles -->
  64. <!-- =========== -->
  65.  
  66. <!-- The preferred list item height. -->
  67. <attr name="listPreferredItemHeight" format="dimension" />
  68. <!-- A smaller, sleeker list item height. -->
  69. <attr name="listPreferredItemHeightSmall" format="dimension" />
  70.  
  71. <!-- The preferred TextAppearance for the primary text of list items. -->
  72. <attr name="textAppearanceListItem" format="reference" />
  73. <!-- The preferred TextAppearance for the secondary text of list items. -->
  74. <attr name="textAppearanceListItemSecondary" format="reference" />
  75.  
  76. <!-- ============= -->
  77. <!-- Window styles -->
  78. <!-- ============= -->
  79.  
  80. <attr name="windowBackground" format="reference|color" />
  81. <attr name="windowBackgroundFallback" format="reference|color" />
  82.  
  83. <!-- ============ -->
  84. <!-- Floating toolbar styles -->
  85. <!-- ============ -->
  86.  
  87. <attr name="floatingToolbarCloseDrawable" format="reference" />
  88. <attr name="floatingToolbarForegroundColor" format="reference|color" />
  89. <attr name="floatingToolbarItemBackgroundBorderlessDrawable" format="reference" />
  90.  
  91. <!-- ============ -->
  92. <!-- Alert Dialog styles -->
  93. <!-- ============ -->
  94.  
  95. <attr name="alertDialogStyle" format="reference" />
  96.  
  97. <!-- ============== -->
  98. <!-- Image elements -->
  99. <!-- ============== -->
  100.  
  101. <!-- Icon that should be used to indicate that an app is waiting for a fingerprint scan.
  102. This should be used whenever an app is requesting the user to place a finger on the
  103. fingerprint sensor. It can be combined with other drawables such as colored circles, so
  104. the appearance matches the branding of the app requesting the fingerprint scan.-->
  105. <attr name="fingerprintAuthDrawable" format="reference" />
  106.  
  107. <!-- ============ -->
  108. <!-- Panel styles -->
  109. <!-- ============ -->
  110.  
  111. <!-- The background of a panel when it is inset from the left and right edges of the screen. -->
  112. <attr name="panelBackground" format="reference|color" />
  113. <attr name="panelMenuIsCompact" format="boolean" />
  114. <attr name="panelMenuListWidth" format="dimension" />
  115. <attr name="panelMenuListTheme" format="reference" />
  116.  
  117. <!-- =================== -->
  118. <!-- Other widget styles -->
  119. <!-- =================== -->
  120.  
  121. <!-- Default Checkbox style. -->
  122. <attr name="checkboxStyle" format="reference" />
  123. <!-- Default EditText style. -->
  124. <attr name="editTextStyle" format="reference" />
  125. <!-- Default Gallery style. -->
  126. <attr name="galleryStyle" format="reference" />
  127. <!-- Default GridView style. -->
  128. <attr name="gridViewStyle" format="reference" />
  129. <!-- Default ListView style. -->
  130. <attr name="listViewStyle" format="reference" />
  131. <!-- Default RadioButton style. -->
  132. <attr name="radioButtonStyle" format="reference" />
  133. <!-- Default Spinner style. -->
  134. <attr name="spinnerStyle" format="reference" />
  135. <!-- NumberPicker style. -->
  136. <attr name="numberPickerStyle" format="reference" />
  137. <!-- The CalendarView style. -->
  138. <attr name="calendarViewStyle" format="reference" />
  139.  
  140. <!-- =================== -->
  141. <!-- Action bar styles -->
  142. <!-- =================== -->
  143.  
  144. <!-- Default style for tabs within an action bar. -->
  145. <attr name="actionBarTabStyle" format="reference" />
  146. <!-- Reference to a style for the Action Bar Tab Bar. -->
  147. <attr name="actionBarTabBarStyle" format="reference" />
  148. <!-- Reference to a style for the Action Bar Tab text. -->
  149. <attr name="actionBarTabTextStyle" format="reference" />
  150.  
  151. <!-- =================== -->
  152. <!-- Action mode styles -->
  153. <!-- =================== -->
  154.  
  155. <!-- Reference to a style for the Action Mode. -->
  156. <attr name="actionModeStyle" format="reference" />
  157. <!-- Reference to a style for the Action Mode close button. -->
  158. <attr name="actionModeCloseButtonStyle" format="reference" />
  159. <!-- Background drawable to use for action mode UI. -->
  160. <attr name="actionModeBackground" format="reference" />
  161.  
  162. <!-- =================== -->
  163. <!-- Preference styles -->
  164. <!-- =================== -->
  165.  
  166. <!-- Default style for PreferenceScreen. -->
  167. <attr name="preferenceScreenStyle" format="reference" />
  168. <!-- Default style for the PreferenceActivity. -->
  169. <attr name="preferenceActivityStyle" format="reference" />
  170. <!-- Default style for Headers pane in PreferenceActivity. -->
  171. <attr name="preferenceFragmentStyle" format="reference" />
  172. <!-- Default style for PreferenceCategory. -->
  173. <attr name="preferenceCategoryStyle" format="reference" />
  174. <!-- Default style for Preference. -->
  175. <attr name="preferenceStyle" format="reference" />
  176. <!-- Default style for informational Preference. -->
  177. <attr name="preferenceInformationStyle" format="reference" />
  178. <!-- Default style for CheckBoxPreference. -->
  179. <attr name="checkBoxPreferenceStyle" format="reference" />
  180.  
  181. <!-- ============================ -->
  182. <!-- Text selection handle styles -->
  183. <!-- ============================ -->
  184.  
  185. <!-- Reference to a drawable that will be used to display a text selection
  186. anchor on the left side of a selection region. -->
  187. <attr name="textSelectHandleLeft" format="reference" />
  188. <!-- Reference to a drawable that will be used to display a text selection
  189. anchor on the right side of a selection region. -->
  190. <attr name="textSelectHandleRight" format="reference" />
  191.  
  192.  
  193. <!-- Theme to use for dialogs spawned from this theme. -->
  194. <attr name="dialogTheme" format="reference" />
  195. <!-- Window decor layout to use in dialog mode with icons. -->
  196. <attr name="dialogTitleIconsDecorLayout" format="reference" />
  197.  
  198. <!-- Theme to use for alert dialogs spawned from this theme. -->
  199. <attr name="alertDialogTheme" format="reference" />
  200. <!-- Icon drawable to use for alerts. -->
  201. <attr name="alertDialogIcon" format="reference" />
  202.  
  203. <!-- Style for button bars. -->
  204. <attr name="buttonBarStyle" format="reference" />
  205.  
  206. <!-- Style for buttons within button bars. -->
  207. <attr name="buttonBarButtonStyle" format="reference" />
  208.  
  209. <!-- Background to use for toasts. -->
  210. <attr name="toastFrameBackground" format="reference" />
  211.  
  212. <!-- Background to use for tooltip popups. -->
  213. <attr name="tooltipFrameBackground" format="reference" />
  214.  
  215. <!-- Foreground color to use for tooltip popups. -->
  216. <attr name="tooltipForegroundColor" format="reference|color" />
  217.  
  218. <!-- ============== -->
  219. <!-- Pointer styles -->
  220. <!-- ============== -->
  221.  
  222. <!-- The drawable for accessibility focused views. -->
  223. <attr name="accessibilityFocusedDrawable" format="reference" />
  224.  
  225. <!-- ============= -->
  226. <!-- Color palette -->
  227. <!-- ============= -->
  228.  
  229. <!-- The primary branding color for the app. By default, this is the color applied to the
  230. action bar background. -->
  231. <attr name="colorPrimary" format="color" />
  232.  
  233. <!-- Dark variant of the primary branding color. By default, this is the color applied to
  234. the status bar (via statusBarColor) and navigation bar (via navigationBarColor). -->
  235. <attr name="colorPrimaryDark" format="color" />
  236.  
  237. <!-- The secondary branding color for the app. -->
  238. <attr name="colorSecondary" format="color" />
  239.  
  240. <!-- Bright complement to the primary branding color. By default, this is the color applied
  241. to framework controls (via colorControlActivated). -->
  242. <attr name="colorAccent" format="color" />
  243.  
  244. <!-- The color applied to framework controls in their normal state. -->
  245. <attr name="colorControlNormal" format="color" />
  246.  
  247. <!-- The color applied to framework controls in their activated (ex. checked) state. -->
  248. <attr name="colorControlActivated" format="color" />
  249.  
  250. <!-- The color applied to framework control highlights (ex. ripples, list selectors). -->
  251. <attr name="colorControlHighlight" format="color" />
  252.  
  253. <!-- The color applied to framework buttons in their normal state. -->
  254. <attr name="colorButtonNormal" format="color" />
  255.  
  256. <!-- The color applied to framework switch thumbs in their normal state. -->
  257. <attr name="colorSwitchThumbNormal" format="color" />
  258.  
  259. <!-- The color applied to framework progress and seek bar backgrounds in their normal state. -->
  260. <attr name="colorProgressBackgroundNormal" format="color" />
  261.  
  262. <!-- The color applied to the edge effect on scrolling containers. -->
  263. <attr name="colorEdgeEffect" format="color" />
  264.  
  265. <!-- =================== -->
  266. <!-- Lighting properties -->
  267. <!-- =================== -->
  268.  
  269. <!-- @hide The default Y position of the light used to project view shadows. -->
  270. <attr name="lightY" format="dimension" />
  271.  
  272. <!-- @hide The default Z position of the light used to project view shadows. -->
  273. <attr name="lightZ" format="dimension" />
  274.  
  275. <!-- Alpha value of the ambient shadow projected by elevated views, between 0 and 1. -->
  276. <attr name="ambientShadowAlpha" format="float" />
  277.  
  278. <!-- Alpha value of the spot shadow projected by elevated views, between 0 and 1. -->
  279. <attr name="spotShadowAlpha" format="float" />
  280.  
  281. </declare-styleable>

As it can be seen from the attrs.xml file , some attributes are set to have a value , which has a reference format , meaning it must refer to another resource , other attributes are set to must have a literal value , such as a boolean , or a dimension .

An example of a theme , is the holo theme ,which was the default system theme , for application targeting android api , from version 11 inclusive , till version 13 inclusive , and from which an exert is provided :

  1. <!-- excerpt from sdk/platforms/android-30/data/res/values/themes_holo.xml -->
  2.  
  3. <?xml version="1.0" encoding="utf-8"?>
  4.  
  5. <resources>
  6. <style name="Theme.Holo">
  7. <item name="colorBackground">@color/background_holo_dark</item>
  8. <item name="colorError">@color/error_color_material_dark</item>
  9. <item name="backgroundDimAmount">0.6</item>
  10.  
  11. <!-- Text styles -->
  12. <item name="textAppearance">@style/TextAppearance.Holo</item>
  13. <item name="textAppearanceInverse">@style/TextAppearance.Holo.Inverse</item>
  14.  
  15. <item name="textColorPrimary">@color/primary_text_holo_dark</item>
  16. <item name="textColorSecondary">@color/secondary_text_holo_dark</item>
  17. <item name="textColorTertiary">@color/tertiary_text_holo_dark</item>
  18.  
  19. <!-- Button styles -->
  20. <item name="buttonStyle">@style/Widget.Holo.Button</item>
  21.  
  22. <item name="buttonStyleSmall">@style/Widget.Holo.Button.Small</item>
  23. <item name="buttonStyleInset">@style/Widget.Holo.Button.Inset</item>
  24.  
  25. <item name="buttonStyleToggle">@style/Widget.Holo.Button.Toggle</item>
  26.  
  27. <!-- Gallery attributes -->
  28. <item name="galleryItemBackground">@drawable/gallery_item_background</item>
  29.  
  30. <!-- List attributes -->
  31. <item name="listPreferredItemHeight">64dip</item>
  32. <item name="listPreferredItemHeightSmall">48dip</item>
  33. <item name="textAppearanceListItem">?attr/textAppearanceLarge</item>
  34. <item name="textAppearanceListItemSecondary">?attr/textAppearanceSmall</item>
  35.  
  36. <!-- Window attributes -->
  37. <item name="windowNoTitle">false</item>
  38. <item name="windowTitleSize">25dip</item>
  39.  
  40. <!-- Dialog attributes -->
  41. <item name="dialogTheme">@style/Theme.Holo.Dialog</item>
  42. <item name="dialogCornerRadius">0dp</item>
  43.  
  44. <!-- AlertDialog attributes -->
  45. <item name="alertDialogStyle">@style/AlertDialog.Holo</item>
  46.  
  47. <!-- Presentation attributes -->
  48. <item name="presentationTheme">@style/Theme.Holo.Dialog.Presentation</item>
  49.  
  50. <!-- Toast attributes -->
  51. <item name="toastFrameBackground">@drawable/toast_frame</item>
  52.  
  53. <!-- Panel attributes -->
  54. <item name="panelBackground">@drawable/menu_hardkey_panel_holo_dark</item>
  55. <item name="panelMenuIsCompact">true</item>
  56. <item name="panelMenuListWidth">250dip</item>
  57. <item name="panelMenuListTheme">@style/Theme.Holo.CompactMenu</item>
  58.  
  59. <!-- Widget styles -->
  60. <item name="checkboxStyle">@style/Widget.Holo.CompoundButton.CheckBox</item>
  61. <item name="editTextStyle">@style/Widget.Holo.EditText</item>
  62. <item name="galleryStyle">@style/Widget.Holo.Gallery</item>
  63. <item name="gridViewStyle">@style/Widget.Holo.GridView</item>
  64. <item name="listViewStyle">@style/Widget.Holo.ListView</item>
  65. <item name="listViewWhiteStyle">@style/Widget.Holo.ListView.White</item>
  66. <item name="radioButtonStyle">@style/Widget.Holo.CompoundButton.RadioButton</item>
  67. <item name="spinnerStyle">?attr/dropDownSpinnerStyle</item>
  68.  
  69. <!-- Action bar styles -->
  70. <item name="actionBarTabStyle">@style/Widget.Holo.ActionBar.TabView</item>
  71. <item name="actionBarTabBarStyle">@style/Widget.Holo.ActionBar.TabBar</item>
  72. <item name="actionBarTabTextStyle">@style/Widget.Holo.ActionBar.TabText</item>
  73.  
  74. <!-- Preference styles -->
  75. <item name="preferenceScreenStyle">@style/Preference.Holo.PreferenceScreen</item>
  76. <item name="preferenceActivityStyle">@style/PreferenceActivity</item>
  77. <item name="preferenceFragmentStyle">@style/PreferenceFragment.Holo</item>
  78. <item name="preferenceCategoryStyle">@style/Preference.Holo.Category</item>
  79. <item name="preferenceStyle">@style/Preference.Holo</item>
  80. <item name="preferenceInformationStyle">@style/Preference.Holo.Information</item>
  81. <item name="checkBoxPreferenceStyle">@style/Preference.Holo.CheckBoxPreference</item>
  82.  
  83. <!-- Text selection handle attributes -->
  84. <item name="textSelectHandleLeft">@drawable/text_select_handle_left_material</item>
  85. <item name="textSelectHandleRight">@drawable/text_select_handle_right_material</item>
  86.  
  87. <!-- Scrollbar attributes -->
  88. <item name="scrollbarFadeDuration">250</item>
  89. <item name="scrollbarSize">10dip</item>
  90. <item name="scrollbarThumbHorizontal">@drawable/scrollbar_handle_holo_dark</item>
  91. <item name="scrollbarThumbVertical">@drawable/scrollbar_handle_holo_dark</item>
  92.  
  93. <!-- Search widget styles -->
  94. <item name="searchWidgetCorpusItemBackground">@color/search_widget_corpus_item_background</item>
  95.  
  96. <!-- SearchView attributes -->
  97. <item name="searchViewStyle">@style/Widget.Holo.SearchView</item>
  98. <item name="searchDialogTheme">@style/Theme.Holo.SearchBar</item>
  99.  
  100. <!-- PreferenceFrameLayout attributes -->
  101. <item name="preferenceFrameLayoutStyle">@style/Widget.Holo.PreferenceFrameLayout</item>
  102.  
  103. <!-- NumberPicker style-->
  104. <item name="numberPickerStyle">@style/Widget.Holo.NumberPicker</item>
  105.  
  106. <!-- CalendarView style-->
  107. <item name="calendarViewStyle">@style/Widget.Holo.CalendarView</item>
  108.  
  109. <!-- TimePicker style -->
  110. <item name="timePickerStyle">@style/Widget.Holo.TimePicker</item>
  111.  
  112. <!-- TimePicker dialog theme -->
  113. <item name="timePickerDialogTheme">?attr/alertDialogTheme</item>
  114.  
  115. <!-- DatePicker style -->
  116. <item name="datePickerStyle">@style/Widget.Holo.DatePicker</item>
  117.  
  118. <!-- DatePicker dialog theme -->
  119. <item name="datePickerDialogTheme">?attr/alertDialogTheme</item>
  120.  
  121. <item name="colorPrimaryDark">@color/holo_primary_dark</item>
  122. <item name="colorPrimary">@color/holo_primary</item>
  123. <item name="colorAccent">@color/holo_blue_dark</item>
  124. <item name="colorEdgeEffect">?attr/colorPrimary</item>
  125.  
  126. <item name="colorControlNormal">@color/holo_control_normal</item>
  127. <item name="colorControlActivated">@color/holo_control_activated</item>
  128.  
  129. <item name="colorControlHighlight">@color/holo_button_pressed</item>
  130. <item name="colorButtonNormal">@color/holo_button_normal</item>
  131. <item name="colorSwitchThumbNormal">@color/switch_thumb_material_light</item>
  132.  
  133. <!-- Holo-only color attributes -->
  134. <item name="colorPressedHighlight">@color/holo_gray_light</item>
  135. <item name="colorLongPressedHighlight">@color/holo_gray_bright</item>
  136. <item name="colorFocusedHighlight">@color/holo_blue_dark</item>
  137. <item name="colorMultiSelectHighlight">@color/holo_green_light</item>
  138. <item name="colorActivatedHighlight">@color/holo_blue_dark</item>
  139.  
  140. </style>
  141. </resources>

So as it can be seen from the provided excerpt , a theme is also defined using the style tag , in a ressource file inside the res/values folder . For the holo theme , this file is named themes_holo.xml .

In a sense , the attributes which a theme defines , have a higher degree of abstraction . So for example , for button styling , the buttonStyleSmall attribute was specified in the attrs.xml file , as to be defined , and is defined in themes_holo.xml , as having the value of @style/Widget.Holo.Button.Small . @style/Widget.Holo.Button.Small is defined in sdk/platforms/android-30/data/res/values/res/values/styles_holo.xml as :

  1. <style name="Widget.Holo.Button.Small">
  2. <item name="background">@drawable/btn_default_holo_dark</item>
  3. <item name="textAppearance">?attr/textAppearanceSmall</item>
  4. <item name="textColor">@color/primary_text_holo_dark</item>
  5. <item name="minHeight">48dip</item>
  6. <item name="minWidth">48dip</item>
  7. </style>

So in other words , different themes can define different values for the buttonStyleSmall attribute , and as an application developper , if you want to model by using themes , you can specify a style attribute for a View , for example the button , by using style="?android:attr/buttonStyleSmall" , where ?android:attr/buttonStyleSmall means , get the value of the android defined attribute , buttonStyleSmall , which is set , in the current set theme .

So whatever theme you set for your application , given the fact that you can also change a theme programmatically , or you can not specify a theme , to default to the device default theme , what is being stated is that in all cases , the button must have a styling of button small , so in other words , switching a theme , can switch the style while keeping the intended meaning , and going with the designer conceptions .

Also note that for example , the buttonStyle , which is defined in a theme , as <item name="buttonStyle">@style/Widget.Holo.Button</item> , will be applied by default to all buttons , to which the theme is set for , for example in the activity , or in a viewgroup .

A fallback theme , can be specified for all the activities in an application , by using the AndroidManifest.xml file . A theme can be specified for an activity , for a ViewGroup such as LinearLayout , for a specific View such as a Button .

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!-- AndroidManifest.xml -->
  3.  
  4. <manifest ...>
  5.  
  6. <application ...
  7. android:theme="@style/AppTheme">
  8.  
  9. <activity
  10. android:name=".MainActivity"
  11. android:theme="@style/Theme.AppCompat.Light.NoActionBar" >
  12. ...
  13. </activity>
  14. </application>
  15.  
  16. </manifest>
  17.  
  18. <?xml version="1.0" encoding="utf-8"?>
  19. <!-- activity_main.xml -->
  20.  
  21. <LinearLayout ...
  22. android:theme="@android:style/Theme.DeviceDefault" >
  23.  
  24. <Button
  25. ...
  26. style="?android:attr/buttonStyleSmall" />
  27.  
  28. <Button
  29. ...
  30. style="@android:style/Widget.Holo.Button.Small" />
  31.  
  32. </LinearLayout>

Applying the theme at a higher level , will make it be inherited at a lower level . So a theme applied to an activity , makes it appliable to its root ViewGroup , if its root ViewGroup , does not specify a theme . The views children of the ViewGroup will also inherit its theme , if they do not specify their own .

In other words , if no theme is specified , higher levels are checked for a theme , if an activity does not specify a theme , the application fallback theme is used , if the application does not specify a theme , then the default theme is used .

The default theme is selected based on the default target sdk version :

If the target sdk is lower than 11 , which is HONEYCOMB , then the default theme is com.android.internal.R.style.Theme , which is @android:style/Theme .

If the target sdk is larger or equal to 11 , and less than 14 , which is ICE_CREAM_SANDWICH , then the default theme will be com.android.internal.R.style.Theme_Holo , which is @android:style/Theme.Holo .

If the target sdk is larger or equal to 14 , and less than 24 , which is android Nougat , then the default theme will be com.android.internal.R.style.Theme_DeviceDefault , which is @android/style/Theme.DeviceDefault . For android platform 30 , this theme has a parent of Theme.DeviceDefaultBase , which has a parent of Theme.Material . Theme.DeviceDefault can be customized by device manufacturor , for example , on older google nexus devices , Theme.DeviceDefault was actually an alias to the Holo theme .

If the target sdk is larger or equal to android version 24 , the default theme is com.android.internal.R.style.Theme_DeviceDefault_Light_DarkActionBar , which is @android:style/Theme.DeviceDefault.Light.DarkActionBar .

If multiple themes are assigned down the hierarchy , for example if one theme is assigned for the activity , a second one is assigned to the activity root ViewGroup , and a third one , on a View in that ViewGroup , then the most local one applies . Attributes which are defined in a higher level of the hierarchy , but which do not exist in the lower level , are also applied .

So in other words , it is like composing themes together , and this brings us to what is called overlay themes , which might be encountered as in android:theme="@android:style/ThemeOverlay.Material.Light" . Overlay themes , define a limited set of attributes , as to be composed with other themes .

  1. <!-- sdk/platforms/android-30/data/res/values -->
  2.  
  3. <!-- Theme overlay that replaces colors with their light versions but preserves
  4. the value of colorAccent, colorPrimary and its variants. -->
  5.  
  6. <style name="ThemeOverlay.Material.Light">
  7. <item name="colorForeground">@color/foreground_material_light</item>
  8. <item name="colorForegroundInverse">@color/foreground_material_dark</item>
  9. <item name="colorBackground">@color/background_material_light</item>
  10. <item name="colorBackgroundFloating">@color/background_floating_material_light</item>
  11. <item name="colorBackgroundCacheHint">@color/background_cache_hint_selector_material_light</item>
  12. <item name="colorError">@color/error_color_material_light</item>
  13.  
  14. <item name="textColorPrimary">@color/text_color_primary</item>
  15. <item name="textColorPrimaryInverse">@color/primary_text_material_dark</item>
  16. <item name="textColorSecondary">@color/text_color_secondary</item>
  17. <item name="textColorSecondaryInverse">@color/secondary_text_material_dark</item>
  18. <item name="textColorTertiary">@color/secondary_text_material_light</item>
  19. <item name="textColorTertiaryInverse">@color/secondary_text_material_dark</item>
  20. <item name="textColorPrimaryDisableOnly">@color/primary_text_disable_only_material_light</item>
  21. <item name="textColorPrimaryInverseDisableOnly">@color/primary_text_disable_only_material_dark</item>
  22. <item name="textColorHint">@color/hint_foreground_material_light</item>
  23. <item name="textColorHintInverse">@color/hint_foreground_material_dark</item>
  24. <item name="textColorHighlight">@color/highlighted_text_material</item>
  25. <item name="textColorHighlightInverse">@color/highlighted_text_material</item>
  26. <item name="textColorSearchUrl">@color/search_url_text_material_light</item>
  27. <item name="textColorAlertDialogListItem">@color/primary_text_material_light</item>
  28.  
  29. <item name="textCheckMark">@drawable/indicator_check_mark_light</item>
  30. <item name="textCheckMarkInverse">@drawable/indicator_check_mark_dark</item>
  31.  
  32. <item name="colorControlNormal">?attr/textColorSecondary</item>
  33. <item name="colorControlHighlight">@color/ripple_material_light</item>
  34. <item name="colorButtonNormal">@color/btn_default_material_light</item>
  35. <item name="colorSwitchThumbNormal">@color/switch_thumb_material_light</item>
  36. <item name="colorProgressBackgroundNormal">?attr/colorControlNormal</item>
  37. </style>

The AppCompat support library , which provides backwards compatibility for older android versions , has some themes which are defined , and which start by Theme.AppCompat , or by ThemeOverlay.AppCompat , and which can be used as in android:theme="@style/Theme.AppCompat" or as in style="?attr/buttonStyleSmall" so without prefixing with android: , as in to dereference an attribute which belongs or has been defined by the application . If an activity extends an AppCompatActivity , it must use an AppCompat theme .

Generally in the android platform , or in AppCompat , the themes are either dark themes which use a dark bacground color , with a light foreground such as one used for text , or light themes , and they do , or do not have , an action bar . Light themes can also have a dark action bar .

There is also what is called a DayNight theme , like Theme.AppCompat.DayNight or Theme.DeviceDefault.DayNight . A DayNight theme switches between a dark and a light theme , as instructed to do by using the setDefaultNightMode method , of AppCompatDelegate , as follows .

  1. <!-- app/src/main/AndroidManifest.xml -->
  2.  
  3. <?xml version="1.0" encoding="utf-8"?>
  4.  
  5. <manifest ...>
  6.  
  7. <application
  8. android:name=".TheApp"
  9. android:theme="@style/Theme.AppCompat.DayNight">
  10. <activity ...
  11. </application>
  12.  
  13. </manifest>
  1. /* src/main/java/com/twiserandom/mobileapps/demo/theme_and_style/TheApp.java */
  2.  
  3. package com .twiserandom .mobileapps .demo .theme_and_style;
  4.  
  5. import android .app .Application;
  6. import androidx .appcompat .app .AppCompatDelegate;
  7.  
  8. public class
  9. TheApp
  10. extends Application {
  11.  
  12. public void
  13. onCreate ( ){
  14. super .onCreate ( );
  15. AppCompatDelegate .setDefaultNightMode (AppCompatDelegate .MODE_NIGHT_AUTO );
  16. /*
  17. MODE_NIGHT_AUTO_BATTERY :
  18. When battery saver is on , use the dark theme ,
  19. otherwise use the light theme .
  20. MODE_NIGHT_YES :
  21. use the dark theme .
  22. MODE_NIGHT_NO :
  23. use the light theme .
  24. MODE_NIGHT_AUTO , MODE_NIGHT_AUTO_TIME :
  25. use the dark theme , when determined by using
  26. the location or if not possible hardcoded time ,
  27. that it is night . This is deprecated .
  28. MODE_NIGHT_FOLLOW_SYSTEM :
  29. use the system night mode setting ,
  30. to determine if the dark theme is to be used . */ }}

A DayNight theme works by having values for the night category , defined in values-night , so for example Theme.AppCompat.DayNight has values defined in both appcompat-version/res/values/values.xml as <style name="Theme.AppCompat.DayNight" parent="Theme.AppCompat.Light"/> , and appcompat-version/res/values-night-v8/values-night-v8.xml as <style name="Theme.AppCompat.DayNight" parent="Theme.AppCompat"/> , values-night-v8 means this applies starting api version 8 only .

Starting android version 10 , nicknamed Q , which is api version 29 , it is possible to set the phone theme to a dark theme , by using system settings , hence applications that uses the day and night themes , will automatically switch to the dark theme , unless they have this overriden , as shown earlier .

When an android application is created , it has a default fallback theme , declared in the AndroidManifest.xml .

  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <!-- app/src/main/AndroidManifest.xml -->
  4.  
  5. <manifest ... >
  6.  
  7. <application ...
  8. android:theme="@style/AppTheme">
  9. ...
  10. </application>
  11.  
  12. </manifest>

AppTheme is defined in the application res/values/styles.xml , and has the following content :

  1. <!-- app/src/main/res/values/styles.xml -->
  2.  
  3. <resources>
  4.  
  5. <!-- Base application theme. -->
  6.  
  7. <style
  8. name="AppTheme"
  9. parent="Theme.AppCompat.Light.DarkActionBar">
  10.  
  11. <!-- Customize your theme here. -->
  12.  
  13. <item name="colorPrimary">@color/colorPrimary</item>
  14. <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
  15. <item name="colorAccent">@color/colorAccent</item>
  16. </style>
  17.  
  18. </resources>

So when defining a theme , or a style , a parent can be specified , as seen from the previous example , as not to start from scratch . For a style , it can be done like so :

  1. <!-- app/src/main/res/values/styles.xml -->
  2. <resources>
  3. <style
  4. name="eccentric_appearance_text"
  5. parent="@android:style/TextAppearance.Inverse">
  6. <!--
  7. To inherit from within the android platform
  8. sdk/platforms/android-version/data/res/values/styles.xml ,
  9. the @android:style/What_To_inherit format can
  10. be used .
  11. For example AppTheme can inherit the
  12. Theme.DeviceDefault.Light by using
  13. parent="@android:style/Theme.DeviceDefault.Light"
  14. -->
  15. <item name="android:textColor">@android:color/white</item>
  16. <item name="android:textSize">22sp</item>
  17. <item name="android:textStyle">italic</item>
  18. </style>
  19.  
  20. <style
  21. name="eccentric_appearance_text.eccentric_button_style"
  22. parent="@style/eccentric_appearance_text" >
  23. <!--
  24. To inherit from within the
  25. application , or within a library in the
  26. application such as AppCompat , the @android:
  27. prefix can be dropped , you can even drop
  28. the @style/ if you want .
  29. So for example eccentric_button_style , can define
  30. a parent of parent="TextAppearance.AppCompat" ,
  31. to specify that its parent style belong to
  32. the AppCompat library .
  33. Also inheritance within the application , not within
  34. libraries that application is using , can be done
  35. using a dot notation in the name , of the like ,
  36. name="eccentric_appearance_text.eccentric_button_style" ,
  37. to specify that eccentric_button_style has a
  38. parent of eccentric_appearance_text .
  39. In such a case it is not necessary to use the
  40. parent attribute , but if the parent attribute
  41. is used , then the styling inherited using
  42. the parent attribute has higher precedence
  43. over styling inherited using the dot notation .
  44. When specifying the style , the complete
  45. dotted name must be used as in
  46. style="@style/eccentric_appearance_text.eccentric_button_style" -->
  47. <item name="android:background">@android:color/holo_red_light</item>
  48. <item name="android:textColor">@android:color/white</item>
  49. <item name="android:focusable">false</item>
  50. <item name="android:clickable">false</item>
  51. <item name="android:gravity">center_vertical|center_horizontal</item>
  52. </style>
  53.  
  54. </resources>
  55.  

When applying android:theme , and style to a view , the style attributes will have a higher priority than the theme attributes , and inline attributes will have a higher priority than both the theme , and the style attributes , and styling attributes which are set programmatically , will have the higher priority .

  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <!-- res/layout/activity_main.xml -->
  4.  
  5. <LinearLayout ... >
  6.  
  7. <Button
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:id="@+id/btn"
  11. style="@style/eccentric_appearance_text.eccentric_button_style"
  12. android:theme="@android:style/Theme.Holo"
  13. android:textColor="@android:color/holo_green_dark"
  14. android:text="Button styled" />
  15. <!--
  16. Attributes defined in
  17. style="@style/eccentric_appearance_text.eccentric_button_style"
  18. have a higher priority than attributes
  19. defined in android:theme="@android:style/Theme.Holo" ,
  20. but they have a lesser priority than inline
  21. defined attributes of the like of :
  22. android:textColor="@android:color/holo_green_dark" ,
  23. which have a lower priority of styling attributes
  24. set programmatically , as in ,
  25. Button btn = (Button) findViewById(R.id.btn);
  26. btn.setTextColor(Color.BLUE); -->
  27. </LinearLayout>

In newer version of androd studio , the default fallback theme , declared in AndroidManifest.xml , and which is used to customize an application theming , had a name change , and is now :

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!-- AndroidManifest.xml -->
  3. <manifest ... >
  4. <application
  5. android:theme="@style/Theme.ThemeAndStyle">
  6. <!--
  7. The application name is actually
  8. Theme And Style , so ThemeAndStyle
  9. is the application name with spaces
  10. being removed . -->
  11. <activity ...
  12. </application>
  13. </manifest>

Theme.[ApplicationName] is defined in res/values/themes.xml and res/values-night/themes.xml . So one is the light theme , and the second one is the dark theme .

  1. <!-- res/values/themes.xml -->
  2. <resources xmlns:tools="http://schemas.android.com/tools">
  3. <!-- Base application theme. -->
  4. <style name="Theme.ThemeAndStyle" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
  5. <!-- Primary brand color. -->
  6. <item name="colorPrimary">@color/purple_500</item>
  7. <item name="colorPrimaryVariant">@color/purple_700</item>
  8. <item name="colorOnPrimary">@color/white</item>
  9. <!-- Secondary brand color. -->
  10. <item name="colorSecondary">@color/teal_200</item>
  11. <item name="colorSecondaryVariant">@color/teal_700</item>
  12. <item name="colorOnSecondary">@color/black</item>
  13. <!-- Status bar color. -->
  14. <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
  15. <!-- Customize your theme here. -->
  16. </style>
  17. </resources>
  18.  
  19. <!-- res/values-night/themes.xml -->
  20. <resources xmlns:tools="http://schemas.android.com/tools">
  21. <!-- Base application theme. -->
  22. <style name="Theme.ThemeAndStyle" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
  23. <!-- Primary brand color. -->
  24. <item name="colorPrimary">@color/purple_200</item>
  25. <item name="colorPrimaryVariant">@color/purple_700</item>
  26. <item name="colorOnPrimary">@color/black</item>
  27. <!-- Secondary brand color. -->
  28. <item name="colorSecondary">@color/teal_200</item>
  29. <item name="colorSecondaryVariant">@color/teal_200</item>
  30. <item name="colorOnSecondary">@color/black</item>
  31. <!-- Status bar color. -->
  32. <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
  33. <!-- Customize your theme here. -->
  34. </style>
  35. </resources>

As seen from the previous excerpt , Theme.[ApplicationName] has a parent of Theme.MaterialComponents.DayNight.DarkActionBar which is a new theme by google , defined in a new library called material components , and which can be gotten by adding to the build.gradle dependencies , the following content :

  1. dependencies {
  2. ...
  3. implementation 'com.google.android.material:material:<version>'
  4. // for example , implementation 'com.google.android.material:material:1.1.0'
  5. ...
  6. }

The material components library defines , a dark theme Theme.MaterialComponents , and a light theme Theme.MaterialComponents.Light , and a day and night theme Theme.MaterialComponents.DayNight , which all can or cannot have an action bar , as in Theme.MaterialComponents.DayNight.NoActionBar . Additionally , the day and night theme , and the light theme , can have a dark action bar , as in Theme.MaterialComponents.Light.DarkActionBar .

The material components library , also defines what is called a bridge theme , this is like , if you were using in an older application , an AppCompat theme, and you want to switch the theme to the new material component theme , but some constraints forbid you of doing so , then you can use a bridge theme as in Theme.MaterialComponents.Bridge . Bridge themes inherit from AppCompat themes , and newer theming must be explicitly enabled , as not to cause attributes errors . For bridge themes , there is a dark and a light version , both with or without an action bar . The light version can also have a dark action bar .

  1. <!-- res/values/themes.xml -->
  2. <resources ...>
  3. <style
  4. name="Theme.ThemeAndStyle"
  5. parent="Theme.MaterialComponents.Bridge">
  6. <item
  7. name="materialButtonStyle">
  8. @style/Widget.MaterialComponents.Button
  9. </item>
  10. <!-- explicitly enabling newer theming .-->
  11. </style>
  12. </resources>
  13.  

A theme as seen earlier , has a color palette , each application can customize it , as its branding sees fit . To understand how color applies to a theme , google has provided a color tool .

Google color tool material palette

In comparison with the older theming color attributes specified in AppCompat , or the android api framework used version , the material component library colorPrimary maps to colorPrimary , and colorPrimaryVariant maps to colorPrimaryDark , and colorSecondary maps to colorAccent , and the material component library defines additional coloring attributes .

To customize a theme , it can be done by using xml . In android studio 3.2 , and earlier versions , there was the theme editor tool , but it was removed , starting android 3.3 .

So customizing a theme , is like defining your designer vision , so it is just about defining the values for some models , or attributes , or just creating some totally new attributes . So if the colors are downloaded from the material color tool , and placed in res/values/colors.xml they will have the following definition :

  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <!-- res/values/colors.xml -->
  4.  
  5. <resources>
  6. ...
  7. <color name="primaryColor">#64b5f6</color>
  8. <color name="primaryLightColor">#9be7ff</color>
  9. <color name="primaryDarkColor">#2286c3</color>
  10. <color name="secondaryColor">#b71c1c</color>
  11. <color name="secondaryLightColor">#f05545</color>
  12. <color name="secondaryDarkColor">#7f0000</color>
  13. <color name="primaryTextColor">#000000</color>
  14. <color name="secondaryTextColor">#ffffff</color>
  15. </resources>

And to apply them in the day theme , and to customize it , a little bit further more , this can be done like so :

  1. <!-- /res/values/themes.xml -->
  2. <resources xmlns:tools="http://schemas.android.com/tools">
  3.  
  4. <!-- define some custom attributes -->
  5. <attr name="MyHeadline6" format="reference"/>
  6. <attr name="MyHeadline7" format="reference"/>
  7. <!-- Define some styles -->
  8. <style
  9. name="MyHeadline6"
  10. parent="TextAppearance.MaterialComponents.Headline6">
  11. <item name="textAllCaps">true</item>
  12. </style>
  13.  
  14. <style
  15. name="MyHeadline7" >
  16. <item name="fontFamily">sans-serif-thin</item>
  17. <item name="android:fontFamily">sans-serif-thin</item>
  18. <item name="android:textStyle">normal</item>
  19. <item name="android:textAllCaps">false</item>
  20. <item name="android:textSize">14sp</item>
  21. <item name="android:letterSpacing">0.009375</item>
  22. </style>
  23.  
  24. <style
  25. name="shapeAppearanceSmallComponent"
  26. parent="ShapeAppearance.MaterialComponents.SmallComponent">
  27. <item name="cornerFamily">cut</item>
  28. <item name="cornerSize">24dp</item>
  29. </style>
  30.  
  31. <style
  32. name="materialButtonStyle"
  33. parent="Widget.MaterialComponents.Button">
  34. <item name="android:textColor">@android:color/holo_orange_dark</item>
  35. </style>
  36. <!-- Define a custom theme -->
  37. <style
  38. name="Theme.ThemeAndStyle"
  39. parent="Theme.MaterialComponents.DayNight.DarkActionBar">
  40.  
  41. <!-- Primary brand color. -->
  42. <item name="colorPrimary">@color/primaryColor</item>
  43. <item name="colorPrimaryVariant">@color/primaryDarkColor</item>
  44. <item name="colorOnPrimary">@color/primaryTextColor</item>
  45.  
  46. <!-- Secondary brand color. -->
  47. <item name="colorSecondary">@color/secondaryColor</item>
  48. <item name="colorSecondaryVariant">@color/secondaryDarkColor</item>
  49. <item name="colorOnSecondary">@color/secondaryTextColor</item>
  50.  
  51. <!-- Status bar color. -->
  52. <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
  53.  
  54. <!-- custom modeling -->
  55. <item name="MyHeadline7">@style/MyHeadline7</item>
  56. <item name="MyHeadline6">@style/MyHeadline6</item>
  57.  
  58. <!-- Overriding material button modeling -->
  59. <item name="materialButtonStyle">@style/materialButtonStyle</item>
  60. </style>
  61. </resources>

And if the res/layout/activity_main.xml has the following content :

  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <!-- res/layout/activity_main.xml -->
  4.  
  5. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  6. xmlns:app="http://schemas.android.com/apk/res-auto"
  7. xmlns:tools="http://schemas.android.com/tools"
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. android:orientation="vertical"
  11. tools:context=".MainActivity">
  12.  
  13. <com.google.android.material.button.MaterialButton
  14. android:layout_width="match_parent"
  15. android:layout_height="wrap_content"
  16. android:textAppearance="?attr/textAppearanceHeadline6"
  17. app:shapeAppearance="?attr/shapeAppearanceSmallComponent"
  18. android:text="hello world" />
  19.  
  20. <com.google.android.material.button.MaterialButton
  21. android:layout_width="match_parent"
  22. android:layout_height="wrap_content"
  23. android:textAppearance="?MyHeadline6"
  24. app:shapeAppearance="@style/shapeAppearanceSmallComponent"
  25. android:text="hello world" />
  26.  
  27. <com.google.android.material.button.MaterialButton
  28. android:layout_width="match_parent"
  29. android:layout_height="wrap_content"
  30. android:textAppearance="?MyHeadline7"
  31. android:text="hello world" />
  32.  
  33. <SeekBar
  34. android:id="@+id/seekBar"
  35. android:layout_width="match_parent"
  36. android:layout_height="wrap_content" />
  37.  
  38. <CalendarView
  39. android:id="@+id/calendarView"
  40. android:layout_width="match_parent"
  41. android:layout_height="wrap_content" />
  42.  
  43. <SeekBar
  44. android:id="@+id/seekBar2"
  45. style="@style/Widget.AppCompat.SeekBar.Discrete"
  46. android:layout_width="match_parent"
  47. android:layout_height="wrap_content"
  48. android:max="10"
  49. android:progress="3" />
  50.  
  51. <Switch
  52. android:id="@+id/switch1"
  53. android:layout_width="match_parent"
  54. android:layout_height="wrap_content"
  55. android:text="Switch" />
  56.  
  57. <com.google.android.material.floatingactionbutton.FloatingActionButton
  58. android:id="@+id/floatingActionButton"
  59. android:layout_width="match_parent"
  60. android:layout_height="wrap_content"
  61. android:clickable="true"
  62. app:srcCompat="@android:drawable/ic_input_add" />
  63.  
  64.  
  65. </LinearLayout>

Then the outcome will be the following :

Theme and style app

Finally as stated earlier , a theme can be set programmatically , using the settheme method , as in the following code fragment .

  1. import androidx .appcompat .app .AppCompatActivity;
  2. import android .os .Bundle;
  3.  
  4. public class
  5. MainActivity
  6. extends AppCompatActivity {
  7.  
  8. @Override
  9. protected void
  10. onCreate
  11. (Bundle savedInstanceState ){
  12. super .onCreate (savedInstanceState );
  13. setTheme (R .style .Theme_MaterialComponents_NoActionBar_Bridge );
  14. /* setTheme must be called before
  15. any views are instantiated in
  16. the Context , so for example
  17. before setContentView is
  18. called .*/
  19. setContentView (R .layout .activity_main ); } }