Text not centered.

trencasostres

Member
Licensed User
Longtime User
Hello,
Does anyone know why the text is not vertically centered in the view when using larger text sizes? For example an edittext with height:120; width:260: text size:130
thanks.
 
It appears OK here (running on Bluestacks at least) I set up an edittext as you said and set Horizontal alignment to CENTER_HORIZONTAL all in the designer.

Typeface : DEFAULT
Style : NORMAL
Horizontal Alignment : CENTER_HORIZONTAL
Vertical Alignment : CENTER_VERTICAL
Size 130
Text Color DEFAULT

It looks as I'd expect. Do you see the cursor in the middle when the edit text is empty?
 
In the example attached, we can see how the vertical centering does not work well. The red line indicates the center of the view. Initially, the text should be centered. Could depend on the level of API? I'll keep trying.
 

Attachments

You need to take into account following parameters:
- Font metrics
- Padding, in your case with a ColorDrawable background there is no padding.

Attached your modified test program.

With big font sizes when the text becomes higher than the view the reference for the display seems to be the top of the character space.

Best regards.
 

Attachments

From the Android developers web site TextView:

setGravity(int gravity)
Sets the horizontal alignment of the text and the vertical gravity that will be used when there is extra space in the TextView beyond what is required for the text itself.

This only has effect if there is extra space in the field, otherwise it appears as Klaus has mentioned that the characters are set to the top position and is borne out by your test app.
 
I am beginner in B4a and I need some time to digest your code. Also, I'm not English speaking. Thank you again.
 
Back
Top