Android Code Snippet Label Justification Mode

Ok, Good morning to all. It's a small tiny snippet that frustrated me for many years and VOILA today morning I found, so simply the solution that was so simple and silly.
So, decided to share to rescue developers' lives.
To justify text within the label just use the Reflector library and one line of code:
[ReflectorObject].Target = [LabelView]
[ReflectorObject].RunMethod2("setJustificationMode", 1, "java.lang.int")

That's it. So silly not?
 

yiankos1

Well-Known Member
Licensed User
Longtime User
Hello,
I am trying your snippet but throws error:
B4X:
Error occurred on line: 138 (fullArticle)
java.lang.NoSuchMethodException: setJustificationMode [int]
    at java.lang.Class.getMethod(Class.java:1981)
    at java.lang.Class.getDeclaredMethod(Class.java:1960)
    at anywheresoftware.b4a.agraham.reflection.Reflection.runmethod(Reflection.java:214)
    at anywheresoftware.b4a.agraham.reflection.Reflection.RunMethod2(Reflection.java:817)
    at com.dreamon.therapy.fullarticle._clvarticles_visiblerangechanged(fullarticle.java:451)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at anywheresoftware.b4a.BA$2.run(BA.java:387)
    at android.os.Handler.handleCallback(Handler.java:761)
    at android.os.Handler.dispatchMessage(Handler.java:98)
    at android.os.Looper.loop(Looper.java:156)
    at android.app.ActivityThread.main(ActivityThread.java:6577)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:831)

B4X:
            Private rf As Reflector
            rf.Target=lblMainDummy
            rf.RunMethod2("setJustificationMode", 1, "java.lang.int")
 

yiankos1

Well-Known Member
Licensed User
Longtime User
You can also do it with the JavaObject library:

B4X:
    Private jo As JavaObject = Label1
    jo.RunMethod("setJustificationMode", Array As Object(1))
Goodmorning Klaus,
Thank you for your time adding your solution. I don't know why throws this error again. My label(textView) does not have this method (setJustificationMode) and i can't figure out why. By the way it would pretty for my app to have this justification but i will try to live without it!
Greetings from extra HOT Greece. (40 °C at 01:00 p.m.)
 

mcqueccu

Expert
Licensed User
Longtime User
Goodmorning Klaus,
Thank you for your time adding your solution. I don't know why throws this error again. My label(textView) does not have this method (setJustificationMode) and i can't figure out why. By the way it would pretty for my app to have this justification but i will try to live without it!
Greetings from extra HOT Greece. (40 °C at 01:00 p.m.)

Which android version are you testing it on? If its less than android 8 (api 26) it will not work, because it was introduced from API level 26.

Try this (It means the justification will only happen on android 8+ devices only
B4X:
    Dim p As Phone    '<----Add phone Library
    If p.SdkVersion >= 26 Then
        Private jo As JavaObject = Label1
        jo.RunMethod("setJustificationMode", Array As Object(1))
    End If
 
Top