Elad Benda
Elad Benda

Reputation: 36672

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)

Upvotes: 1

Views: 1135

Answers (1)

Cauê Jannini
Cauê Jannini

Reputation: 551

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;

Upvotes: 2

Related Questions