{"id":3323,"date":"2013-01-01T16:56:57","date_gmt":"2013-01-02T00:56:57","guid":{"rendered":"http:\/\/blog.xamarin.com\/?p=3323"},"modified":"2013-01-01T16:56:57","modified_gmt":"2013-01-02T00:56:57","slug":"android-tricks-using-shape-for-theming","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/android-tricks-using-shape-for-theming\/","title":{"rendered":"Android Tricks: Using Shape for Theming"},"content":{"rendered":"<p>\t\t\t\tAndroid supports all sorts of &#8220;drawable&#8221; objects (the <code>Bitmap<\/code> drawable, which can contain images in formats like PNG and JPG, is one example). The one we are going to talk about today is so-called <a href=\"http:\/\/developer.android.com\/reference\/android\/graphics\/drawable\/ShapeDrawable.html\">Shape drawable<\/a>.<\/p>\n<p>Shape drawables are defined with a simple XML syntax and stored in the <code>drawable<\/code> folder of an Android solution. Several basic shapes are supported, including rectangles (with optional rounded corners) and ovals. The border and fill of a shape can be specified&mdash;the user can choose a solid fill color or a multistep gradient. The Android designer in MonoDevelop is actually smart enough to interpret XML drawables, so you can change them and see the result appear live in MonoDevelop&#8217;s Android layout designer as you are building your application!<\/p>\n<p>A standalone shape drawable is probably not very useful, but shapes can be immensely helpful when used together in conjunction with other Views. You can use a combination of simple shapes and other elements for simple widget theming. In the following image, which was first shown in my <a href=\"\/mfa-tricks-2-2-fingers-expandable-layout\/\">expandable ListView blog post<\/a>, you can see three different shape drawables in action:<\/p>\n<p><a href=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/expandhelper-collapsed.png\"><img decoding=\"async\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/expandhelper-collapsed.png\" alt=\"expandhelper-collapsed\" width=\"800\" height=\"194\" class=\"aligncenter size-full wp-image-3366\" \/><\/a><\/p>\n<p>First, the circle around the coffee cup is an oval drawable filled with a semi-transparent color to darken the main header background color. The following XML is the source code for that shape:<\/p>\n<pre class=\"lang:xml decode:true\">\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;\n&lt;shape xmlns:android=&quot;http:\/\/schemas.android.com\/apk\/res\/android&quot; android:shape=&quot;oval&quot;&gt;\n    &lt;solid android:color=&quot;#42000000&quot; \/&gt;\n    &lt;padding android:left=&quot;8dp&quot; android:top=&quot;8dp&quot;\n             android:right=&quot;8dp&quot; android:bottom=&quot;8dp&quot; \/&gt;\n&lt;\/shape&gt;\n<\/pre>\n<p>In the following XML layout, you can see the shape referenced in the <code>android:background<\/code> attribute:<\/p>\n<pre class=\"lang:xml decode:true\">\n&lt;FrameLayout\n   android:layout_width=&quot;80dp&quot;\n   android:layout_height=&quot;80dp&quot;\n   android:layout_gravity=&quot;center&quot;\n   android:background=&quot;@drawable\/aitemring&quot;&gt;\n   &lt;ImageView\n       android:src=&quot;@drawable\/coffee&quot;\n       android:layout_width=&quot;fill_parent&quot;\n       android:layout_height=&quot;fill_parent&quot;\n       android:scaleType=&quot;center&quot; \/&gt;\n&lt;\/FrameLayout&gt;\n<\/pre>\n<p>To force the oval shape to be square, you have to set the size of the <code>FrameLayout<\/code> accordingly. The necessary value will depend on your application. You can even set it dynamically, though that has to be done in code.<\/p>\n<p>Second, the bottom bar where the location and date appear is a rectangle shape filled with a gradient to give it volume. Gradients like that one are a convenient way to add some eye candy to your UI elements. For more details, see the <a href=\"http:\/\/parleys.com\/#st=5&amp;id=2897&amp;sl=6\">Android graphics talk<\/a> (the relevant part starts at 3:10) that Google developers <a href=\"https:\/\/twitter.com\/chethaase\">Chet Haase<\/a> and <a href=\"https:\/\/twitter.com\/romainguy\">Romain Guy<\/a> presented at Devoxx.<\/p>\n<p>The XML code for the bar shape is given below:<\/p>\n<pre class=\"lang:xml decode:true\">\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;\n&lt;shape xmlns:android=&quot;http:\/\/schemas.android.com\/apk\/res\/android&quot; android:shape=&quot;rectangle&quot;&gt;\n    &lt;solid android:color=&quot;#AA212121&quot; \/&gt;\n    &lt;gradient android:angle=&quot;90&quot;\n    \tandroid:startColor=&quot;#59AAAAAA&quot;\n    \tandroid:endColor=&quot;#59EEEEEC&quot; \/&gt;\n    &lt;padding android:left=&quot;8dp&quot; android:top=&quot;8dp&quot;\n             android:right=&quot;8dp&quot; android:bottom=&quot;8dp&quot; \/&gt;\n&lt;\/shape&gt;\n<\/pre>\n<p>In this example, I combined the <code>&lt;solid\/&gt;<\/code> element to paint my base color and the <code>&lt;gradient\/&gt;<\/code> element to perform the shading on top of it.<\/p>\n<p>Again, to apply the effect, simply use the <code>android:background<\/code> attribute in one of your layout elements:<\/p>\n<pre class=\"lang:xml decode:true\">\n&lt;RelativeLayout\n    android:layout_width=&quot;fill_parent&quot;\n    android:layout_height=&quot;fill_parent&quot;\n    android:background=&quot;@drawable\/aitemfooter&quot;&gt;\n<\/pre>\n<p>Finally, the border (with round corners) that is displayed around the whole UI widget is also a shape drawable:<\/p>\n<pre class=\"lang:xml decode:true\">\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;\n&lt;shape xmlns:android=&quot;http:\/\/schemas.android.com\/apk\/res\/android&quot; android:shape=&quot;rectangle&quot;&gt;\n    &lt;solid android:color=&quot;#00FFFFFF&quot; \/&gt;\n    &lt;stroke android:width=&quot;1dp&quot; android:color=&quot;#ffaaaaaa&quot; \/&gt;\n    &lt;corners android:radius=&quot;3px&quot; \/&gt;\n&lt;\/shape&gt;\n<\/pre>\n<p>In this case, since we only care about the border, we set the filling color of the shape to be transparent. The <code>&lt;stroke\/&gt;<\/code> element defines your border while the <code>&lt;corners\/&gt;<\/code> element makes it round.<\/p>\n<p>As a final treat, if you combine things, you can also make a fancy header using shapes:<\/p>\n<p><a href=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/header-shape-gradient.png\"><img decoding=\"async\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/header-shape-gradient.png\" alt=\"header-shape-gradient\" width=\"800\" height=\"106\" class=\"aligncenter size-full wp-image-3367\" \/><\/a><\/p>\n<p>The following code defines the Shape XML (notice the left padding value, which is used for indenting):<\/p>\n<pre class=\"lang:xml decode:true\">\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;\n&lt;shape xmlns:android=&quot;http:\/\/schemas.android.com\/apk\/res\/android&quot; android:shape=&quot;rectangle&quot;&gt;\n    &lt;solid android:color=&quot;#AA212121&quot; \/&gt;\n    &lt;gradient android:angle=&quot;-90&quot;\n    \tandroid:startColor=&quot;#447F7F7F&quot;\n    \tandroid:endColor=&quot;#007F7F7F&quot; \/&gt;\n    &lt;stroke android:width=&quot;1dp&quot; android:color=&quot;#447F7F7F&quot; \/&gt;\n    &lt;padding android:left=&quot;48dp&quot; android:top=&quot;8dp&quot;\n             android:right=&quot;8dp&quot; android:bottom=&quot;8dp&quot; \/&gt;\n&lt;\/shape&gt;\n<\/pre>\n<p>The following code defines the <code>TextView<\/code> XML (as always, the shape is registered in <code>android:background<\/code> attribute):<\/p>\n<pre class=\"lang:xml decode:true\">\n&lt;TextView\n    android:text=&quot;What&quot;\n    android:textAppearance=&quot;?android:attr\/textAppearanceLarge&quot;\n    android:layout_width=&quot;fill_parent&quot;\n    android:layout_height=&quot;wrap_content&quot;\n    android:background=&quot;@drawable\/header&quot; \/&gt;\n<\/pre>\n<p>Now go add a bit of &#8220;woowoo&#8221; to your app!\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Android supports all sorts of &#8220;drawable&#8221; objects (the Bitmap drawable, which can contain images in formats like PNG and JPG, is one example). The one we are going to talk about today is so-called Shape drawable. Shape drawables are defined with a simple XML syntax and stored in the drawable folder of an Android solution. [&hellip;]<\/p>\n","protected":false},"author":1925,"featured_media":39167,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[5,4],"class_list":["post-3323","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers","tag-android","tag-xamarin-platform"],"acf":[],"blog_post_summary":"<p>Android supports all sorts of &#8220;drawable&#8221; objects (the Bitmap drawable, which can contain images in formats like PNG and JPG, is one example). The one we are going to talk about today is so-called Shape drawable. Shape drawables are defined with a simple XML syntax and stored in the drawable folder of an Android solution. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/3323","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/users\/1925"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=3323"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/3323\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media\/39167"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=3323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=3323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=3323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}