Android: How to determine the Length of text (in pixels)? - android

I am building an app and based on the content and screen size I want to dynamically set the font size to fill the entire screen.
Is there a way to determine the width of a line of text (in pixels)? I want to avoid the flicker that trial and error can present the user. Can it be done without first displaying a TextView?

The Paint class has a measureText method for this purpose :)

Related

How Google Keep resizes the text of a note?

I've been developing an application similar to the Google Keep, and was wondering how actually they allocate the text size to a note (algo)?
Do they just allocate size depending upon the Note length?
Or they also check for Card Dimensions i-e card width etc?
Its a bit confusing, because sometimes notes have equal card size dimensions with font-difference, and sometimes notes have both different size dimensions and text size?
So basically, the Question is, What is the algorithm/method to calculate the text size for a note?
you have to use auto-fit TextView for this & add it in RecycleView or ListView(whatever you want) item layout. here is one of stack answer of AutoFit-TextView and source code from github android-autofittextview
apply max text size in TextView then it automatically adjust if there is more text
Do they just allocate size depending upon the Note length
No,
Or they also check for Card Dimensions i-e card width etc
No
just make you TexView size wrapcontent its allow you to add all text in textview like whatsup chat thread.
Edited
you have to manage like below way
*in Google keep text size depend of words. text length is 10 then maximum text size. text length is 30 then medium textsize and more then 50 then small text*
they are using two typeface one is for title and another is for text message

How to set absolute text size on android (TypedValue.COMPLEX_UNIT_MM)

I am writing an app about eye test. It is necessary to set the standard text size. I used the following code but it showed what I did not expect.
Typeface type=Typeface.createFromAsset(getAssets(),"Optotypes.ttf");
textView2.setTypeface(type);
textView2.setTextSize(TypedValue.COMPLEX_UNIT_MM,25);
textView2.setText(randomLetter);
I expected the textview show a 2.5cm letter but it is not the exact length/height still.
This situation appear also on different device.
The next problem is that the size is different between the original font and ttf I added. (the original font didn't show the text with 2.5cm also.
Is my code wrong or anything else i missed ? Thanks guys . it is important to me.
I think you're missing how Android handles text sizes.
In Android, you should specify text size in SP units, so Android can scale it accordingly to the user's font size preferences. Never specify hardcoded pixels or centimeters.
Check this references for documentation on the subject:
http://developer.android.com/guide/topics/resources/more-resources.html#Dimension
http://developer.android.com/reference/android/widget/TextView.html#attr_android:textSize
What is the difference between "px", "dp", "dip" and "sp" on Android?
If you want to set the text size in SP programatically, you can do this
// same as android:textSize="15sp" in XML
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
-- EDIT
Keep in mind that by just setting a certain text size it doesn't mean that every letter will be of that size. Remember that there are multiple letters with multiple sizes. For instance, with a size of 20mm, this is what you get
Because Android needs to accommodate every possible character in a textview with the size you provided. That being said, textSize is not 100% accurate to what you provide to it.
If this is not enough for you, please provide more details of the problem you have at hands.

Android print on specific position regardeless of resolution

I have this scenario:
An activity with a global layout that has a background image that fills it up.
In such image (provided by designer) there is a white box where some text is supposed to be printed with information from the app at runtime.
We are having problems to understand how we can print in the same position regardless of screen resolution and density... Whenever we change the tablet we get wrong positions for the text...
Any hints?.
Thanks in advance
The best way to do this is to ask the designer to break out the box from the image, and then you aren't worried about the exact nuance of image placement.
Okay, so what if that's not an option? This is where you have to go pixel counting. I would recommend you do the following:
Devote an entire class to getting the text right. This class should extend View, or one of it's subclasses.
In the class, you can determine the size of the image, but not at onCreate. You'll have to determine this in a custom View onMeasure.
This class will have to figure out how to stretch the image appropriately, and based on how it's stretched, figure out where the text box should go.
Hopefully this is enough to get a start on it.

Android create semi resizeable box

So basically I have 3 boxes on screen, each displaying different information regarding audio frequencys.
The top box shows the Note, Middle shows the pitch of that Note, and 3rd shows your pitch.
I wanted to have the numbers in a box, and since they constantly change, I wanted the box to remain a certain size instead of constantly increasing in size to fit the text. However, I also don't want to give the boxes a specific size as I want them to be able to resize depending on the screen size.
Is there a way to do this easily? Or would I have to create separate XML files for each device or something?
I will attach an image so you can see what I mean.
Image: http://i.imgur.com/5NVR54N.png
So just imagine that each box is resizing itself depending on what text is placed in the box.
Any help with this would be great thanks!
If "So just imagine that each box is re-sizing itself depending on what text is placed in the box" this is needed then in the XML for the TextView widget that you use for displaying the values use.,
android:layoutWidth="wrap_content", android:layoutHeight="wrap_content" . The wXh of the view would be adjusted according to the content (values) .
If "I wanted the box to remain a certain size instead of constantly increasing in size to fit the text." -If this is not done, that is if you have fixed width text widgets, then the text will be clipped if they exceed the max width.
2.1 By above if you mean same size on each device, then figure out the maximum possible width/height on a mdpi (320X480 mdpi) emulator for each textview, say 100px width and 80px height and is what suits you for the Note, then in the XML for the TextView widget for Note use android:layoutWidth="100dp", android:layoutHeight="80dp" where "dp" would give you device independent pixel size, meaning the size will be adjusted according to the screen density on which the emulator is running.
http://developer.android.com/training/multiscreen/screensizes.html and related material should help.

Is there a fill parent for Text Size?

I am trying to make a sign (the ones that drivers use at the airport to find someone "Mr Smith") and I wanted the sign to have the largest Font size possible, (its for a tablet), I could write a function to change the size depending on the length of the Text but is there a way of doing natively/better?
Thanks
The most flexible way is to create a custom view that draws the text in onDraw(). When drawing the text (with one of Canvas.drawText()) you will have to provide a Paint, and that would let you know the precise length of text, not just length of string (see Paint.measureText()).
This way you would have plenty of ways to calculate and redistribute the space (and it would totally rely on you how).
One way I can think of is calculating the length for a huge font size and then using the h/w ratio to see if I want to fill the screen in width or height.

Categories

Resources