title: Drawing a line on top of a button in Android?
body: <p>I am messing around with a calendar application in Android and would like to be able to lines over the top of a button or place an image over the button at a specific Y location. Is there a way to do this? I don't think the normal drawables (left,top,right,bottom) will give me the positioning I'm looking for.
  Thanks!</p>
  
  <p><img src="https://i.stack.imgur.com/5rNod.png" alt="calendar view with images/lines on buttons"></p>
  
  <p><strong>UPDATE:</strong> </p>
  
  <p>Here are the results of the code below for drawing a line on a button drawable. What I had to do was give the button a transparent png image for the top drawable in the XML code. Without it, it wouldn't work. </p>
  
  <p>One of the problems I see here is I am quite limited to just a few lines. One of the replies said something about stacking buttons on a framelayout but not sure I follow. Will have to experiment some more. </p>
  
  <p>Any way to draw on the whole button instead of just the small drawable areas?</p>
  
  <p><img src="https://i.stack.imgur.com/68Xc2.png" alt="Resules after drawing lines on top drawable"></p>
  
  <pre><code>package com.test.buttondraw;
  
  import android.app.Activity;
  import android.graphics.Bitmap;
  import android.graphics.Canvas;
  import android.graphics.Color;
  import android.graphics.Paint;
  import android.graphics.Paint.Style;
  import android.graphics.drawable.BitmapDrawable;
  import android.os.Bundle;
  import android.widget.Button;
  
  public class ButtonDrawTestActivity extends Activity
  {
      /** Called when the activity is first created. */
      @Override
      public void onCreate (Bundle savedInstanceState)
      {
          super.onCreate (savedInstanceState);
          setContentView (R.layout.main);
  
          Button b1 = (Button) findViewById (R.id.button1);
  
          // get the second array element (0 based) which is the top drawable
          BitmapDrawable d = (BitmapDrawable) b1.getCompoundDrawables ()[1];
  
          if (d != null)
          {   // thanks to dbrettschneider for bitmap code sample
              Bitmap b1src = d.getBitmap ();
              Bitmap tmp = Bitmap.createBitmap(b1src.getWidth(), b1src.getHeight(), b1src.getConfig());
              BitmapDrawable bd = new BitmapDrawable (tmp);
              bd.setBounds (d.getBounds ());
  
              Canvas c = new Canvas(tmp);
  
              Paint p = new Paint();
              p.setAntiAlias(true);
  
              c.drawBitmap (b1src, 0, 0, p);
  
              // set thickness of line
              p.setStyle (Style.STROKE);
              p.setStrokeWidth (5);
  
              // colors to use for each y position
              int [] clr    = {Color.BLACK,     Color.RED,     Color.GREEN, 
                                    Color.MAGENTA, Color.WHITE, Color.YELLOW, 
                                    Color.CYAN,        Color.BLUE,   Color.DKGRAY };
              int ypos = 10;
  
              // draw a different colored line for each element in the ypos array
              for (int i = 0; i &lt; clr.length; i++)
              {
                  p.setColor(clr[i]); 
                  c.drawLine (0, ypos, b1src.getWidth (), ypos, p);
                  ypos += 8;
              }
  
              b1.setCompoundDrawables (null, bd, null, null);
          }
  
      }
  
  }
  </code></pre>
  
  <p>Here is the xml code:</p>
  
  <p>
  </p>
  
  <pre><code>&lt;Button
      android:id="@+id/button1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:drawableTop="@drawable/png_transbg"
      android:maxHeight="128dp"
      android:maxWidth="128dp"
      android:minHeight="128dp"
      android:minWidth="128dp"
      android:text="Button" /&gt;
  </code></pre>
  
  <p></p>