1

Hello I am trying to set the image in the ImageView but after setting the image it is appearing different in size on different android device.

Sony Z Experia 4.3

enter image description here

Samsung Y Dous 2.3 where it covered the entire image to the screen width

enter image description here

Here is layout

<ImageView
    android:id="@+id/logo"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:padding="5dip"
    android:scaleType="fitStart" />

How can I make such that it appear same as Sony Z Xperia. Any idea ?

Thanks in advance

N Sharma
  • 33,489
  • 95
  • 256
  • 444

4 Answers4

3

In android you have the option hdpi, mdpi, xdpi,etc..

folders for that , you have to create different images according your device resolution and put your images at there after confirming your device resolution and density category.

for the more reference why it'll happen you can see here

here i explain some chart may be helpful to you.

Low density Small screens QVGA 240x320 (120dpi):

layout-small-ldpi (240x320)  
layout-small-land-ldpi (320x240)

Low density Normal screens WVGA400 240x400 (x432) (120dpi):

layout-ldpi  (240 x 400 )
layout-land-ldpi  (400 x 240 )

Medium density Normal screens HVGA 320x480 (160dpi):

layout-mdpi (320 x 480 )
layout-land-mdpi (480 x 320 )

Medium density Large screens HVGA 320x480 (160dpi):

layout-large-mdpi (320 x 480 )
layout-large-land-mdpi (480 x 320)

Galaxy Tab ( 240 dpi ):

layout-large  (600 x 1024) 
layout-large-land  (1024 x 600)

High density Normal screens WVGA800 480x800 (x854) (240 dpi):

layout-hdpi (480 x 800)
layout-land-hdpi (800 x 480)

Xoom (medium density large but 1280x800 res) (160 dpi):

layout-xlarge (800 x 1280)
layout-xlarge-land (1280 x 800)
GovindRathod
  • 867
  • 1
  • 8
  • 22
  • I don't have those bitmaps in res folder, I am getting this bitmap from URL – N Sharma May 21 '14 at 05:17
  • then you have a single choice you have to fix your image view according "dp" mins fix the height and width of your image view. – GovindRathod May 21 '14 at 05:20
  • If I fix this value in "dp" then it will appear same in all devices. I want to make such that it looks good in all devices and scale according to the device resolution – N Sharma May 21 '14 at 05:26
  • No my friend there is result in all device are different if you take the "dp", dp considers the different as resolution vise. that's will happen(mins same size in all device) if you take pixel. so don't worry try "dp" and be happy. – GovindRathod May 21 '14 at 05:31
  • just look here for the difference for the "dp" over "px" http://stackoverflow.com/questions/2025282/difference-between-px-dp-dip-and-sp-in-android – GovindRathod May 21 '14 at 05:34
  • Do you mean to use this attribute `android:minWidth="" android:minHeight="" ? – N Sharma May 21 '14 at 05:34
  • No my friend you have to use `android:layout_width="100dp" android:layout_height="100dp"` – GovindRathod May 21 '14 at 05:35
  • OK If I set 100dp width then what would be width of `ImageView` in different density ? Any calculation to calculate it ? – N Sharma May 21 '14 at 05:37
  • yes here id the calculator for the convert the "dp" to "px" you can calculate it according your dpi of your device http://labs.rampinteractive.co.uk/android_dp_px_calculator/ – GovindRathod May 21 '14 at 05:41
  • so when I give the value in "dp" then does android convert them in px at runtime automatically ? – N Sharma May 21 '14 at 05:42
  • yes, that's want to i trying to say you. whether you give the same "dp" to your component then, as different-different device it calculate different pixel according on device's density. – GovindRathod May 21 '14 at 05:45
1

There are two ways you need to go about this.

  1. As mentioned is you need to have images of various sizes to account for different Android devices having different display densities. There are several densities outlined here: http://developer.android.com/guide/practices/screens_support.html
  2. Now that you have images of different sizes, you may also want to go an extra step and scale them perfectly to a specific width and height. Try the following layout (note that dip is the same as dp)

<ImageView
    android:id="@+id/logo"
    android:layout_width="200dp"
    android:layout_height="75dp"
    android:padding="5dip"
    android:scaleType="fitStart" />

Of course you could just use a single image (ignore #1) and use #2 to stretch it to the appropriate size, but then you might be scaling an image 10x bigger, and it will look very stretched.

Martin Konecny
  • 57,827
  • 19
  • 139
  • 159
  • I don't have those bitmaps in `res` folder, I am getting this bitmap from URL – N Sharma May 21 '14 at 05:16
  • @Williams, if you have control over the bitmap on the server, then you can upload multiple versions of it, and then download the appropriate one depending on the current device screen density. If you don't have control, then you will need to scale it as per method #2. – Martin Konecny May 21 '14 at 13:22
0

you need different images for different densities devices. Sony Z has higher density thats why its look smaller.xxdpi,xdpi,hdpi, mdpi and ldpi folders are used for this thing. Try this tool to convert images for different devices. I know this tool is for icon only. its generate icons for different densities. but you can give it a try.

Waqar Ahmed
  • 5,005
  • 2
  • 23
  • 45
0

Try this code and provide height and width in percentage of your screen

Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();

display.getMetrics(metrics);

int widthScreen = metrics.widthPixels;
int heightScreen = metrics.heightPixels;

ImageView logo=findviewById(R.id.logo);

logo.getLayoutParams().height = (int) (heightScreen * 0.10);//it set the height of image 10% of your screen
logo.getLayoutParams().width = (int) (widthScreen * 0.10);
ComFreek
  • 29,044
  • 18
  • 104
  • 156
Pawan asati
  • 292
  • 2
  • 13