0

I have a checkbox with a custom icon (bigger than the default icon) for the checkbox which I set like this:

mon  = new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.checkbox_on));
moff = new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.checkbox_off));

state = new StateListDrawable();
state.addState(new int[] {-android.R.attr.state_checked}, moff);
state.addState(new int[] { android.R.attr.state_checked}, mon);

cb.setButtonDrawable(state);

It works well on a phone but on a tablet with a larger screen and resolution the icon is much larger but the text at the same time is much smaller than on the phone and the icon overlays the first few letters of the text. How can I prevent this?

Edit:

I verified that the problem is from the custom checkbox icon having a different size than the default checkbox icon. The sizes seem to be different for both devices, so is there a maybe way to find out which size the default checkbox icon has so I can make necessary adjustments to the size of custom icons or the padding ?

HardCoder
  • 3,026
  • 6
  • 32
  • 52

3 Answers3

0

this will happened because of different screen densities, we can avoid by using this code : check this Question Spacing between CheckBox and text

Community
  • 1
  • 1
RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166
  • I read that already but it didn't really help me. It seems as if android places the text in a checkbox at a specific position dependent of the default checkbox icon size, and the default size of the icons are just different on my 2 devices. I use a custom checkbox icon that is about the size of the checkbox on my phone but its way bigger than the smaller checkbox icon on my tablet. – HardCoder May 31 '12 at 11:11
  • It's not helping me with custom checkboxes that have a different size from the default. – HardCoder May 31 '12 at 11:47
0

I solved it by scaling the checkbox icon dynamically based on the dpi. So it always fits and the text is also ok:

int gg = (int) (this.getResources().getDisplayMetrics().density * cb_info.getCompoundPaddingLeft());
mon  = new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.checkbox_on ));
int scalex = gg;
int scaley = mon.getBitmap().getHeight() * gg / mon.getBitmap().getWidth ();

mon  = new BitmapDrawable(Bitmap.createScaledBitmap( BitmapFactory.decodeResource(getResources(), R.drawable.checkbox_on ), scalex, scaley, true));
moff = new BitmapDrawable(Bitmap.createScaledBitmap( BitmapFactory.decodeResource(getResources(), R.drawable.checkbox_off), scalex, scaley, true));
HardCoder
  • 3,026
  • 6
  • 32
  • 52
0

Still whoever is finding for the solution please use the below line in the checkbox attribute in the

android:drawablePadding="15dp"

this will solve problem of overlapping...

Nagesh
  • 11
  • 1