Search code examples
codenameonetextfieldvertical-alignment

Codename One TextField Unable to Align Text


I have a series of text fields in an Android app. I want to align the text vertically to the center (and horizontally to the left), but I can't find the correct approach.

This is my code:

textField.setUIID("TextFieldSome");
textField.getAllStyles().setAlignment(Component.BOTTOM);

This is the css:

TextFieldSome {

    background-color: rgba(255,0, 255, 0.2);
    color: blue; 
    font-size: 9pt; 
    margin-left: 5pt;
    margin-right: 15pt;
    margin-bottom: 5pt; 
    text-align: left;
    padding-left: 20pt; 

}

This is the result:

enter image description here

I have tried WITHOUT css and using these code variations:

textField.getAllStyles().setAlignment(TextField.BOTTOM);
textField.setAlignment(Component.BOTTOM);
textField.setAlignment(Component.TOP);
textField.setAlignment(TextField.BOTTOM);

But the text always aligns vertically to the center and never to the top or bottom.

What am I missing?


Solution

  • The setAlignment method is defined as:

    Sets the Alignment of the Label to one of: CENTER, LEFT, RIGHT

    We don't support vertical alignment of text in the API. Even center horizontal alignment isn't supported well in text components because of the complexity related to that. The core problem is switching back and forth to native which would make the text "jump".