Android Question [Solved] Center vertically text in BBCodeView

Toky Olivier

Active Member
Licensed User
I did many attemps to center vertically text in BBCodeView when paragraph height is not larger than the Base height (or the scrollview height) but no success. It's like text in BBLabel which is centered vertically but in a BBCodeView. I picked code from BBLabel (from the Redraw sub) but always, there are errors.
My target is to put a BBCodeView in a custom B4XDialog and I don't want to have texts in the top but centered when there are only few texts. The scrollview will be used only when there are long text in the BBCodeView.
Can someone help please?
 

Erel

Administrator
Staff member
Licensed User
B4J code that sets the BBCodeView height to match the content and puts it in the vertical center of the form:
B4X:
    BBCodeView1.Text = $"wefwefwef
wefwef jlkwe fjlkwef
wef jwklefj lwkef wefw
f
wef"$
SetBBCodeViewPosition

Private Sub SetBBCodeViewPosition
    If BBCodeView1.Paragraph.IsInitialized Then
        Dim ContentHeight As Int = Min(BBCodeView1.Paragraph.Height / TextEngine.mScale + BBCodeView1.Padding.Top + BBCodeView1.Padding.Bottom, BBCodeView1.mBase.Height)
        BBCodeView1.mBase.Height = ContentHeight
        BBCodeView1.mBase.Top = MainForm.RootPane.Height / 2 - ContentHeight / 2
    End If
End Sub

Sub MainForm_Resize (Width As Double, Height As Double)
    SetBBCodeViewPosition
End Sub
 

Toky Olivier

Active Member
Licensed User
Thank you @Erel. It works. See below the result, the dialog is more natural now and if the text is longer, text with all formatting and stuffs will appear in the scrollview:
Dlg.png


I integrated your code in the library like this so that I can reuse it later by setting the variable CenterVerticallyIfPossible to true:
B4X:
Private Sub SetBBCodePosition
    If CenterVerticallyIfPossible And Paragraph.IsInitialized Then
        Dim ContentHeight As Int = Min(Paragraph.Height / mTextEngine.mScale + Padding.Top + Padding.Bottom, mBase.Height)
        Dim mParent As B4XView = mBase.Parent
        mBase.Height = ContentHeight
        If mParent.IsInitialized Then
            mBase.Top = mParent.Height / 2 - ContentHeight / 2
        End If
    End If
End Sub
Enclosed the modified b4xlib.
 

Attachments

Last edited:

Toky Olivier

Active Member
Licensed User
It is a mistake to modify the library for this. You will need to do this change whenever the library is updated.

There is nothing wrong with keeping this sub in your code.
Noted. I'll do like you said. Thank you
 

Toky Olivier

Active Member
Licensed User
It is a mistake to modify the library for this. You will need to do this change whenever the library is updated.

There is nothing wrong with keeping this sub in your code.
But you don't like to add something like this in the original library? May be someone also need it.
 
Top