Android Drawable question.
Posted
by Tarmon
on Stack Overflow
See other posts from Stack Overflow
or by Tarmon
Published on 2010-04-25T01:31:47Z
Indexed on
2010/04/25
1:43 UTC
Read the original article
Hit count: 395
android
|android-sdk
Hey Everyone,
I am trying to create a drawable in code and change the color based on some criteria. I can get it to work but it doesn't want to let me set the padding on the view. Any help would be appreciated.
<?xml version="1.0" encoding="utf-8"?>
<ImageView
android:id="@+id/icon"
android:layout_width="50px"
android:layout_height="fill_parent"
/>
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="17px"
android:textSize="28sp"
/>
ImageView icon = (ImageView) row.findViewById(R.id.icon);
ShapeDrawable mDrawable;
int x = 0;
int y = 0;
int width = 50;
int height = 50;
float[] outerR = new float[] { 12, 12, 12, 12, 12, 12, 12, 12 };
mDrawable = new ShapeDrawable(new RoundRectShape(outerR, null, null));
mDrawable.setBounds(x, y+height, x + width, y);
switch(position){
case 0:
mDrawable.getPaint().setColor(0xffff0000); //Red
break;
case 1:
mDrawable.getPaint().setColor(0xffff0000); //Red
break;
case 2:
mDrawable.getPaint().setColor(0xff00c000); //Green
break;
case 3:
mDrawable.getPaint().setColor(0xff00c000); //Green
break;
case 4:
mDrawable.getPaint().setColor(0xff0000ff); //Blue
break;
case 5:
mDrawable.getPaint().setColor(0xff0000ff); //Blue
break;
case 6:
mDrawable.getPaint().setColor(0xff696969); //Gray
break;
case 7:
mDrawable.getPaint().setColor(0xff696969); //Gray
break;
case 8:
mDrawable.getPaint().setColor(0xffffff00); //Yellow
break;
case 9:
mDrawable.getPaint().setColor(0xff8b4513); //Brown
break;
case 10:
mDrawable.getPaint().setColor(0xff8b4513); //Brown
break;
case 11:
mDrawable.getPaint().setColor(0xff8b4513); //Brown
break;
case 12:
mDrawable.getPaint().setColor(0xffa020f0); //Purple
break;
case 13:
mDrawable.getPaint().setColor(0xffff0000); //Red
break;
case 14:
mDrawable.getPaint().setColor(0xffffd700); //Gold
break;
case 15:
mDrawable.getPaint().setColor(0xffff6600); //Orange
break;
}
icon.setBackgroundDrawable(mDrawable);
icon.setPadding(5, 5, 5, 5);
If I set the padding in XML it just ignores it.
Thanks, Rob
© Stack Overflow or respective owner