Android Question creating layout programmatically

apty

Active Member
Licensed User
Longtime User
is it possible to read layout units and apply to the layout as string? e.g code:
B4X:
dim width as string=File.ReadString(File.DirAssets,"aa.txt")
Activity.width=width

The example text file has 80%x as the content. If i try this i get an error
 

apty

Active Member
Licensed User
Longtime User
I tried that. It gives an error:
java.lang.NumberFormatException: For input string: "80%x"
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I don't know your goal, of course, if it is really useful; anyway, you could create a function like:

B4X:
Sub ConvertStringCoordinate(Container As Panel, Value As String) As Int
    Dim Result As Float
    Value = Value.ToLowerCase

    If Value.Contains("%x") Then
        Result = Value.SubString2(0, Value.IndexOf("%x"))
        Result = Container.Width * (Result / 100)
    Else If Value.Contains("%y") Then
        Result = Value.SubString2(0, Value.IndexOf("%y"))
        Result = Container.Height * (Result / 100)
    Else
        Result = Value
    End If

    Return Result
End Sub

Container should be Activity or a Panel.

NOT TESTED!!!
 
Last edited:
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
At compile time the compiler computes the value. At runtime you have to do yourself.

B4X:
Log(percentX("80%x"))

Sub percentX(w As String) As Int
    Dim wx As Float = w.SubString2(0, w.IndexOf("%"))
    Return wx*Activity.Width/100
End Sub

Just a few minutes too late :)

And your solution is more general.
 
Upvote 0
Top