3

I am trying to define a circular background to be used as a background for buttons.

<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:insetLeft="@dimen/abc_button_inset_horizontal_material"
    android:insetTop="@dimen/abc_button_inset_vertical_material"
    android:insetRight="@dimen/abc_button_inset_horizontal_material"
    android:insetBottom="@dimen/abc_button_inset_vertical_material">
    <shape android:shape="oval">
    <solid android:color="@color/material_orange_200" />
    <padding android:left="@dimen/abc_button_padding_horizontal_material"
             android:top="@dimen/abc_button_padding_vertical_material"
             android:right="@dimen/abc_button_padding_horizontal_material"
             android:bottom="@dimen/abc_button_padding_vertical_material" />
</shape>
</inset>    

The problem is that if the button is not completely square the shape get stretched into oval (elipsoid) which is undesirable. I could use <size> tag and set width and height to the same arbitrary value. But this would defeat the purpose to use this background as universal background for different size buttons.

Is there a way to force (in xml) oval shape to remain a circle?

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
  • you don't need to for oval use ring check [here](http://stackoverflow.com/questions/22105775/imageview-in-circular-through-xml) – Prashant Sharma Sep 19 '16 at 07:12
  • Hey, you have to make your button with same width and height otherwise your drawable will stretch to oval as you have said above. If you dont want to make this oval, in that case your drawable size will increase and will cover lot of space to make it perfect circle. Can you please let me know that what you are showing on button? – Rahul Sharma Sep 19 '16 at 07:17
  • maybe this [SO question-and-answers](http://stackoverflow.com/questions/1400782/android-scale-a-drawable-or-background-image/9362168) will help – Bö macht Blau Sep 19 '16 at 08:12
  • You have to ensure the view has same height and width. That's it. End of story. By the way `@dimen/abc_button_inset_horizontal` is 4dp and `...vertical` is 6dp. Might help you with your calculations. – Eugen Pechanec Sep 26 '16 at 20:08

1 Answers1

0

create a drawable file like this

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">

    <solid android:color="#22C2FE" />
    <stroke
        android:width="0dp"
        android:color="#2B8DC6"></stroke>
</shape>

and use the same height and width on which view are using the drawable.

Rahul Khurana
  • 8,577
  • 7
  • 33
  • 60
  • 2
    No. Not good. What if I use ImageButton where the image is not perfect square. Actully, your proposition is just delegating fixed height and width to View. –  Sep 19 '16 at 07:33
  • @user6694745 if your view is not perfect square then give it a try using some padding – Rahul Khurana Sep 19 '16 at 08:12