Android Question Programatically setting Label height, width, position?

Roger Daley

Well-Known Member
Licensed User
Longtime User
Hi All

A question about somethimg which I thought should not be a problem.
In the following code I try to specify the labels Height/Width/Left/Top all of which cause a crash. I can't see the issue.
The line "FNumLabel(i).Height = 5.625%y" is the first to throw an error.
I am sure it is something simple but I usually do all this in the designer.

B4X:
Sub Globals
  Dim FNumLabel(16) As Label  
End Sub

Sub FNumSelect
'Brings up ListView to allow selection of F0-F15 as assigned function.
'Called by Formula_click
    ListFlag = 1
    FNumView.SingleLineLayout.ItemHeight = 5.625%y
    FNumView.Color = Colors.DarkGray                'ListView color matches Label color
    FNumView.Clear  

    For i = 0 To 15   
        For Each v As View In Activity.GetAllViewsRecursive
            If v Is Label Then
                If v.Tag = "F"&i Then 
                    FNumLabel(i).Initialize("FNumLabel"&i)
                    FNumLabel(i).TextColor = lbl.TextColor 
                    FNumLabel(i).Text = lbl.Text
                    FNumLabel(i).Height = 5.625%y
                    FNumLabel(i).Width = 100%x
                    FNumLabel(i).Color = Colors.Transparent
                    FNumLabel(i).Left = 0
                    FNumLabel(i).Top = 10%y + (i*5.625%y)          
                   End If
            End If
        Next         
        FNumView.AddSingleLine("")                                
     Next
   
    Activity.AddView(FNumView, 0, 10%y, 100%x, 90%y)   'Displays the list built by the previous loop.
    lblTitle.Text = "Select a Function Key to Program"
    lblTitle.TextSize = 14*FontScale
    lblTitle.BringToFront
    lblTitle.Height = 10%y
    ListBack.BringToFront
    ListBack.Height = 10%y
    FNumView.BringToFront 
End Sub

LabelError.jpg

Thanks in advance
Roger
 

LucaMs

Expert
Licensed User
Longtime User
B4X:
Dim LblID as Int 
        For Each v As View In Activity.GetAllViewsRecursive
            If v Is Label Then
                If v.Tag = "F"&LblID Then
                    FNumLabel(LblID).Initialize("FNumLabel"&i)
                    FNumLabel(LblID).TextColor = lbl.TextColor
                    FNumLabel(LblID).Text = lbl.Text
                    FNumLabel(LblID).Height = 5.625%y
                    FNumLabel(LblID).Width = 100%x
                    FNumLabel(LblID).Color = Colors.Transparent
                    FNumLabel(LblID).Left = 0
                    FNumLabel(LblID).Top = 10%y + (i*5.625%y)    ' <--- ?
                    LblID = LblID + 1     
                   End If
            End If
        Next

I'm not sure, of course, because I don't know your code, but can it be right?
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
I believe the problem will be that you cannot set the height or other properties becuse you have not added the view to anything yet. It essentially doesn't exist yet.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
that should work, but you can't read out unless it's added to a parent.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
the approach of that for loop with nested getallviews doesn't make sense.

you should add the view to an map with the tag as key, then you can call it easily.

what is it that you are trying to do, @Roger Daley ?
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
LucaMs

Still baffling.

The "NullPointerException" can't be because the label is not created as the code handle the Label.text, label.textcolor OK
The Log(i) shows the error occurs first time through the loop.
Using the LblID code produces the same error at the "FNumLabel(LblID).Height = 5.625%y" line

Roger
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
Sorex/RandomCoder

Long sad story in summary.
I have 16 Labels [F0-F15] initially labelled F0-F15 in white text.
The user in operation changes the text of a label and the program changes the text colour.
When the user operates the appropriate key a list view is built F0-F15

I thought it would be good to have the ListView show the current label text and in in the text colour. not possible with ListView
I tried and even with Erels help could not master CustomListView.

