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

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(...)

Related

how to calculate the actual text size display?

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;

Android textSize unit to fit text in parent with exact size in dp

I have a 32dp * 32dp TextView and a single "A" character in center with size of 10pt. In my device "A" is exactly fit to bounds (from top and bottom and looks good) but in other devices it is smaller or overflows the bounds.
Does dp, ds, px or other units help? (please remind I am not searching for this automatic resizeable font where I have exact height of 32dp)
Use sp as the unit of measurement to set text size to support different densities across devices.
Also, read this for a good explanation of dp/sp.

Show 2 square pictures above eachother, taking 50% of the width of the screen

I am a webdeveloper, and I am very lost in all the Android screen settings for aligning content. I've seen weight, gravity, scaletypes of the image etc. etc.)
I hope I can make my intentions clear, and that it's possible to fullfill them.
In this picture of a phone screen, I have 2 square pictures (the purple and the green one).
What I want to accomplish is that the width of the pictures always take up 50% of the screen width, and that it is centered (so the margin left and margin right is 25%).
So let's say you have a 600*800 screen, the width should be 300 pixels and the margin left and right would be 150 pixels (or better 25%). On a 768*1024 screen the width should be 384 pixels.
The height should follow that size (I've tried a lot of code, and the best result was that the width was sort of a pertentage, with 3 relative layouts in a lineair layout, but then only the width was correct, but the height wasn't)
Depending on your screen size it could be that the height of the two images together would be higher than the screen and a scrollbar would be neccessary)
Wrap them in PercentRelativeLayout and fix their width 50%. set their height equal to their width in Java when the view gets populated

Android: Is line length in PX or DIP?

I have a custom view.
When I draw a line from (x,y) to (x+10,y), is the distance in DIP or PX?
As documentation says,
At runtime, the system transparently handles any scaling of the dp
units, as necessary, based on the actual density of the screen in use.
Thus, all units are in dp.
If you are using one of the Canvas.drawLine methods it is in PX
I believe it's in pixels. If you want to know how to convert from DP to pixels as in if you want your custom view to work with DPs just like all other Views.
Converting pixels to dp

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.

Categories

Resources