Access View properties

Penko

Active Member
Licensed User
Longtime User
Hello.

I want to base my activity on one main ScrollView and add multiple panels. I want to achieve something like this:

https://lh4.ggpht.com/56JUPtwXyn9KJu52RqUM-bm9jxf4BLJM84BDk_UianAJpE6ST6GMPe0jiSDKNHrbJ1ph

B4X:
Sub Activity_Create(FirstTime As Boolean)

   Activity.LoadLayout("layTests_def") '  my Activity layout
   
   
   For i = 0 To 10
   
   Dim pnl As Panel
   pnl.LoadLayout("layTests_pnl") ' a special layout for panels
   
 pnl.GetView(0).text = ?
   

   Next

End Sub

pnl.GetView(0).text = ? - here comes the problem. I am unable to access the text property. I am planning to put Tags to make the difference between the labels so when I Fetch all Panel views, I know what text to put there. Unfortunately, I am unable to cast the view to label and pass .Text by reference back to the view.

I know I can define all labels by code but I wanted to use the designer to design easier and just put contents by code.

Any ideas?
 

alfcen

Well-Known Member
Licensed User
Longtime User
Hi Penko

A panel has no Text property, but you can employ Tag to store associated text data.
The principle procedure of adding views with a common click event is:

B4X:
Sub Activity_Create(FirstTime As Boolean)
   For i = 0 To 10
      Dim pnl As Panel
      pnl.Initialize("SelectPanel")
      pnl.Tag = "Panel " & i & " contents"
      Activity.AddView(pnl, 0, 50dip * i , Activity.Width, 50dip)
   Next
End Sub   

Sub SelectPanel_Click
   Dim p as Panel
   p = Sender
   Dim my_text_for_label as String
   my_text_for_label = p.Tag
End Sub

Since you intend to place panels on a scrollview, I would
simply create a layout for the scrollview containing 10 panels
with appropriate tag data and event name.

ScrollView1.Panel.LoadLayout("layout_myPanels")
 
Upvote 0

Penko

Active Member
Licensed User
Longtime User
@alfcen, Thank you very much for the detailed reply you've written.

I have been stuck on this for 1 hour now and finally figured out the most of it. I am adding panels with code now and the panels layout is loaded from a file as you suggest.

The problem is that I have several labels in the layout that I have to fill with the actual data to present to the user.

If we assume the panel is p and the current View is v (from a For loop).
B4X:
 p.GetView(v).Text= "This is the text"
- there is no Text property as the compiler can't know what type of View it is.

To summarize, the only problem I still have is the access to the .Text property of the labels. I have to somehow tell the compiler that p.GetView(v) is a Label and thus receive access to the .Text Property.

Check the attached file to see how my labels have their default values now and are not filled with any meaningful data.

The final summarized question would be - am I forced to use labels added on-the-fly to be able to access their .Text property or there is something I am missing with the access to the .Text property of items added with the designer but being dynamic and repeated multiple times(inside panels)?
 

Attachments

  • screenshot.png
    screenshot.png
    27.4 KB · Views: 174
Upvote 0

klaus

Expert
Licensed User
Longtime User
Attached you find a small example showing almost the example you supplied.
Before you can use GetView you need to add the Panel to the ScrollView.Panel.
A View doesn't have a Text property so you need, in your case, to dim a Label and set this one with GetView etc.
B4X:
Sub Globals
    Dim scvTest As ScrollView
    Dim NbPanels, pnlHeight1, pnlWidth, pnlHeight As Int
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("main")
    FillScrollView
End Sub

Sub FillScrollView
    Dim i, j As Int
    Dim pnlTop As Int

    NbPanels = 10
    pnlHeight = 70dip
    pnlHeight1 = pnlHeight + 4dip
    pnlWidth = scvTest.Panel.Width
    pnlTop = 0
    For i = 0 To NbPanels -1
        Dim pnl As Panel
        pnl.Initialize("Panel")
        pnl.LoadLayout("panel1")
        scvTest.Panel.AddView(pnl, 0, i * pnlHeight1, pnlWidth, pnlHeight)
        For j = 0 To pnl.NumberOfViews - 1
            Dim lbl As Label
            lbl = pnl.GetView(j)
            lbl.Text = "Test " & i & " / " & j
            lbl.Tag = 1000 * i + j
        Next
    Next
    scvTest.Panel.Height = i * pnlHeight1
