2

How can i add radius to imageButton object programmatically?
I checked exist topic on this site but i could not found my answer?
I want to add radius without xml file

  • Do you mean to add a **rounded corner**? – Phantômaxx Dec 06 '14 at 12:27
  • http://stackoverflow.com/questions/6054562/how-to-make-the-corners-of-a-button-round check this . This may help you :) – Nitin Dec 06 '14 at 12:31
  • Thanks, but i want to add it programmatically without xml –  Dec 06 '14 at 12:32
  • There are several **easy** ways to do that. One involves using a ShapeDrawable (xml). Another one involves using a 9 patch (png). Then there are the **hard** ways to do that... programmatically. See Roman Guy's post: http://www.curious-creature.com/2012/12/11/android-recipe-1-image-with-rounded-corners/comment-page-1/ – Phantômaxx Dec 06 '14 at 12:57

2 Answers2

0

I think you should use GradientDrawable.

here is an example :

GradientDrawable gdDefault = new GradientDrawable();
ColorDrawable cd = new ColorDrawable(0xFFFF6666);
gdDefault.setColor(bgColor);
gdDefault.setCornerRadius(cornerRadius);
gdDefault.setStroke(strokeWidth, strokeColor);
yourView.setBackgroundDrawable(cd);
Mohammad Rahchamani
  • 5,002
  • 1
  • 26
  • 36
0

You can do it using GradientDrawable.

 GradientDrawable shape =  new GradientDrawable();
 shape.setCornerRadius(8);

 ImageButton ivButton = (ImageButton) findViewById( R.id.ivbtn );
 ivButton.setBackgroundDrawable(shape);

Enjoy!!!

Ganesh Katikar
  • 2,620
  • 1
  • 26
  • 28