Android Question Every new Panel a new Color

Alexander Stolte

Expert
Licensed User
Longtime User
Greetings from Germany,

I have a Problem, i have 4 Color, and 1 Panel.
I add the Panel in my ListView and by adding the next panel into the ListView i will a new Color for my Panel.

I need a Loop, after the 4. Color return to the first color.

I have no Idia how to programm it :/

My Code:

B4X:
Sub CreateListItem(Text As String, Width As Int, Height As Int) As Panel
    Dim p As Panel
    p.Initialize("")
    'we need to add the panel to a parent to set its dimensions. It will be removed after the layout is loaded.
    Activity.AddView(p, 0, 0, Width, Height)
    p.LoadLayout("frm_content")
    p.RemoveView
    'label1 and button1 will point to the last added views.
   
    Return p
End Sub

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

    For i = 1 To 10
        clv1.Add(CreateListItem($"Item #${i}"$, clv1.AsView.Width, 150dip), 150dip, $"Item #${i}"$)
    Next
End Sub

B4X:
Sub Globals
     Private pnl_content001 As Panel
   End Sub
 

Attachments

  • Screenshot_20161227-010319.png
    Screenshot_20161227-010319.png
    47.2 KB · Views: 168
  • Screenshot_20161229-034745.png
    Screenshot_20161229-034745.png
    45.9 KB · Views: 161

DonManfred

Expert
Licensed User
Longtime User
Code 90% Erel, one method from me

Create a new code Module "tools" with the code
B4X:
'Code module
'Subs in this code module will be accessible from all modules.
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private data As List
End Sub
Public Sub Initialize
    data.Initialize
End Sub
Public Sub nextitem As Object
    Dim o As Object = data.Get(0)
    data.RemoveAt(0)
    data.Add(o)
    Return o
End Sub

Public Sub Push(o As Object)
    data.Add(o)
End Sub

Public Sub Pop As Object
    Dim o As Object = data.Get(data.Size - 1)
    data.RemoveAt(data.Size - 1)
    Return o
End Sub

Public Sub Peak As Object
    Return data.Get(data.Size - 1)
End Sub

Public Sub getSize As Int
    Return data.Size
End Sub

Example use:

B4X:
    tools.Initialize
    tools.Push(Colors.Black)
    tools.Push(Colors.Red)
    tools.Push(Colors.Yellow)
    tools.Push(Colors.Transparent)
 
    For i = 0 To 99
        Log(tools.nextitem)
    Next

So, basically in your Create item method you do a
B4X:
dim color as int = tools.nextitem
' Use this color to set it to the active panel
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I add the Panel in my ListView
You must be more accurate. CustomListView and ListView are two different things.

B4X:
Sub Process_Globals
 Private MyColors() As Int = Array As Int(Colors.Red, Colors.Green, Colors.Blue, Colors.Black)
 Private ColorIndex As Int
End Sub

Sub CreateListItem
 ...
 p.Color = MyColors(ColorIndex)
 ColorIndex = (ColorIndex + 1) Mod MyColors.Length
 Return p
End Sub
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
Incrementing a pointer to get the colour from an array is how I would have approached the problem. But I would have used an If statement to determine when the array length was exceeded and thus reset the pointer back to zero.
B4X:
ColorIndex = (ColorIndex + 1) Mod MyColors.Length
@Erel very nice code, I'll have to try and remember to use the Mod function in the future! Very neat!!
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
@Erel

The Color is the same with your code.

B4X:
Private MyColors() As Int = Array As Int(Colors.RGB(74,74,82), Colors.RGB(132,121,121), Colors.RGB(152,99,122), Colors.RGB(162,116,92))
    Private ColorIndex As Int

B4X:
Sub CreateListItem(Text As String, Width As Int, Height As Int) As Panel
    Dim p As Panel
    p.Initialize("")
    'we need to add the panel to a parent to set its dimensions. It will be removed after the layout is loaded.
    Activity.AddView(p, 0, 0, Width, Height)
    p.LoadLayout("frm_content")
    p.RemoveView
    'label1 and button1 will point to the last added views.
  
    p.Color = MyColors(ColorIndex)
    ColorIndex = (ColorIndex + 1) Mod MyColors.Length
  
    Return p
End Sub

I´m VisualBasic Developer, this language ist new for me :)
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
You load a layout file to the panel (p) "frm_content". Make sure the layout has a transparent backround. ;)

EDIT: does your layout contains a panel? do you want to change that panel color? the code you posted above will change the CLV item background color.
each item in a CLV is basically a panel and you change the color of it but if you load a layout that contains another view then you need to change the color of that view.
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Private ColorIndex As Int
sure you want to use a PRIVATE variable? If the variable is declared outside of our sub then your sub does not have access to the right variable.
I suggest to use a Global PUBLIC valiable for the list and the indexvariable.
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
It Works :)

B4X:
Dim pnl_content As  Panel

ppnl_content.Color = MyColors(ColorIndex)

So now i have a Problem, i will on the ground a imageview like the Picture, but the Problem is, the CustomListView is on Top and my ImageView in the background.
How can i fix it?
 

Attachments

  • Screenshot_20161230-024615.png
    Screenshot_20161230-024615.png
    57 KB · Views: 158
  • 001.PNG
    001.PNG
    8 KB · Views: 181
Upvote 0

ilan

Expert
Licensed User
Longtime User
It Works :)

B4X:
Dim pnl_content As  Panel

ppnl_content.Color = MyColors(ColorIndex)

So now i have a Problem, i will on the ground a imageview like the Picture, but the Problem is, the CustomListView is on Top and my ImageView in the background.
How can i fix it?

you can add an imageview to the activity via designer or via code and then call

imageview.BringtoFront and it will be in z order on the top place.

btw, "will" in english means something different then in german. will in english means "werde..." like "ich werde morgen..." i will tomorrow...
the right word is "want", "i want..." (ich will...) :)
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
I use:

B4X:
ImageView_addcontent.Initialize("frm_chatrooms")
    ImageView_addcontent.BringtoFront

the CustomListView is on top, and the ImageView is still in back...

have you added a image to the imageview?
note that if you are adding the imageview via designer you should not intialize it.
 
Upvote 0
Top