0

I'm trying to draw a circle which will have the same diameter of 2.5 inches (or any other value) in every android device, regardless of the screen size. The diameter size must be same in all the devices and below is what I have tried.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:background="@drawable/light"
    tools:context=".MainActivity">

    <ImageView
        android:layout_width="3in"
        android:layout_height="3in"
        android:src="@drawable/oval_shape"
        android:id="@+id/imageView"
        android:scaleType="matrix"/>

</RelativeLayout>

oval_shape.xml

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

    <solid
        android:color="#666666"/>

    <size
        android:width="2.5in"
        android:height="2.5in"/>
</shape>

The issue with this code is when I try it with 4.5 in screen (real phone), the diameter is 2.5inches as expected but when tested in 4 inch screen the diameter is less than 2.5 inches, it has a slight difference. I tried using millimeters (mm) as well but it didn't work well either.

So how can I achieve the requirement of same size circle in every phone?

Terance Wijesuriya
  • 1,928
  • 9
  • 31
  • 61

1 Answers1

-1

//You are using fix static size in oval_shape.xml for all screens. thats why it is varying for different screens. you can use this in dimens. // use this code in oval_shape.xml

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

    <solid
        android:color="#666666"/>

    <size
        android:width="@dimen/inch_size"
        android:height="@dimen/inch_size"/>
</shape>

// and You can put dimens.xml in

1) values-xlarge

2) values-small

3) values-normal

4) values-large

And give different sizes in dimens.xml within corresponding folders according to densities.

// like 2.5in in normal , 1.5x of normal value in large (means 3.75in), 2x of normal value in xlarge(means 5in), .75x of normal in small (means 1.875in aprox)

//hope it may be helpfull for you.