help - change text align at runtime

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
hi

i am trying to change text align from right to left or left to right at runtime
i try something like this but it does not work - no change
perhaps gravity is not the right way ?
anyone can help
thanks

For i = 0 To Activity.NumberOfViews - 1

G = Gravity.RIGHT

If Activity.GetView(i) Is Label Then
L = Activity.GetView(i)
L.Gravity = G
End If

If Activity.GetView(i) Is CheckBox Then
C = Activity.GetView(i)
C.Gravity = G
End If

If Activity.GetView(i) Is EditText Then
E = Activity.GetView(i)
E.Gravity = G
End If

If Activity.GetView(i) Is RadioButton Then
R = Activity.GetView(i)
R.Gravity = G
End If

Next
 

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User

thank you,
so why doesn't it work?
with or without this code the labels and text fields are aligned the same so it has no effect
 
Upvote 0

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
thank you,
so why doesn't it work?
with or without this code the labels and text fields are aligned the same so it has no effect

sorry for this one but i have a disaster - i can't run the app
when trying to run it i get

jarsigner: unable to sign jar: java.util.zip.ZipException: duplicate entry: AndroidManifest.xml

please help...
 
Upvote 0

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
sorry for this one but i have a disaster - i can't run the app
when trying to run it i get

jarsigner: unable to sign jar: java.util.zip.ZipException: duplicate entry: AndroidManifest.xml

please help...

sorry - fixed it - used a post telling to deleted in jar files...
 
Upvote 0

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
thanks.
it is wide, in some phones it shows the text aligned to right in some to left - it is in hebrew
so i thought to mark a flag and align it accordingly
but it does not work as there is no change in alignment
if i do it from designer to test it is fine
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
The code below works.
B4X:
Sub ToggleButton1_CheckedChange(Checked As Boolean)
    Dim i, G As Int
    Dim objType As String
    
    If Checked Then
        G = Bit.Or(Gravity.CENTER_VERTICAL, Gravity.RIGHT)
    Else
        G = Bit.Or(Gravity.CENTER_VERTICAL, Gravity.LEFT)
    End If
    
    For i = 0 To Activity.NumberOfViews - 1
        objType = GetType(Activity.GetView(i)) 
        Select objType
        Case "android.widget.TextView", _
                "android.widget.EditText", _
                "android.widget.RadioButton", _
                "android.widget.CheckBox"
            Dim lbl As Label
            lbl = Activity.GetView(i)
            lbl.Gravity = G
        End Select
    Next
End Sub
Attached the small test project.
I used GetType to make a distinction between the different view types.
Using If Activity.GetView(j) Is Label
changes also the text alignment in Buttons.

Best regards.
 

Attachments

  • Gravity.zip
    7.5 KB · Views: 216
Upvote 0

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
strange
your sample works like charm
my app not
does it work only on the active panel ?
or can i just run the procedure once at loading time for all views ?

edit:
if i do it directly label by label like l.gravity-... it works
if i use the loop it does not

edit2:
ok, the loop does not change the field
the index numerator increases but the lbl is always the first field
 
Last edited:
Upvote 0

jgmdavies

Member
Licensed User
Longtime User
@Zeev

Re. your edit2, I tried the following and got the expected result in the log, i.e. two different label texts in my case:

B4X:
Sub Test1

    Dim i As Int
    Dim lbl As Label
    Dim objType As String

    For i = 0 To Activity.NumberOfViews - 1

      objType = GetType(Activity.GetView(i)) 
      Log("View " & i & ": " & objType)

      Select objType

      Case "android.widget.TextView", _
                "android.widget.EditText", _
                "android.widget.RadioButton", _
                "android.widget.CheckBox"
            lbl = Activity.GetView(i)
         Log(lbl.Text)

      End Select

   Next

End Sub

HTH
Jim
 
Upvote 0

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
thanks Jim,

the sample code i had worked just fine, oddly - when i tried it on my code it did not. it didn't even show the right text in debug - i mean i wanted to see if it goes to the next label that had another text but it did not
i think it is due to some problem in my app as i load the activity into panel and hide all panels and then show the panel i want to work on
anyways, i solved it by just adding all relevant fields and manually (in code) set them)
yeh, i know it is stupid but i had to solve it asap... and this was dirty and working
 
Upvote 0

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
thanks Klaus,
the code is HUGE and is a live system so it it a bit problematic
never mind - i solved it quick & dirty and moved on

thanks again

BTW - if i may - i am trying the SerialAudio library to read magnetic card through the earphone plug - any idea if it works and how ?
my tests did not work - i just tried the sample code in the original post
no results...
 
Upvote 0

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
sorry for being rude...

is there a way to have a background color to text field ?
i couldn't find it anywhere
it is only in white and i can only set font color
i'd like to set a background color to critical fields

thanks & sorry again
 
Upvote 0

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
sorry - not working
the error is a compile error saying that color is write only
i want to change in run time - possible ?
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…