I am trying to set some LabelField
text during its layout (inside the layout() method) depending on display's orientation in the following way:
LabelField roomDescLabel = new LabelField("", Field.FIELD_VCENTER) {
protected void layout(int width, int height) {
if (Display.ORIENTATION_LANDSCAPE == orientation) {
setText("LANDSCAPE");
} else {
setText("NOT LANDSCAPE");
}
super.layout(width, height);
}
};
Sometimes, I am catching an IllegalStateException
with Layout requested during layout message. The message makes sense since that's exactly what I am doing (I am calling setText()
which in its turn requests layout update). Is there any acceptable way to set the text value depending on display's orientation?
I've found a solution to this issue but I am not totally satisfied about it. The solution would be to override the LabelField
manager's sublayout()
method and replace the existing label field with a new one with updated text (and only than call to super.sublayout()
). Surprisingly, this workaround works as expected and is not triggering layout update.
UPDATE:
Another possible solution would be to call invokeLater()
with the setText()
operation in the layout()
method of the LabelField
. The disadvantage in this approach compared to the solution I proposed earlier is that sublayout()
and paint()
methods will be called twice.