<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title><![CDATA[Sage42]]></title>
  <link href="http://www.sage42.org/atom.xml" rel="self"/>
  <link href="http://www.sage42.org/"/>
  <updated>2013-01-30T00:00:26+08:00</updated>
  <id>http://www.sage42.org/</id>
  <author>
    <name><![CDATA[Corey Scott]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[Make better android buttons with image and text]]></title>
    <link href="http://www.sage42.org/blog/2013/01/20/make-better-android-buttons-with-image-and-text/"/>
    <updated>2013-01-20T00:03:00+08:00</updated>
    <id>http://www.sage42.org/blog/2013/01/20/make-better-android-buttons-with-image-and-text</id>
    <content type="html"><![CDATA[<p>Today the design comes in from the designer for what I thought at first look was a pair of simple buttons with text and icon.
Turns out, it wasn&#8217;t so simple.</p>

<h2>The Design</h2>

<p><img src="http://www.sage42.org/images/20130120/design.jpg"></p>

<p>Now I incorrectly assumed that buttons like those at the top were pretty common place either I&#8217;m wrong or judging from the stackoverflow.com articles
others are creating buttons that aren&#8217;t as &#8216;pixel perfect&#8217; as my situation requires.</p>

<p>Anyhow, on with the code:</p>

<!-- more -->


<h2>Version 1: Text Button with Drawable</h2>

<p>Using what appears to the the &#8216;standard&#8217; approach.</p>

<div><script src='https://gist.github.com/4577246.js?file=activity_main.xml'></script>
<noscript><pre><code>&lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    tools:context=&quot;.MainActivity&quot; &gt;

    &lt;TextView
        android:id=&quot;@+id/title_v1&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_alignParentTop=&quot;true&quot;
        android:gravity=&quot;center&quot;
        android:textAppearance=&quot;?android:attr/textAppearanceMedium&quot;
        android:text=&quot;@string/title_v1&quot; /&gt;

    &lt;LinearLayout
        android:layout_below=&quot;@+id/title_v1&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot; &gt;

        &lt;Button
            android:id=&quot;@+id/btn_cancel&quot;
            android:layout_width=&quot;0dp&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:layout_weight=&quot;1&quot;
            android:drawableLeft=&quot;@drawable/ic_cancel&quot;
            android:minHeight=&quot;48dp&quot;
            android:onClick=&quot;onClickCancel&quot;
            android:text=&quot;@android:string/cancel&quot; /&gt;

        &lt;Button
            android:id=&quot;@+id/btn_ok&quot;
            android:layout_width=&quot;0dp&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:layout_weight=&quot;1&quot;
            android:drawableLeft=&quot;@drawable/ic_ok&quot;
            android:minHeight=&quot;48dp&quot;
            android:onClick=&quot;onClickOk&quot;
            android:text=&quot;@android:string/ok&quot; /&gt;
    &lt;/LinearLayout&gt;

&lt;/RelativeLayout&gt;</code></pre></noscript></div>


<p><img src="http://www.sage42.org/images/20130120/v1.jpg"></p>

<p>As you can see, there is an inconsistent spacing between the text and the drawable.
While I can move the drawable in from the left using android:drawablePadding=&#8221;-<some value>dp&#8221;
this still with result in inconsistent results.<br/>
This is particularly obvious when the some non-english languages where the length of &#8220;OK&#8221; and &#8220;CANCEL&#8221; can differ quite a bit.</p>

<h2>Version 2: Compound Drawable with onClick</h2>

<p>Resorting to just a standard compound drawable (image and button together) doesnt get you any closer, its still the same 2 problems.</p>

<p>Problem 1: The padding on the drawable is relative to the outside of the &#8220;button&#8221; instead of being relative to the text.</p>

<p>Problem 2: The text and drawable together are not centered in the &#8220;button&#8221;.</p>

<div><script src='https://gist.github.com/4577398.js?file=activity_main.xml'></script>
<noscript><pre><code>&lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    tools:context=&quot;.MainActivity&quot; &gt;

    &lt;TextView
        android:id=&quot;@+id/title_v2&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_alignParentTop=&quot;true&quot;
        android:gravity=&quot;center&quot;
        android:text=&quot;@string/title_v2&quot;
        android:textAppearance=&quot;?android:attr/textAppearanceMedium&quot; /&gt;

    &lt;LinearLayout
        android:id=&quot;@+id/content_v2&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_below=&quot;@+id/title_v2&quot;
        android:orientation=&quot;horizontal&quot; &gt;

        &lt;TextView
            android:id=&quot;@+id/btn_cancel_v2&quot;
            android:layout_width=&quot;0dp&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:layout_margin=&quot;4dp&quot;
            android:layout_weight=&quot;1&quot;
            android:background=&quot;#d6d6d6&quot;
            android:drawableLeft=&quot;@drawable/ic_cancel&quot;
            android:drawablePadding=&quot;16dp&quot;
            android:gravity=&quot;center&quot;
            android:minHeight=&quot;48dp&quot;
            android:onClick=&quot;onClickCancel&quot;
            android:paddingLeft=&quot;16dp&quot;
            android:paddingRight=&quot;16dp&quot;
            android:text=&quot;@android:string/cancel&quot; /&gt;

        &lt;TextView
            android:id=&quot;@+id/btn_ok_v2&quot;
            android:layout_width=&quot;0dp&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:layout_margin=&quot;4dp&quot;
            android:layout_weight=&quot;1&quot;
            android:background=&quot;#d6d6d6&quot;
            android:drawableLeft=&quot;@drawable/ic_ok&quot;
            android:drawablePadding=&quot;16dp&quot;
            android:gravity=&quot;center&quot;
            android:minHeight=&quot;48dp&quot;
            android:onClick=&quot;onClickCancel&quot;
            android:paddingLeft=&quot;16dp&quot;
            android:paddingRight=&quot;16dp&quot;
            android:text=&quot;@android:string/ok&quot; /&gt;
    &lt;/LinearLayout&gt;

&lt;/RelativeLayout&gt;</code></pre></noscript></div>


<p><img src="http://www.sage42.org/images/20130120/v2.jpg"></p>

<h2>Version 3: Way too much code, but it works.</h2>

<p>When I &#8220;waste&#8221; so much time on something that is seemingly so trivial I tend to resort to a &#8220;sledgehammer&#8221; approach.
This is an approach that I know works even though I consider it ugly and not at all well crafted.</p>

<div><script src='https://gist.github.com/4577541.js?file=activity_main.xml'></script>
<noscript><pre><code>&lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    tools:context=&quot;.MainActivity&quot; &gt;

    &lt;TextView
        android:id=&quot;@+id/title_v3&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_alignParentTop=&quot;true&quot;
        android:gravity=&quot;center&quot;
        android:text=&quot;@string/title_v3&quot;
        android:textAppearance=&quot;?android:attr/textAppearanceMedium&quot; /&gt;

    &lt;LinearLayout
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_below=&quot;@+id/title_v3&quot;
        android:baselineAligned=&quot;false&quot;
        android:orientation=&quot;horizontal&quot; &gt;

        &lt;RelativeLayout
            android:id=&quot;@+id/btn_cancel&quot;
            android:layout_width=&quot;0dp&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:layout_marginLeft=&quot;8dp&quot;
            android:layout_marginRight=&quot;4dp&quot;
            android:layout_weight=&quot;1&quot;
            android:background=&quot;#d6d6d6&quot;
            android:clickable=&quot;true&quot;
            android:minHeight=&quot;48dp&quot;
            android:onClick=&quot;onClickCancel&quot;
            android:paddingLeft=&quot;8dp&quot;
            android:paddingRight=&quot;4dp&quot; &gt;

            &lt;TextView
                android:layout_width=&quot;wrap_content&quot;
                android:layout_height=&quot;wrap_content&quot;
                android:layout_centerHorizontal=&quot;true&quot;
                android:layout_centerVertical=&quot;true&quot;
                android:drawableLeft=&quot;@drawable/ic_cancel&quot;
                android:drawablePadding=&quot;16dp&quot;
                android:gravity=&quot;center&quot;
                android:text=&quot;@android:string/cancel&quot;
                android:textAppearance=&quot;?android:attr/textAppearanceButton&quot; /&gt;
        &lt;/RelativeLayout&gt;

        &lt;RelativeLayout
            android:id=&quot;@+id/menu_ok&quot;
            android:layout_width=&quot;0dp&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:layout_marginLeft=&quot;4dp&quot;
            android:layout_marginRight=&quot;8dp&quot;
            android:layout_weight=&quot;1&quot;
            android:background=&quot;#d6d6d6&quot;
            android:clickable=&quot;true&quot;
            android:minHeight=&quot;48dp&quot;
            android:onClick=&quot;onClickOk&quot;
            android:paddingLeft=&quot;4dp&quot;
            android:paddingRight=&quot;8dp&quot; &gt;

            &lt;TextView
                android:layout_width=&quot;wrap_content&quot;
                android:layout_height=&quot;wrap_content&quot;
                android:layout_centerHorizontal=&quot;true&quot;
                android:layout_centerVertical=&quot;true&quot;
                android:drawableLeft=&quot;@drawable/ic_ok&quot;
                android:drawablePadding=&quot;16dp&quot;
                android:gravity=&quot;center&quot;
                android:text=&quot;@android:string/ok&quot;
                android:textAppearance=&quot;?android:attr/textAppearanceButton&quot; /&gt;
        &lt;/RelativeLayout&gt;
    &lt;/LinearLayout&gt;

&lt;/RelativeLayout&gt;</code></pre></noscript></div>


<p><img src="http://www.sage42.org/images/20130120/v3.jpg"></p>

<h2>Conclusion</h2>

<p>I think I would feel less disgusted by this result if I could find some time to refactor it into some standard component.
The performance will still suffer slightly from the complicated layout but at least the coding experience and readability of the code would be greatly increase.</p>
]]></content>
  </entry>
  
</feed>