Realizing that the ListView in this case does not scroll I thought I would be clever and produce labels to stick on each item of the ListView.
It seems I have now dug myself in to a deeper hole. I thought this would be a quick and minor asthetics upgrade, wrong.

I have tried inserting this line Activity.AddView(FNumLabel(i), 0, 10%y, 100%x, 5.625%y) after this line FNumLabel(i).Initialize("FNumLabel"&i).
The result is an error occurs at this line If v.Tag = "F"&i Then on the second pass of the i loop. It means something.


Just for completness the full code is HERE the PGM button is the button that brings up the ListView.

Any help much appreciated

Regards Roger
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
I added a design with 2 labels (F0 & F1) and this works without errors

B4X:
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("main")
Dim FNumLabel(16) As Label

For i = 0 To 15 
        For Each v As View In Activity.GetAllViewsRecursive
            If v Is Label Then
                If v.Tag = "F"&i Then
                    FNumLabel(i).Initialize("FNumLabel"&i)
                    Activity.AddView(FNumLabel(i),0,0,100,100)
                    FNumLabel(i).TextColor = Colors.Yellow
                    FNumLabel(i).Text = i &" label"
                    FNumLabel(i).Height = 5.625%y
                    FNumLabel(i).Width = 100%x
                    FNumLabel(i).Color = Colors.Transparent
                    FNumLabel(i).Left = 0
                    FNumLabel(i).Top = 10%y + (i*5.625%y)        
                   End If
            End If
        Next       
     Next
End Sub


I don't know why you need extra labels when using the add single line later tho.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Sorry, but I still don't understand what exactly you want to do.
What is the ListView supposed to do and what are the labels supposed to do ?
If you want scrolling labels you could use a ScrollView for this.

I had a look at the project, but I can't test it, I get an error in line 324 Recording(FNM,STEPS,DATA2) = Recording2(FNM,STEPS,DATA2) because Recording2 is empty when the program is run the first time.

The labels are defined in the Designer, so you could simply define an array of labels.
Dim FNumLabel() As Label
FNumLabel = Array As Label (lblF0, lblF1, lblF2, lblF3, lblF4, lblF5, lblF6, lblF7, lblF8, lblF9, lblF10, lblF11, lblF12, lblF13, lblF14, lblF15)

Then you can access each Label with FNumLabel(i).
And your code would look like this:
B4X:
For i = 0 To 15
    FNumLabel(i).TextColor = Colors.Yellow
    FNumLabel(i).Text = i &" label"
    FNumLabel(i).Height = 5.625%y
    FNumLabel(i).Width = 100%x
    FNumLabel(i).Color = Colors.Transparent
    FNumLabel(i).Left = 0
    FNumLabel(i).Top = 10%y + (i*5.625%y)    
Next
 
Last edited:
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
Klaus/Sorex

Firstly Klaus I don't understand why you couldn't run the project, I have just downloaded it and run it on an emulator I have never used before. All worked.
DownLoad from PlayStore might help explanation.

F0-F15 represent programmed functions, when a position is programmed the text and text colour change. When the user wishes to program another position the FNumselect module is called and a list of the function positions is generated using listview. Currently this list shows F0-F15 in the default white text.

I want the list to show the actual text and text colour on the labels. A listview cannot different coloured text in different items. To do this CustomListView would work I am told. I have failed to come to grips with CustomListView. To bypass this I thought to produce 16 labels with the corect text and text colour, then position them over the corresponding item on the listview.

Calculator showing labels where keys have been programmed. Calc.jpg



List of positions to be selection to be programmed Select.jpg would be better if F7 and F10 showed the text on the calculator in Gold.
This is what I am trying to do.

The code that you have both shown in posts #12 & #13 is basically what I have done and does not work for me. If you can spot where the following code goes wrong please throw a brick at me. I can't see it.

