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.
 

stevel05

Expert
Licensed User
Longtime User
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?
 

trencasostres

Member
Licensed User
Longtime User
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

  • textsize.zip
    7.3 KB · Views: 214

klaus

Expert
Licensed User
Longtime User
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

  • textsize_1.zip
    7.7 KB · Views: 238

stevel05

Expert
Licensed User
Longtime User
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.
 

trencasostres

Member
Licensed User
Longtime User
Thanks Klaus. I will study your contribution, although it is a little difficult to understand for me.
 

trencasostres

Member
Licensed User
Longtime User
I am beginner in B4a and I need some time to digest your code. Also, I'm not English speaking. Thank you again.
 
Top