I’m trying to draw a line on the Compound Drawable of a regular old Android button. I started out by just trying to get the drawable of the button using Button.getCompoundDrawables()[1] but the lines would never show up. So, I went ahead and put an actual image in my XML layout for the compound drawable on the button. This works OK (the orange square in the pic) but when the phone is rotated, the orange square doesn’t resize, proprotional to the button, so it ends up being way too big. Do I need to be calling getBounds() upon rotation or something?
There must be some resizing going on with the drawable because if you notice, the red line goes to the corners in Horizontal orientation, but not in the vertical; it’s off or something. The orange square resides as different sizes in drawable-[lhm]dpi/ directories, but I do not have two separate layouts for horizontal and vertical.
Code for drawing the line:
@Override
public View getView (int position, View convertView, ViewGroup parent)
{
View row = convertView;
if (row == null)
{
LayoutInflater inflater = (LayoutInflater) _context.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate (R.layout.monthview, parent, false);
}
btn_cell = (Button) row.findViewById (R.id.bcell);
...
BitmapDrawable btn_draw = (BitmapDrawable) btn_cell.getCompoundDrawables ()[1];
if (btn_draw != null)
{
Log.d (TAG, "+++++++++++++ drawing line");
Bitmap btn_bmp = btn_draw.getBitmap ();
Bitmap offscreen_bmp = Bitmap.createBitmap(btn_bmp.getWidth(), btn_bmp.getHeight(), btn_bmp.getConfig());
BitmapDrawable offscreen_draw = new BitmapDrawable (offscreen_bmp);
offscreen_draw.setBounds (btn_draw.getBounds ());
Canvas c = new Canvas(offscreen_bmp);
// draw line
Paint p = new Paint();
p.setAntiAlias(true);
p.setStrokeWidth(1);
p.setStyle(Style.FILL_AND_STROKE);
p.setColor(Color.RED);
c.drawBitmap (btn_bmp, 0, 0, p);
c.drawLine (0, 0, offscreen_bmp.getWidth (), offscreen_bmp.getHeight (), p);
(R.drawable.cal_left_arrow_off), null, null);
btn_cell.setCompoundDrawables(null, offscreen_draw, null, null);
}


In case it helps someone else, I ended up doing away with the Button and instead using an ImageView. I ran into a (apparently) typical problem of just how to draw a line on the ImageView. I knew about sub-classing ImageView and drawing lines in the
onDraw()method, however I didn’t know you could reference that “custom” sub-class inside Eclipse’s XML Layout GUI. All I needed to do was create a myImageView class and then reference it in the XML source code. Instead of specifying<ImageView>I needed to use<com.example.myImageView>and everything worked.I was using myImageView in a custom BaseAdapter so getting the lines placed on myImageView in the
getView()method was just a matter of:com.example.test.myImageView.java:
xml file: