Setting Android Character Width - android

You can control the height of a text character, drawn into the canvas, with setTextSize(). For example, if you want text of height 8 pixels, you would write:
mPaint.setTextSize(8);
This works fairly well. However, I cannot seem to find a way to set the width. Hence a 8x12 pixel character looks exactly the same as an 12x12 character. I am using monospace font and want to carefully control the size of each character in a canvas.
Any ideas on how to set character width in pixels?

I believe the vertical height of the text is considered the text's "size", while the horizontal width is considered the "scale". For example, if you are drawing text onto a canvas using a Paint, the Paint has methods called getTextScaleX and setTextScaleX which you can use to:
Get [and set] the paint's horizontal scale factor for text.
Depending on how you are using the canvas these may be the methods you want to use - most other methods for text deal with horizontal text spacing rather than modifying the size of the actual characters themselves.

Related

90º rotated textview gets size to display text as the horizontal space

I have a textview where I want to show text in vertical (just like rotated 90º). With android:rotation="-90" you can get this.
If the text would have no rotation, and you would like to fit a sentence, you would first look at the width of the textview, since text will try to be in 1 line. Since I rotate the text, here I expect to see the height of the object. However, Android is still considering the width. I need to fix the size of the width, but this causes me a problem for the text: Although the textview has a big vertical dimension and text would fit, Android thinks it needs to use the width to calculate the available space. At the end this causes me overflow of the text, appearing for example, just one word as multiple lines of 1 char.
How can I make android to understand that the space for the lenght to fit the text is now the vertical, instead of the horizontal?

How can I get the size in pixels of the space below the text of a textview? (not the bottom padding)

bottomPadding returns 0 and is set to 0, but my textview still has padding below it's text.
I am trying to draw text on a canvas at the same height as the textview is from its parent but for some reason Paint.drawText() ignores the extra padding at the bottom.
Since the canvas object has its origin a the bottom-left corner, I have to provide the the y position of the text from below.
I can't do the following:
yPosition = textview.bottom + ((rectbounds.getHeight() - textViewHeigth)/2)
This is because the size of the space on top of the text is bigger than the bottom space.
Removing the extra space from the textview would also solve my problem. However, I already tried setting includeFontPadding to false, and setting padding to 0, but neither work.
What happens if you put a 'g', 'p', 'y', or 'j' in your TextView? Each of these characters has a descender, and the metrics for the font will always allocate space for this feature of many scripts. This is to allow for the fact that multiple lines of flowing text ought not be crammed into each other.
If you really need to get the size of a font's descender, you can use a Paint object, load it with a Typeface and ask for its descent(). But a TextView I don't think is going to change the way it renders its lines for you. If you must ignore the descent, draw your own text in a custom view.

TextView onDraw - Drawing Lines

I would like to draw vertical lines between Numbers/Letters in my TextView. So it should look similar to
A|B|C|D
without the use of the | character and use drawLine() instead.
I am trying to do it using the width of the TextView and assuming the centre of each character will find itself at , 1/8, 3/8, 5/8, 7/8 of the TextView width for this example. However the lines dont line up as they should.
Not sure whats not working, help appreciated.
I am trying to do it using the width of the TextView and assuming the centre of each character will find itself at , 1/8, 3/8, 5/8, 7/8 of the TextView width for this example.
That's your problem. For starters, you haven't specified that you're using a mono-spaced font. If you're not, then the letters won't be evenly distributed. Even if you are using a mono-spaced font, likely the padding at the beginning (and possibly end) of the TextView are going to offset things. I can't remember exactly how TextView measures things, but I suspect looking at actual left padding value would be a good start to find the left padding. If you want to use this with a variable width font, you'll want to use something like Paint.measureText to measure the width of the characters.
Once you have all that, you can add the width of the character(s) to the left padding to find the position to place each line.

How to have a circular TextView

I have been trying to make a circular TextView. Its a circle in which I want to accomodate whole space above a circular bubble as shown in image below.
Kindly see attached image.
In this image, we have a circular bubble with circular text in it.
I have already tried setting oval shape .xml as background of TextView but still no luck.
Edit:
As text length increase. It must reduces in size to fit inside the circle. This is the hardest part to think about.
You need to create a custom view, extending from TextView probably, setting the circle as background image, and calculate the text width / break the lines manually according to the width of the text.
To calculate the width of a string, see How to calculate string font width in pixels?
Some math and calculations is required of course to measure the available space per line; but I think that's the only way, as there's no standard component out there to do it.
To place the text onto the view, use drawText of the Canvas class.

how do I automatically adjust textSize in android?

I am writing an app, in which part of it displays a line of text. there are certain scenarios where that line of text will take up more than one line. the problem is that I only want it to take up one line, and when I set it up either in java or in xml to only take up one line, the text is cut off. how would I make it so that it automatically adjusts the font size of the text so that it will only take up one line without being cut off?
Use proportions along with Paint.measureText():
(text size / measureText width) = (perfectSize / screenWidth)
Solving for the perfect text size:
perfectSize = (text size / measureText width) * screenWidth;
You can find the screen width with getWindowManager().getDefaultDisplay().getWidth() from the Display class.
Turned it into a math problem!
This isn't too hard to do if you use Paint#measureText.
Basically you would start with a font height smaller than the height of your TextView and then iterate (or do a binary search) through fonts of varying sizes until you find one that measures to smaller than the width of your TextView.
It requires some implementation, as there is no "automatic" way provided by the framework.

Categories

Resources