how to calculate the actual text size display? - android

I have a textView in another view.
I want to calculate in runtime the actual display size of that text
and of its container.
If the text is too big to fit in the view I have few shorted text content alternatives.
I have read the textView doc, but it only shows [setTextSize][1]
how can i get the text size (in dp I guess ?) according to the actual screen.
(orientation, screen size, font size and so on)

TextView actually has a getTextSize() method, which returns the font size in pixels for you. You can check here the documentation. Of course this pixel value will vary accordingly to the screen density of the device. If you want to convert that value for dp or sp on runtime, you can do the following:
Pixels for SP:
float sp = px / getResources().getDisplayMetrics().scaledDensity;
Pixels for DP:
float dp = px / getResources().getDisplayMetrics().densityDpi;

Related

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);
}

what is the difference between scaledDensity and density in Android`s DisplayMetrics class?

TextView compute text size value with scaledDensity instead of density. so what`s the difference between these two values?
DisplayMetrics#scaledDensity
A scaling factor for fonts displayed on the display. This is the same as density, except that it may be adjusted in smaller increments at runtime based on a user preference for the font size.
DisplayMetrics#Density
The logical density of the display. This is a scaling factor for the Density Independent Pixel unit, where one DIP is one pixel on an approximately 160 dpi screen.
sp
Scale-independent Pixels – this is like the dp unit, but it is also scaled by the user’s font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and user’s preference.
dp
A virtual pixel unit that you should use when defining UI layout, to express layout dimensions or position in a density-independent way.
The density-independent pixel is equivalent to one physical pixel on a 160 dpi screen, which is the baseline density assumed by the system for a “medium” density screen.
you can find nice explanation here Difference of px, dp, dip and sp in Android and Android units

How get a text size in SP (Scaled Pixel) directly from a TextView?

I`ll wish change the text size in run-time based in the old size.
How get a text size in SP (Scaled Pixel) directly from a TextView?
Something like:
textMove.setTextSize(textMove.getTextSPSize() + sizeFontScale);
What about:
float px = editext.getTextSize();
float sp = px / getResources().getDisplayMetrics().scaledDensity;
So... setTextSize(int) actually assumes Scaled Pixels. getTextSize() returns the actual pixels. If you want to increase by an amount of actual pixels then you can call getTextSize(), add whatever, then call setTextSize(TypedValue.COMPLEX_UNIT_PX, newValue). I guess I'm kind of wondering if you really want to do everything in Scaled Pixels.

change image size wih jquery

I have a image under div id myimage. Now I want to resize it by using change. I want from select box if selected 700X7000 then image's size will be height 700px and width 700px. With out reloading the page.
Can any one help me how can I do this?
Set new new LayoutParams(200, 200) everytime.
screenDensity = getResources().getDisplayMetrics().density;
dimInDP = 200 * (screenDensity/(float)160)
imageTextView.setLayoutParams(new LinearLayout.LayoutParams(dimInDP ,dimInDP ));
EDIT : The Android system takes 160dpi as the reference density for density independent pixel calculation. For example, in your case you need dimension as 200dp. The system takes this as the numbersof pixels you want on a device of screen density 160dpi. So at runtime it scales the width relative to the reference density. Assume you run ur app on a screen density 120. The view width will be scaled to 150 physical pixels.

Android: Converting an image's pixel height to DP?

I got an image I created in Photoshop that is 300 X 300 pixels. I need to create a text label that is the same height. I know I shouldn't set it to 300px, as we should use dp. So how do I convert the 300 pixels to dp, so I know what to set the height as?
I know it's possible to relatively align the text label to the right of the image, but ignore that for now... how can I determine what height in dp to set the text label so it's the same height as the image?
Try TypedValue.applyDimension(...)

Categories

Resources