My font size is 12dp.
I'm setting the font using TextPaint, since I'm using a span. The problem is the parameter that TextPaint accepts is in float. I'm wondering how can I convert 12 dp to float?
From android.content.res.Resources.getDimension(int id):
float twelveDp = TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, 12,
mContext.getResources().getDisplayMetrics() );
Try this:
public static float dipToPixels(Context context, float dipValue){
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dipValue, metrics);
}
You can try following:
// Convert the sp to pixels
final float scale = getResources().getDisplayMetrics().scaledDensity;
int mTextSizeP = (int) getResources().getDimensionPixelSize(R.dimen.text_size) / scale );
I have already have text_size defined in res/values/dimens.xml :
<resources>
<dimen name="text_size">12sp</dimen>
</resources>
Related
I have a custom action bar which i want a 80ish! height for it.
so i set my layouts height as 80dp with this code:
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
xdpi = displayMetrics.xdpi;
x = Math.round(80 * (xdpi / DisplayMetrics.DENSITY_DEFAULT))
... new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, x);
but its HUGE in xxxhdpi devices.
when i remove the conversion and use 80pixels value directly, it seems ok,
When to use converted dp and when to use direct pixel?
Edit:
the problem was somewhere else, i stored the "80dp" value in xml and retrieve it with "context.getResources().getDimension()", and it seems it converts the dimension to pixel internally and i was actually converting the converted value! I wonder if the same thing happens when using "sp" for fonts....
You're doing the right thing it's just your conversion is wrong, it should be something like this:
float scale = context.getResources().getDisplayMetrics().density;
x = Math.round(80 * scale);
... new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, x);
Convert dp to pixel:
public int dpToPx(int dp) {
DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
int px = Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
return px;
}
Convert pixel to dp:
public int pxToDp(int px) {
DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
int dp = Math.round(px / (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
return dp;
}
Pixel to Dp converter
public static float pxToDp(float px) {
float densityDpi = Resources.getSystem().getDisplayMetrics().densityDpi;
return px / (densityDpi / 160f);
}
Dp to Pixel converter
public static int dpToPx(int dp) {
float density = Resources.getSystem().getDisplayMetrics().density;
return Math.round(dp * density);
}
In my Android application I have pixels(69px) and I need to convert this pixels into dip(Density Independent Pixels).
Any suggestions?
Just divide your value in pixels by DisplayMetrics.density.
Hope this will be helpful
Resources r = getResources();
float dp = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 69, r.getDisplayMetrics());
You can try this:
public int convertDiptoPx(int pixel){
float scale = getResources().getDisplayMetrics().density;
int dips=(int) ((pixel * scale) + 0.5f);
logMessage("Px=" +pixel+" DipValue="+dips );
return dips;
}
Editted:
public int convertPxtoDip(int pixel){
float scale = getResources().getDisplayMetrics().density;
int dips=(int) ((pixel / scale) + 0.5f);
return dips;
}
I had written method to get the pixels from dip but it is not working. It give me runtime error.
Actually I was running this method in separate class and initialized in my Activity class
Board board = new Board(this);
board.execute(URL);
This code runs asynchronously. Please help me.
public float getpixels(int dp){
//Resources r = boardContext.getResources();
//float px = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpis, r.getDisplayMetrics());
final float scale = this.boardContext.getResources().getDisplayMetrics().density;
int px = (int) (dp * scale + 0.5f);
return px;
}
Try this:
Java
public static float dipToPixels(Context context, float dipValue) {
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dipValue, metrics);
}
Kotlin
fun Context.dipToPixels(dipValue: Float) =
TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dipValue, resources.displayMetrics)
You can add the dp value in dimen.xml and use
int pixels = getResources().getDimensionPixelSize(R.dimen.idDimension);
It's easier...
The formula is: px = dp * (dpi / 160), for having on a 160 dpi screen. See Convert dp units to pixel units for more information.
You could try:
public static int convertDipToPixels(float dips) {
return (int) (dips * appContext.getResources().getDisplayMetrics().density + 0.5f);
}
Hope this helps...
Try this for without passing context:
public static float dipToPixels(float dipValue) {
return TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
dipValue,
Resources.getSystem().getDisplayMetrics()
);
}
Is it possible to specify the pixel unit in code.
What I mean is, say I have a layout and I want the size to be 20dp, then is there any way to do so without writing in a layout xml
In a view:
DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
float dp = 20f;
float fpixels = metrics.density * dp;
int pixels = (int) (fpixels + 0.5f);
In an Activity, of course, you leave off the getContext().
To convert from scaled pixels (sp) to pixels, just use metrics.scaledDensity instead of metrics.density.
EDIT: As #Santosh's answer points out, you can do the same thing using the utility class TypedValue:
DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
float dp = 20f;
float fpixels = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, metrics);
int pixels = Math.round(fpixels);
For sp, substitute TypedValue.COMPLEX_UNIT_SP for TypedValue.COMPLEX_UNIT_DIP.
Internally, applyDimension() does exactly the same calculation as my code above. Which version to use is a matter of your coding style.
You can use
float pixels = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, getResources().getDisplayMetrics());
now, the value of pixels is equivalent to 20dp
The TypedValue contains other similar methods that help in conversion
Does setWidth(int pixels) use device independent pixel or physical pixel as unit?
For example, does setWidth(100) set the a view's width to 100 dips or 100 pxs?
Thanks.
It uses pixels, but I'm sure you're wondering how to use dips instead. The answer is in TypedValue.applyDimension(). Here's an example of how to convert dips to px in code:
// Converts 14 dip into its equivalent px
Resources r = getResources();
int px = Math.round(TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 14,r.getDisplayMetrics()));
The correct way to obtain a constant number of DIPs in code is to create a resources XML file containing dp values a bit like:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="image_width">100dp</dimen>
<dimen name="image_height">75dp</dimen>
</resources>
Then refer to the resource in your code like so:
float width = getResources().getDimension(R.dimen.image_width));
float height = getResources().getDimension(R.dimen.image_height));
The float you have returned will be scaled accordingly for the pixel density of the device and so you don't need to keep replicating a conversion method throughout your application.
Method setWidth(100), set 100 px as width(not in dp).So you may face width varying problems on different android phones.So use measurement in dp instead of pixels.Use the below code to get measurement in dp of sample width=300px and height=400px.
int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 300, getResources().getDisplayMetrics());
int Height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 400, getResources().getDisplayMetrics());
float dps = 100;
float pxs = dps * getResources().getDisplayMetrics().density;
Source (#Romain Guy)
Based on above answers which works fine to me, i generate some helper methods, just add them in your utils to use them in whole project.
// value in DP
public static int getValueInDP(Context context, int value){
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, context.getResources().getDisplayMetrics());
}
public static float getValueInDP(Context context, float value){
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, context.getResources().getDisplayMetrics());
}
// value in PX
public static int getValueInPixel(Context context, int value){
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, value, context.getResources().getDisplayMetrics());
}
public static float getValueInPixel(Context context, float value){
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, value, context.getResources().getDisplayMetrics());
}
It uses pixels. here's a Kotlin extension function to convert pixels to dp
fun Context.pxToDp(value: Float):Int{
val r: Resources = resources
return TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, value, r.displayMetrics
).roundToInt()
}
Pixels of course, the method is asking for pixels as parameter.