Identify dp sizes in different screens - android

if a Custom View of size 500w*600h in dp , remain same as px in (1152w*720h)px screens of android , then what will be view's size in dp and px on screen of (480w*600h)px screens. And how to calculate for different size of View.

The dp / px ratio is based on the density of the screen of the device.
I would encourage you to read the Android docs on the subject.
http://developer.android.com/guide/practices/screens_support.html
Each classification of screen density has a specific px multiplier associated with it i.e. mdpi = px * 1 and hdpi = px * 1.5
Here is a nice little calculator to help you make sense of it:
http://labs.rampinteractive.co.uk/android_dp_px_calculator/

Assuming that you specified your view sizes in dp(same as dip), you can use get an instance of DisplayMetrics to convert the dp to actual pixels for your current device.
A handy function you can add to a utility class for doing conversions:
private static float dipToPixels(Context context, int dip)
{
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, metrics);
}

Related

Relation between dp and android different screen size

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:
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.
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;
}

Understanding text size on different resolution image in canvas android

When I tried same text size on top different resolution bitmaps on a canvas. Same text size looked small on a bigger resolution and bigger on a small resolution image. Please Help me understand this.
You need to set proper text size for your Paint, that accounts for density. Paint.setTextSize(float), takes in a float value. You need to ensure that this is not a constant value, but one that accounts for density.
How to get the density? You get that information from DisplayMetrics.scaledDensity or DisplayMetrics.density. Once you have this value, multiply this with the fontSize and set that value as the text size. Somthing like the below.
Paint.setTextSize(density * 10f);
This way a text with 10f font size will look the same in all devices with varying densities. You can find more information on density and scaledDensity here: http://developer.android.com/reference/android/util/DisplayMetrics.html#scaledDensity
This is way you can make all text uniform size in all device.
canvas.drawText(hourText, getPx(TEXT_SIZE, activity), getPx(TEXT_SIZE / 3, activity), nPaint);
public static int getPx(int dp, Activity activity) {
DisplayMetrics metrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
float logicalDensity = metrics.density;
return (int) Math.ceil(dp * logicalDensity);
}

designing layouts programatically for multiple screen sizes

I would like to know how to create an android UI designs for multiple screen sizes programatically?
Take for example a Button or Layout set for 3.5inch programatically using width and height will look very small in 5inch device
similarly Button or layout created for 5 inch device will get trimmed for 3.5 inch device.
So would like to know how to handle it
Use Density pixels (dp) instead of pixels.
read here android supporting multiple screens
Is use these helper methods to convert between density pixels and real pixels :
/**
* Converts dp unit to equivalent pixels, depending on device density.
*
* #param dp A value in dp (density independent pixels) unit. Which we need to convert into pixels
* #return float value to represent px equivalent to dp depending on device density
*/
public static float dpToPixel(float dp){
Resources resources = MyApplication.getAppContext().getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float px = dp * (metrics.densityDpi / 160f);
return px;
}
/**
* Converts device specific pixels to density independent pixels.
*
* #param px A value in px (pixels) unit. Which we need to convert into db
* #return float value to represent dp equivalent to px value
*/
public static float convertPixelsToDp(float px){
Resources resources = MyApplication.getAppContext().getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float dp = px / (metrics.densityDpi / 160f);
return dp;
}
(MyApplication is a class that extends the Application class)
public class MyApplication extends Application
android supports the variety of screen sizes, and resolutions in a very good fashion.
you can provide different layouts to different screen sizes by using the smallest width qualifier.
you can also provide different versions of each image/icon for each screen density.
check Deacoy's link for further information.
Always remember the best way to start diving into a new technology is the official documentation.

Image Height Appear Different on different density

I'm new in designing the layouts. I am getting the bitmap from the server and I'm setting the imageview height such that it look good on every density either ldpi, mdpi, hdpi, xhdpi.
stripImageView.getLayoutParams().height = (int) ((Util.screenWidth(this) * strip.getHeight() / strip.getWidth()) * getResources().getDisplayMetrics().density);
But This code not working for me. Some device images looking very small and Some device it look fine
Try just setting the dimensions in your layout using dp instead of px. Dp (or dip) stands for density independent pixels and will automatically scale based on density.
EDIT: another issue I see is that you're referencing both the screen width and height so your screen aspect ratio would also affect that calculation.
convert them to DIP:
stripImageView.getLayoutParams(). height =
(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, <HEIGHT>,
getResources().getDisplayMetrics());
where HEIGHT is the height in px you want. You could base it off the strip height or something.

Changing EditText's text size from pixel to scaled pixels

I'm providing an option for text sizes and one of them is for the EditText boxes. I've determined the original text size using EditText#getTextSize(). The result is given in pixels. Once the user selects the text size, I use edittext.setTextSize(TypedValue.COMPLEX_UNIT_SP, chosen) to apply the text size.
Problem is different screen sizes give me wildly different text sizes, e.g. on an ICS phone, EditText.getTextSize() returns 36, which is absolutely massive and definitely not the default text size used throughout the system.
What am I doing wrong?
You are probably seeing different values due to different screen densities and the fact that the value you get from getTextSize() is in pixels(px). Density Independent Pixels(dp) and Scaled Pixels(sp) are adjusted to be independent of the screen density and they are what you should be using. To get dp or sp from px you can use a simple conversion like this:
public static float pxToDp( float px, Context context ) {
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float dp = px / ( metrics.densityDpi / 160f );
return dp;
}
or this (which I took from this post):
public static float pixelsToSp(Context context, Float px) {
float scaledDensity = context.getResources().getDisplayMetrics().scaledDensity;
return px/scaledDensity;
}

Categories

Resources