End Sub

Sub Label_Click
    Dim lbl As Label
    Dim i, j As Int

    lbl = Sender
    
    i = Floor(lbl.Tag / 1000)
    j = lbl.Tag Mod 1000
    
    Activity.Title = "Panel " & i & " / Label" & j & " clicked"
End Sub
Best regards.
 

Attachments

  • ScrrollViewWithPanels.zip
    7.1 KB · Views: 179
  • ScrrollViewWithPanels.jpg
    ScrrollViewWithPanels.jpg
    44 KB · Views: 195
Upvote 0

Penko

Active Member
Licensed User
Longtime User
Thanks Klaus, this is exactly what I was supposed to achieve.

The problem was:

B4X:
Dim lb1 As Label
      lb1 = p.GetView(0)
      lb1.Text = "Item: " & i

I didn't expect that GetView() returns a reference to the original view on the Activity. I thought it's a copy of this view. Now things are clear.

Credits go for both of you guys, thank you one more time.
 
Upvote 0

alfcen

Well-Known Member
Licensed User
Longtime User
Hallo Klaus,
Thanks a lot for joining. Top class!

Penko,
When dealing with views this comes in handy as it helps minimize errors:

ex.: If Panel1.GetView(9) Is Label Then

Good luck with your project.
 
Upvote 0

Penko

Active Member
Licensed User
Longtime User
Sure! I am planning to use the .Tag property to make the difference between the labels. E.g "NAME, PROGRESS, etc...".

The type check is definitely a must although I am absolutely sure my panel includes only labels. But this can change in time and if I forget to change the code.... (it will be a good amount of debugging time).

Thank you, I love working with B4A but it is not rare that I still have questions which stuck me for a while with the project progress. Will demonstrate soon :)
 
Upvote 0

alfcen

Well-Known Member
Licensed User
Longtime User
Never mind, I am also stuck with a few issues.
Haven't learned the lesson myself yet, but patience helps solve a few problems.
 
Upvote 0

Penko

Active Member
Licensed User
Longtime User
:))) @alfcen, impossible is nothing :p

Unfortunately, I have to continue the topic with new news. Can anyone tell me why First snippet changes the color of the last added panel? It looks much simpler and faster than fetching all views but it makes the last panel yellow no matter which panel I've clicked? The second snippet works but it is the logical error here I am interested in.

First snippet:
B4X:
Dim pnlto As Panel
pnlto = Sender
pnlto.Color = Colors.Yellow

Second snippet:
B4X:
Dim pnlto As Panel
pnlto = Sender

For j = 0 To scrollViewMain.Panel.NumberOfViews - 1
   
   If(scrollViewMain.Panel.GetView(j) Is Panel AND scrollViewMain.Panel.GetView(j).Tag = pnlto.Tag) Then
      scrollViewMain.Panel.GetView(j).Color = Colors.Cyan
   End If

Next
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
It's impossible to give you any concrete answer, missing information:
- How did you define the Panels and where ?
- In what routine are your code snippets ?

It's always the best to post the whole project as a zip file or at least a smaller project that shows the problem. So we can see what you have done and how and test the project in the same conditions as you do.

Best regards.
 
Upvote 0

Penko

Active Member
Licensed User
Longtime User
Klaus, sorry for not posting back. The problem was the some as in my other topic. I was defining the variable outside the For loop and it was causing the issue. No, it is figured out. Today I even got help from another post of yours where you explained how to clear the scrollView before re-loading the data. It was big help. Thank you very much.

Sent from my HTC Desire using Tapatalk
 
Upvote 0
Top