B4X:
    For i = 0 To 15   
        Log(i)
        For Each v As View In Activity.GetAllViewsRecursive
            If v Is Label Then
                Dim lbl As Label = v
                If lbl.Tag = "F"&i Then
                     'lblText = lbl.Text       
                    FNumLabel(i).Initialize("FNumLabel"&i)
                    Activity.AddView(FNumLabel(i), 0, 10%y, 100%x, 5.625%y)
                    FNumLabel(i).TextColor = lbl.TextColor 
                    FNumLabel(i).Text = lbl.Text
                    FNumLabel(i).TextSize = 18*FontScale   
                    FNumLabel(i).Height = 5.625%y
                    FNumLabel(i).Width = 100%x
                    FNumLabel(i).Color = Colors.Transparent
                    FNumLabel(i).Left = 0
                    FNumLabel(i).Top = 10%y + (i*5.625%y)          
                   End If
            End If
        Next                                'Builds the viewable list RecallView from the List "storelist"
    
       'FNumView.AddSingleLine("")           
      Next

Thanks for your continuing help.
Roger
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Instead of using a ListView and the Labels you should use a ScrollView with the 16 Labels on it.
You define an array of Labels and add those onto the ScrollView and then you can access each Label with its index and change whatever you need.
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
Thanks to all of the above for your help.

I have finally nailed it. The core of the probblem was as RandomCoder said in post #7, but had a number of other bad coding issues so it was hard to reveal as the villian.
All came good after I put everthing in some sort of logical order and plugged a few holes.

Again thanks to everyone for the help.

Regards Roger


B4X:
Sub FNumSelect
'Brings up ListView to allow selection of F0-F15 as assigned function.
'Called by Formula_click
    ListFlag = 1
     Activity.AddView(FNumView, 0, 10%y, 100%x, 90%y)   'Displays the list built by the previous loop.
    FNumView.SingleLineLayout.ItemHeight = FNumView.SingleLineLayout.Label.Height
    FNumView.SingleLineLayout.ItemHeight = 5.625%y
    FNumView.Color = Colors.DarkGray                'ListView color matches Label color as labels don't fill ListView
    FNumView.Clear   
    FNumView.BringToFront                               
    For i = 0 To 15           
        FNumView.AddSingleLine("")       
        If FNumLabel(i).IsInitialized = False Then
            FNumLabel(i).Initialize("FNumLabel"&i)
        End If
        Activity.AddView(FNumLabel(i), 0, 10%y, 100%x, 5.625%y)
        FNumLabel(i).TextSize = 18*FontScale   
        FNumLabel(i).Height = 5.625%y
        FNumLabel(i).Width = 100%x
        FNumLabel(i).Color = Colors.Transparent
        FNumLabel(i).Left = 0
        FNumLabel(i).Top = 10%y + (i*5.625%y)        
        FNumLabel(i).BringToFront
        FNumLabel(i).Gravity = Bit.Or(Gravity.CENTER_VERTICAL, Gravity.LEFT)
        FNumLabel(i).Tag = "FNL"&i
        For Each v As View In Activity.GetAllViewsRecursive
            If v Is Label Then
                Dim lbl As Label = v
                If lbl.Tag = Null Then lbl.Tag = ""
                If lbl.Tag = "F"&i Then   
                    FNumLabel(i).TextColor = lbl.TextColor 
                    FNumLabel(i).Text = lbl.Text
                   End If
            End If
        Next                                   
      Next
    lblTitle.Text = "Select a Function Key to Program"
    lblTitle.TextSize = 14*FontScale
    lblTitle.BringToFront
    lblTitle.Height = 10%y
    lblTitle.Width = 100%x
    ListBack.BringToFront
    ListBack.Height = 10%y
End Sub

Sub FNumView_ItemClick (Position As Int, Value As Object)
'Completes/Saves the selection of F0-F15 to be the assigned function.
'Returns to Formula_click
    FNum = Position
    FNumV = "F"&FNum 'Value
    FNum_flag = 1
    FNumView.RemoveView                        'Exit ListView.
    For i = 0 To 15       
        FNumLabel(i).RemoveView
    Next   
    lblTitle.TextSize = 28*FontScale
    lblTitle.SendToBack
    ListBack.SendToBack
    listclear.SendToBack
    ListFlag = 0
    Formula_click
End Sub
 
Upvote 0
Top