{"id":2069,"date":"2015-01-31T17:09:15","date_gmt":"2015-01-31T11:39:15","guid":{"rendered":"http:\/\/codetheory.in\/?p=2069"},"modified":"2015-01-31T18:53:01","modified_gmt":"2015-01-31T13:23:01","slug":"android-themes-styles-and-attributes","status":"publish","type":"post","link":"https:\/\/codetheory.in\/android-themes-styles-and-attributes\/","title":{"rendered":"Android Themes, Styles and Attributes"},"content":{"rendered":"
One thing that bugs me from time to time is the appearance and disappearance of the Action bar or the change in its UI with similar XML code and Java API calls but with different themes. I was somehow managing till now but then decided that it’s probably a good time to understand the concept of themes and styles in android to make sure I know why there’s that frustrating difference just by a small change in the theme name ( <\/p>\n I’m not going to get into full details but the aim of this small article is to sort of compile all sorts of useful resources with regards to themes and styles so that one can quickly go through them and understand. The difference not only affects Action bars but also all sorts of other UI elements including the Window itself that actually gets styled by the themes you specify. Some ship with the app compat support library, some ship with the Android platform itself, some ship with some other library and some can be custom made by yourself or a teammate.<\/p>\n Consider going through these resources:<\/p>\n Once your styles, themes and even other resources like dimensions, strings, etc. are defined in their respective resource files (can also be combined but keep separate for sanity), they can be used in the layout files like It is also very important to understand attributes that you can access using this syntax – So if we take this code for example:<\/p>\n Each of them, i.e., Hope that helps!<\/p>\n","protected":false},"excerpt":{"rendered":" One thing that bugs me from time to time is the appearance and disappearance of the Action bar or the change in its UI with similar XML code and Java API calls but with different themes. I was somehow managing till now but then decided that it’s probably a good time to understand the concept … Continue reading “Android Themes, Styles and Attributes”<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[121,40],"tags":[105,89,147,146],"_links":{"self":[{"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/posts\/2069"}],"collection":[{"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/comments?post=2069"}],"version-history":[{"count":4,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/posts\/2069\/revisions"}],"predecessor-version":[{"id":2073,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/posts\/2069\/revisions\/2073"}],"wp:attachment":[{"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/media?parent=2069"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/categories?post=2069"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/tags?post=2069"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}res\/values\/styles.xml<\/code> then used in
AndroidManifest.xml<\/code>) that you sometimes do not even realize while creating new projects as it is set automatically based on the API levels you target.<\/p>\n
\n
android:text=\"@android:string\/string_name<\/code> or
android:text=\"@string\/string_name<\/code> where the former is defined in the platform whereas the latter in your own app.<\/p>\n
?android:attr\/someAttribute<\/code> – where the value of the attribute is generally defined in
themes.xml<\/code> and the definition of attribute (that it can exist) is defined in
attrs.xml<\/code>. Here are some very useful link on this concept of attributes:<\/p>\n
\n
\r\n<TextView android:id="@android:id\/text1"\r\n android:layout_width="match_parent"\r\n android:layout_height="wrap_content"\r\n android:layout_marginLeft="?android:attr\/listPreferredItemPaddingLeft"\r\n android:layout_marginTop="8dip"\r\n android:textAppearance="?android:attr\/textAppearanceListItem"\r\n \/>\r\n<\/pre>\n
android:marginTop<\/code> for instance is an attribute that’ll be defined by the platform where
android<\/code> is the package name and
marginTop<\/code> is the attribute name. Similarly
listPreferredItemPaddingLeft<\/code> is another attribute defined first and then somewhere in the themes XML resource its value is set. It is then accessed with that syntax
?[package]:attr\/attr_name<\/code>.<\/p>\n