Android Question How to get the new width of a label...

stevel05

Expert
Licensed User
Longtime User
Try this:
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

   
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim L As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
   
   
    L.Initialize("L")
   
    Activity.AddView(L,10,10,-2,-2)
   
    L.Text = "Test"
   
    Log(L.Width)

    CallSubDelayed(Me,"Measure")

End Sub


Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Public Sub Measure
    Dim Layout As JavaObject = AsJO(L).RunMethod("getLayout",Null)
    Log(Layout.RunMethod("getWidth",Null))
End Sub

Sub AsJO(JO As JavaObject) As JavaObject
    Return JO
End Sub
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
upload_2017-8-2_18-3-10.png
upload_2017-8-2_18-3-13.png
upload_2017-8-2_18-3-15.png
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Unfortunately there is a pretty good problem: if the Activity is not in foreground the app crashes.

P.S. ergo, mi sa che si torna al canvas e si crea una nuova classe (o perlomeno si modifica quella appena creata, ma è poco codice).

Comunque, ogni cosa che vuoi fare, soprattutto se TI CHIAMANO LucaMs, ti fa perdere un'intera giornata, per cui per una buona app servono dai 7 ai 10 anni :p

(questo chiaramente perché non hai già pronta una funzione / libreria che faccia ciò che serve in questo caso; ma ci sono sempre... CASI :mad:. Domani avrò questa libreria ed un nuovo progetto potrà usufruirne per cui lo sviluppo sarà più veloce... ma quante di queste mancanze troviamo ogni giorno? VADO A PESCAAAREEEE - nemmeno questo, non me piace!).
 
Last edited:
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Why would the activity not be in the foreground when you are accessing a view that is on it?
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Well, the layout will need to be calculated and drawn before the getLayout call returns meaningful values. You could check if the returned Layout is Null (or similar) and not read the value. Or put it in a try block.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Well, the layout will need to be calculated and drawn before the getLayout call returns meaningful values. You could check if the returned Layout is Null (or similar) and not read the value. Or put it in a try block.
The problem is ALSO that I'm trying to put your example code in a class (and then in a library) and I set the label width (and text) there, so if...

Well, I will think about this tomorrow, thank you, Steve.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Interesting, do you load and assign the font before you set the labels size?
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
It appears to work as expected with a truetype font I have. What font are you loading?
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Seems to work OK on Bluestacks and my Nexus 7 :
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim CustomFont As Typeface
    Dim L As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")

    CustomFont = Typeface.LoadFromAssets("D3_Biscuitism_Bold.ttf")
    L.Initialize("L")
    L.Color = Colors.Yellow
    Activity.AddView(L,10Dip,10Dip,-2,-2)
    L.Typeface = CustomFont
   
   
   
    L.Text = "Test"
   
   
    CallSubDelayed(Me,"Measure")
End Sub

Public Sub Measure
    Dim Layout As JavaObject = AsJO(L).RunMethod("getLayout",Null)
    Log(Layout.RunMethod("getWidth",Null))
End Sub

Sub AsJO(JO As JavaObject) As JavaObject
    Return JO
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
 
Upvote 0
Top