Handling all screen sizes android
Asked Answered
D

3

9

I'm working on an android App,and it's designed with numbers (margins etc) no relative things so I think I'll find many problems with different screen sizes so I thought of making a function which will keep the data in DIMENSIONS file proportional to user screen size like ( User's screen size X dimen)/(the screen size which the app were designed on), so I want to know if this won't cause any problem on App working etc ..

Thank you =)

Dermis answered 6/1, 2017 at 3:50 Comment(1)
There is no one solid answer to this. It really depends on your layout, the drawables you have, etc. Sometimes just scaling will work. Sometimes it won't (things won't fit when scaling down, images get too stretched, etc). Small margins (things in the low double digits especially in dp) typically scale well. Numbers in the 100s of pixels may not. Its part of your job as an Android engineer to figure out those relative and linear linkages and create a good layout from it. You can't expect to be handed everything, if you were you wouldn't be needed.Ogrady
E
11
//Dimen
implementation 'com.intuit.ssp:ssp-android:1.0.5'
implementation 'com.intuit.sdp:sdp-android:1.0.5'

use this two library, ssp is for text size, and sdp is for margin, padding and layout size

android:layout_width="@dimen/_24sdp"
android:layout_height="@dimen/_24sdp"
Enallage answered 5/10, 2018 at 7:2 Comment(5)
can we use it programaticallyLouie
@ArnoldBrown yes you canAngevin
@Angevin yes I did this. ThanksLouie
@harshil-kakadiya In example they have 2 activities one using ssp and other example in other project using only sdp. I am following your recommendation, but can you explain why?Widget
Use ssp for Text size and for remaining use sdp.Louie
J
6

May be you can try below library which manages all the screen size resolution automatically.

compile 'com.intuit.sdp:sdp-android:1.0.4'

You need to just add the dependency in your build.gradle file and you are done.

You need to specify like:

android:layout_height="@dimen/_10sdp"

Instead of:

android:layout_height="@dimen/10sdp"

Jeramey answered 6/1, 2017 at 6:51 Comment(4)
I tried this library but its just enlarging everythingOosphere
The library seems pretty good, only thing it acts like a framework.. if you want more control then use different layout files... especially for image sizesArchitect
can we use it programaticallyLouie
@ArnoldBrown You can use below code to access dimen pragmatically. int dp = (int) (getResources().getDimension(R.dimen.test) / getResources().getDisplayMetrics().density)Joost
S
3

Create three different Layouts Folder in your res folder for all devices and use the dimensions accordingly.

Generic Layout Folders

res/layout-small
res/layout-normal
res/layout-large
res/layout-xlarge

After you are done with making your Normal/Medium Layouts follow these steps:

  1. Convert the Normal Dimensions for other Screen Sizes.
  2. Copy your Normal Layout xml files in to other Folders.
  3. Change the suffix of the dimensions used according to the folder that you are in
  4. Resize the Image Resources in your drawable folder (Width and Height - Same technique as we used for converting the dimens) and put them in their respective drawable folder (drawable-ldpi, drawable-mdpi, drawable-hdpi, drawable-xdpi and so on).
  5. Then your Layouts should work on every device with correct positioning.

For converting Values

0.75 - ldpi  (small)   //mdpi dimens *0.75
1.0  - mdpi  (normal)  //First create these dimensions
1.5  - hdpi  (large)   //mdpi dimens *1.5
2.0  - xhdpi (xLarge)  //mdpi dimens *2.0

For Example

android:layout_width="66dip" //in normal
android:layout_width="100dip"//in large 66*1.5=100(approx)
android:layout_width="52dip" //in small 66*0.75=52(approx)

Also new Qualifier has been introduced - SmallestWidth - AvailableScreenWidth - AvailableScreenHeight

read more about it here https://developer.android.com/guide/practices/screens_support.html

I hope this helps.

Schlenger answered 6/1, 2017 at 4:5 Comment(2)
thank you so much , I have another problem if you can help : so that user can upload pictures through my app I added three imageviews and made them clickable so after user click on the first it takes him to the gallery the image is uploaded and imageview change color and is no longer clickable (that's what should happen) but when I test it I need to do all of this and click again on imageview and go out of the gallery so I can see my imageview changing color and being non clickable I hope you got the point thank youDermis
i'll surly help but if the above stated problem is solved please accept this answer and for the another question you need to ask separate question and give me the link in comment :)Schlenger

© 2022 - 2024 — McMap. All rights reserved.