0

I'm trying to set a custom background to a check box, but it's doubled, and i don't know why.

My Main activity :

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    CheckBox cb = new CheckBox(getApplicationContext());
    DrawableCheckButton d = new DrawableCheckButton(getApplicationContext());
    cb.setBackgroundDrawable(d);
    setContentView(cb);
}

DrawableCheckButton class :

public class DrawableCheckButton extends StateListDrawable {

public DrawableCheckButton(Context context) {
    int[] state_checked = { android.R.attr.state_checked };
    int[] state_active = { android.R.attr.state_enabled };
    CustomDrawableCheckButton d = new CustomDrawableCheckButton(context);
    CustomDrawableCheckButton2 d2 = new CustomDrawableCheckButton2(context);
    addState(state_checked, d);
    addState(state_active, d2);
}

}

And the other class look like this

public class CustomDrawableCheckButton extends Drawable{
private Context context;
public CustomDrawableCheckButton(Context context) {
    // TODO Auto-generated constructor stub
    this.context = context;
}
@Override
public void draw(Canvas canvas) {
    Path path1 = new Path();
    Paint paint1 = new Paint();
    path1.moveTo(0, 3);
    path1.lineTo(17, 3);
    path1.moveTo(0, 3);
    path1.lineTo(0, 23);
    path1.lineTo(20, 23);
    path1.lineTo(20, 14);
    paint1.setColor(context.getResources().getColor(R.color.orange));
    paint1.setStrokeWidth(1);
    paint1.setStyle(Paint.Style.STROKE);
    //
    Path path2 = new Path();
    Paint paint2 = new Paint();
    path2.moveTo(4,10);
    path2.lineTo(10, 16);
    path2.lineTo(22, 4);
    paint2.setColor(context.getResources().getColor(R.color.purple));
    paint2.setStrokeWidth(5);
    paint2.setStyle(Paint.Style.STROKE);
    canvas.drawPath(path1, paint1);
    canvas.drawPath(path2, paint2);

}

I need some help.

Tsunaze
  • 3,204
  • 7
  • 44
  • 81

2 Answers2

0

Here's my suggestion:
In MainActivity, you should avoid using ApplicationContext, see this question for more information, and use setButtonDrawable instead of setBackgroundDrawable:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    CheckBox cb = new CheckBox(this);
    DrawableCheckButton d = new DrawableCheckButton(this);
    cb.setButtonDrawable(d);
    setContentView(cb);
}

In DrawableCheckButton:

public DrawableCheckButton(Context context) {
    CustomDrawableCheckButton d = new CustomDrawableCheckButton(context);
    addState(new int[]{android.R.attr.state_checked }, d);
    CustomDrawableCheckButton2 d2 = new CustomDrawableCheckButton2(context);
    addState(new int[]{}, d2);
}

Hope that help. Please let me know if it works or not.

Community
  • 1
  • 1
nlt
  • 555
  • 7
  • 11
  • Sorry, i found the answer, it's much simplier. instead of doing cb.setBackgroundDrawable(), i just did cb.setButtonDrawable(). Background is for the all and button is for the mark. – Tsunaze Feb 28 '13 at 17:55
  • sorry for previous answer, I have updated it for anyone who meets the same problem. – nlt Feb 28 '13 at 18:49
0

The answer was instead of doing cb.setBackgroundDrawable(), i just did cb.setButtonDrawable(). Background is for the all and button is for the mark.

Tsunaze
  • 3,204
  • 7
  • 44
  • 81