I want to make folders of dimens according to screen size and not according to density so what will be the dp values for each screen size. I want to adjust my DP value according to screen size only and not according to density so is there any formula for calculating dp for different screen size . As there is a relation between density , pixel and dp . But i want it for different screen size . It is like for normal-xhdpi screen and for normal-xxhdpi screen the size of the button should be same as independent of density as name suggest of "DP"
Asked
Active
Viewed 2,100 times
1
-
1Possible duplicate of [What is the difference between "px", "dp", "dip" and "sp" on Android?](http://stackoverflow.com/questions/2025282/what-is-the-difference-between-px-dp-dip-and-sp-on-android) – harshithdwivedi Jan 19 '17 at 07:05
-
Use this library.https://github.com/Shekhar14o3/Library-On-Different-Screen-Supports – Aman Shekhar Jan 19 '17 at 07:06
-
For normal-xxhdpi my button is coming smaller as compare to normal--xhdpi . But i want that for every normal phone size it should be same so what dp relation should have to be use for different screen size like small,large,normal and xlarge – ritwik Jan 19 '17 at 07:21
2 Answers
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:
- Convert the Normal Dimensions for other Screen Sizes.
- Copy your Normal Layout xml files in to other Folders.
- Change the suffix of the dimensions used according to the folder that you are in
- 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).
- 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
If you are looking in Java code
/// convert dp to pixels
public static int dp2px(Context context, float dp) {
return Math.round(dp * context.getResources().getDisplayMetrics().density);
}
I hope this helps.

Jayanth
- 5,954
- 3
- 21
- 38
-
One solution which comes in my mind after reading your solution is that (Please correct me if i am wrong) :- when screen size is small the divide the px with 3. if screen size is small then divide px with 2 and for other screen size keep pixel and dp as it is , Independent of the density of the screen – ritwik Jan 19 '17 at 07:38
-
read full answer, i have given step by step procedure and examples too – Jayanth Jan 19 '17 at 07:43
-
For xhdpi , it shouldn't be like divide by 2 . Reason being as as it depends upon density and value of density for xhdpi is 2 so for mdpi lets take an example if value is 30 therefore converting into pixel , it would be 30 px as 1 dp = 1 px for mdpi and then dividing by 2 i.e. density so it will be out to 15dp for xhdpi , in ypu r case it will be 45 dp what will be right – ritwik Jan 19 '17 at 07:55
-
-
Is it necessary to make dimens as normal-xhdpi, normal-xxhdpi or is it okay to make only for normal dimens file thats it i want to make for different screen size. – ritwik Jan 19 '17 at 08:08
0
public static float dpToPx(Resources rs,float dp){
DisplayMetrics metrics = rs.getDisplayMetrics();
float px = dp * (metrics.densityDpi/160f);
return px;
}
public static float pxToDp(Resources rs,float px){
DisplayMetrics metrics = rs.getDisplayMetrics();
float dp = px / (metrics.densityDpi / 160f);
return dp;
}

Junaid Hafeez
- 1,618
- 1
- 16
- 25
-
This formulae takes density into consideration so like for norma-xxhdpi screen the size of screen would be smaller as compare to normal-xhdpi screen . I want it to be same for all normal screen phone. What will be the formula for converting px into dp relative to screen size only – ritwik Jan 19 '17 at 07:28