Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I round a button programmatically in Android?

Tags:

android

I have a Button which populates a GridView using a custom adapter's call to getView(). As such, there is no .xml file for the Button.

Is there a way to programmatically round the Button?

like image 780
sdasdadas Avatar asked Jun 24 '26 00:06

sdasdadas


1 Answers

Use a nine patch, here is an example one:

enter image description here

save this in your /res/drawable-mdpi/ directory with the filename btn_round.9.png

then in your java code do this:

mBtn.setBackgroundResource(R.drawable.btn_round);

because it is 9 patch wit will stretch to fit whatever content you are putting inside the button. Search for "android draw9patch" to learn more about how to create the 9 patch files.

like image 130
FoamyGuy Avatar answered Jun 25 '26 13:06

FoamyGuy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!