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"
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:
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.
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.