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
Asked
Active
Viewed 967 times
2
-
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 Answers
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
-
How can i get button previous attributes and appear them again in this solution? Like background,color and etc.. – Dec 06 '14 at 12:34
-
I think you can just `setCornerRadius` and don't use other methods in my example and then you don't need previous attributes ;) – Mohammad Rahchamani Dec 06 '14 at 12:36
-
I used just setCornerRadius ,but my previous background attribute in xml layout missed with this code – Dec 06 '14 at 12:37
-
-
if your background was `transparent` it should be ok but if you are setting custom background to your view, use `gdDefault.setBackgroundDrawable(view.getBackgroundDrawable());` – Mohammad Rahchamani Dec 06 '14 at 12:39
-
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