Text length in pixels

Post your questions and help other users.

Moderator: Martin

Post Reply
User avatar
digitalstone
Posts: 342
Joined: 21 Oct 2017 12:36
Location: The Netherlands

Text length in pixels

Post by digitalstone » 13 Dec 2017 08:34

Is there a way to get the amount of pixels (length and height) based off the text being showed?

I'm creating a global message box.
Phone: LG Nexus 5X (rooted vanilla Android 7.1.2)

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: Text length in pixels

Post by Desmanto » 13 Dec 2017 15:54

Quite to difficult to do, even though I think it is possible.
Maybe the better way is to get the bound x,y of the element id.
What is the purpose of getting the amount of pixel from the message box?
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

User avatar
digitalstone
Posts: 342
Joined: 21 Oct 2017 12:36
Location: The Netherlands

Re: Text length in pixels

Post by digitalstone » 13 Dec 2017 18:18

Desmanto wrote:Quite to difficult to do, even though I think it is possible.
Maybe the better way is to get the bound x,y of the element id.
What is the purpose of getting the amount of pixel from the message box?
What do you mean with the element id?
I'm building the messagebox based on a widget btw

Getting the amount of pixels is to determine the dimensions of the message box.
I like things to be futureproof while not taking up more space than needed.
But i guess i could make it just 1 size for every possible message ever if it's not possible.
Phone: LG Nexus 5X (rooted vanilla Android 7.1.2)

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: Text length in pixels

Post by Desmanto » 14 Dec 2017 05:19

Oh, I thought you wanna retrieve the size in certain UI Element. Measuring the text pixel seems quite difficult unless you use monospace font. Non-monospace font has kerning, which makes the length varies depend on the content. Counting based on the number of char will give only rough estimation.

So maybe you can use rough estimation based on the char count.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

User avatar
digitalstone
Posts: 342
Joined: 21 Oct 2017 12:36
Location: The Netherlands

Re: Text length in pixels

Post by digitalstone » 14 Dec 2017 17:42

Alright thanks.
Already thought it was more difficult than worthy of implementation.

I'll just see what i can do with a monospace font, if i can setup any that is. But i'll check it out.
Phone: LG Nexus 5X (rooted vanilla Android 7.1.2)

User avatar
digitalstone
Posts: 342
Joined: 21 Oct 2017 12:36
Location: The Netherlands

Re: Text length in pixels

Post by digitalstone » 09 Jan 2018 15:40

Btw, Monospace is the fontname.
It's right inside the font options of the widget.
Phone: LG Nexus 5X (rooted vanilla Android 7.1.2)

Post Reply