0

i am progrmatically creating radio button, and it works fine,

RadioImageButton RadioImageButton = new RadioImageButton(this);
    RadioImageButton.setGravity(Gravity.CENTER);
    RadioImageButton.setId(buttonId);
    RadioImageButton.setTextColor(Color.BLACK);
   RadioImageButton.setCompoundDrawablesWithIntrinsicBounds(icon, null,null, null)// use this to set the icon
    RadioImageButton.setBackgroundDrawable(drawable);
    RadioGroup.LayoutParams radioImageButtonParams = new RadioGroup.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
        LinearLayout.LayoutParams.MATCH_PARENT, 1f);
    radioImageButtonParams.setMargins(0, 0, 1, 0);

    RadioGroup.addView(RadioImageButton, radioImageButtonParams);

In RadioImageButton class

Drawable image;

    public RadioImageButton(Context context) {
    super(context);
    setButtonDrawable(android.R.color.transparent);
    }

    @Override
    protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if (image != null) {
        image.setState(getDrawableState());

        final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK;
        final int height = image.getIntrinsicHeight();

        int y = 0;

        switch (verticalGravity) {
        case Gravity.BOTTOM:
        y = getHeight() - height;
        break;
        case Gravity.CENTER_VERTICAL:
        y = (getHeight() - height) / 2;
        break;
        }

        int buttonWidth = image.getIntrinsicWidth();
        int buttonLeft = (getWidth() - buttonWidth) / 3;
        image.setBounds(buttonLeft, y, buttonLeft + buttonWidth, y + height);
        image.draw(canvas);
    }
    }

But the icon which is being displayed is big , i need to decrease the size of that icon. I have tried with setHeight but its not working.

    RadioImageButton.setCompoundDrawablesWithIntrinsicBounds(icon, null,null, null)
this is used to set the icon
Goofy
  • 6,098
  • 17
  • 90
  • 156
  • [Here's](http://stackoverflow.com/questions/30437223/how-to-decrease-radio-button-drawable-size/35550901#35550901) the actual solution :-) – Sarasranglt Feb 22 '16 at 10:26

1 Answers1

1

ok i fixed it by myself , here is the answer

private Drawable resize(Drawable image) {
    Bitmap b = ((BitmapDrawable)image).getBitmap();
    Bitmap bitmapResized = Bitmap.createScaledBitmap(b, 50, 50, true);// filter attribute set to true
    return new BitmapDrawable(bitmapResized);
}

The code above is to modify the size of drawable, and make sure the filter attribute is set to true so that the icons does not look blur.

Hope it helps some one in future. Happy Coding :)

Goofy
  • 6,098
  • 17
  • 90
  • 156