0

how to make images fit in all different screens in android ? I need images with 440x300dp and I created mdpi etc folders to place images,check out my screenshots and help me to solve this one

I need images to fill match parent of width and height should be 300dp but these mdpi,hdpi ..etc not having it, I'm confused

enter image description here enter image description here

enter image description here

Thank you..

1 Answers1

1

if you need ImageView with fixed size then just use fixed size

android:layout_width="440dp"
android:layout_height="300dp"

DON'T use layout_weight/weightSum params at all, with it you won't ever achieve what you want.

and after that use android:scaleType param to fulfil your View. check out THIS visual tutorial for picking proper type

laso all your drawables should have proper ratio, e.g. mdpi drawable may have 440px x 300px, hdpi x1.5, so 660px x 450px, xhdpi x2 and so on. some nice tutorial about density buckets in HERE

snachmsm
  • 17,866
  • 3
  • 32
  • 74
  • so now my images will be fixed sizes according to your code then how it will be in same size in different screens? – MadhanMohan Nov 30 '20 at 09:26
  • well, "fixed size" isn't same as "being in same size"? check out how density buckets works, especially my last link, and also what exacly is [dp unit](https://stackoverflow.com/questions/2025282/what-is-the-difference-between-px-dip-dp-and-sp). in short: fixed size in `dp` will have same size on all screens - on low-res `100dp` will be `100px`, on high-res will be `300px` (higher density, more pixels per inch) – snachmsm Nov 30 '20 at 09:34
  • one more doubt now i've created mdpi,xdpi etc folders to hold images for different screen can i leave it like that ? for should i put one drawable for all different screens? now I just fixed my width and height like your code, I didn't delete that folders – MadhanMohan Nov 30 '20 at 09:55
  • make `drawable-` folders with `mdpi`, `hdpi1, `xhdpi` etc. suffixes, then put inside every folder properly sized image (so in fact there will be few images, one per density). check out factors per density bucket (linked in answer) - `mdpi` have factor x1, so you are creating image e.g. 100px x 100px, next `hdpi` folder have x1.5 factor, so image in this folder should have 150px x 150px, `xhdpi` have factor x2, `xxhdpi` x3, `xxxhdpi` x4 – snachmsm Nov 30 '20 at 10:00
  • Thank you bro already I created folders that you can see in my ss ..is that right? now i need to place images according to it , – MadhanMohan Nov 30 '20 at 10:29
  • exacly, all folders looks like properly named, just put properly sized images in every bucket and you are good to go (set for `ImageView`, add `scaleType`) – snachmsm Nov 30 '20 at 10:32
  • Bro I forgot to tell you one thing now I'm going to retrieve images from firebase, now all folders mdpi,xdpi will be empty because all images will be in firebase, shall I leave that folders ? or Should I create separate json column for mdpi,hdpi etc images? – MadhanMohan Nov 30 '20 at 11:16
  • best option for app would be to get links to different images (resized) and app should pick proper one - if `mdpi` then gets low-res image, if `xxhdpi` then picks some high-res. – snachmsm Nov 30 '20 at 11:21
  • you can also shorten a bit this mechanism, e.g. two links to hi-res and low-res images, devices below `xhdpi` pick first one, all other second one. yet another approach is one link to one hi-res image and scale it down after downloading to fit device screen properly - but this is power-and-time-cost (conversion/resize), not so efficient, and still very old device may have problems with bigger bitmaps e.g. 1500px x 1500px, at the same time another flagship device may have UHD display and 1500x1500 resolution isn't scary/too much for shure – snachmsm Nov 30 '20 at 11:22
  • so now what i've to do for example i have demo image with 440x300dp – MadhanMohan Nov 30 '20 at 11:28
  • `dp` or `px`? put link to 880px x 600px as `lowResUrl` (`xhdpi`, factor x2) and 1760px x 1200px as `hiResUrl` (`xxxhdpi`, factor x4). use `lowResUrl` on `xhdpi` and lower only, for `xxhdpi` and higher (xxx, but also maybe some next in future) use `hiResUrl`. `Bitmap`s below 1000px in any dimensions can by handled by every device, even oldest – snachmsm Nov 30 '20 at 11:32
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/225315/discussion-between-madhanmohan-and-snachmsm). – MadhanMohan Nov 30 '20 at 11:55
  • sadly I'm not your personal trainer, I don't have time to explain to you all the things related to images/densities/possibilities etc. I've already answered your question, my job is done here. If you have any other question feel free to make new topic/question – snachmsm Nov 30 '20 at 11:58