Android Question Loop through buttons

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
Hi

My app has multiple panels, each is a view wirh it's own buttons (simple buttons)

I want to loop on app load through all buttons, but only buttons
Currently when I loop I also get checkboxes...

Is there a way to loop and get only buttons?

Thanks
 
Solution
it does the job but also take checkboxes as buttons...
Check this out:
B4X:
For Each b As B4XView In Panel1.GetAllViewsRecursive
        If b Is CheckBox Then
            b.SetColorAndBorder(xui.Color_white, 4dip, xui.Color_Cyan, 10dip)  'need to use the B4XViews properties
            b.Checked = True
            b.Text = "Zeev"
        else if b Is Button Then
            b.SetColorAndBorder(xui.Color_Green, 4dip, xui.Color_Yellow, 10dip)
            b.TextColor =xui.Color_Red           
        End If
    Next
1674164465987.png

Mahares

Expert
Licensed User
Longtime User
Check out Erel's answer here:
 
Upvote 0

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
Check out Erel's answer here:
thanks,

i did try it but got error

Compiling code. Error
Error compiling program.
Error description: Regular variable followed by '=' expected.
Error occurred on line: 9615
For b As B4XView in Activity.GetAllViewsRecursive
Word: as

i'm feeling lost...
 
Upvote 0

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
B4X:
For Each b As B4XView in Activity.GetAllViewsRecursive
thanks

that did solve the error
but doesn't let me do what i want - i want to change colors...

something like this


Dim cd As ColorDrawable
cd.initialize2(bColor,50,3,fColor)

Try

For Each b As B4XView In Activity.GetAllViewsRecursive
If b Is Button Then
b.color = bColor
b.TextColor = tColor
b.Background = cd
End If
Next

Catch
Log (LastException.Message)
End Try
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
In your layout Buttons are declared as Button or B4XView?
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
Try this
B4X:
For Each b As Button In Activity.GetAllViewsRecursive
    If b Is Button Then
        b.As(B4XView).SetColorAndBorder(Colors.Red, 5, Colors.Blue, 25)
        b.As(B4XView).TextColor = Colors.Black
    End If
Next
Remove or comment this
B4X:
Dim cd As ColorDrawable
cd.initialize2(bColor,50,3,fColor)
 
Last edited:
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
I made some change in the previous code because probably I got inverted value for BorderWidth and CornerRadius.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
You can alternatively use this:
You can declare them as button or B4XView in Globals
B4X:
For Each b As B4XView In Activity.GetAllViewsRecursive
        If b Is Button Then
            b.SetColorAndBorder(xui.Color_Black, 4dip, xui.Color_Cyan, 10dip)  'need to use the B4XViews properties
'            'the rest that applies to B4XView. You cannot use colorDrawable
        End If
    Next
Make sure you use dips like 50dip not 50
My goodness Zeev, you should use code tags when you post code
 
Last edited:
Upvote 0

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
You can alternatively use this:
You can declare them as button or B4XView in Globals
B4X:
For Each b As B4XView In Activity.GetAllViewsRecursive
        If b Is Button Then
            b.SetColorAndBorder(xui.Color_Black, 4dip, xui.Color_Cyan, 10dip)  'need to use the B4XViews properties
'            'the rest that applies to B4XView. You cannot use colorDrawable
        End If
    Next
Make sure you use dips like 50dip not 50
My goodness Zeev, you should use code tags when you post code
thanks,
i'm sure i missed something

when using your code with root - i get error: Undeclared variable 'root' is used before it was assigned any value.

when using the second sample with xui i get: Undeclared variable 'xui' is used before it was assigned any value.

library xui is added of course
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
Your project is based on old Activities, so just change Root in Activity.
Root it's for B4XPages based projects.
Probably you are even missing
B4X:
Private Xui as Xui
in Process_Globals.
 
Upvote 0

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
Your project is based on old Activities, so just change Root in Activity.
Root it's for B4XPages based projects.
Probably you are even missing
B4X:
Private Xui as Xui
in Process_Globals.
something is wrong here, i get errors on stack now
seems i will have to go the primitive way - do it one by one as this way it works
thanks for the good will to help
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
I am quite confident that the code posted by @Mahares should work for you.
If not it would be helpful to post your code.
We are trying our best. ;)
 
Upvote 0

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
I am quite confident that the code posted by @Mahares should work for you.
If not it would be helpful to post your code.
We are trying our best. ;)
i know you are, and i truly appreciate it
i did post the code that in question
but the entire app is huge and i don't think is relevant
i use buttons (simple buttons -> dim b as button
i just want to loop the button and manipulate the colors
i can do it one by one and it works
all the loops did not work as i wanted
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
know you are, and i truly appreciate it

Here is a full conventional project that will illustrate the objective. To give you an idea

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    Private Button1, Button2, Button3, Button4 As B4XView
    Private xui As XUI
    Private Panel1 As B4XView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layoutb")
    Panel1.SetColorAnimated(200, xui.Color_DarkGray, xui.Color_LightGray)
    For Each b As B4XView In Panel1.GetAllViewsRecursive
'    For Each b As B4XView In Activity.GetAllViewsRecursive  'this works too
        If b Is Button Then
            b.SetColorAndBorder(xui.Color_Black, 4dip, xui.Color_Cyan, 10dip)  'need to use the B4XViews properties
            b.TextColor =xui.Color_White
'            b.col'the rest . You cannot use colorDrawable
        End If
    Next
End Sub
 

Attachments

  • Zeev.zip
    9.3 KB · Views: 109
Upvote 0

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
Here is a full conventional project that will illustrate the objective. To give you an idea

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    Private Button1, Button2, Button3, Button4 As B4XView
    Private xui As XUI
    Private Panel1 As B4XView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layoutb")
    Panel1.SetColorAnimated(200, xui.Color_DarkGray, xui.Color_LightGray)
    For Each b As B4XView In Panel1.GetAllViewsRecursive
'    For Each b As B4XView In Activity.GetAllViewsRecursive  'this works too
        If b Is Button Then
            b.SetColorAndBorder(xui.Color_Black, 4dip, xui.Color_Cyan, 10dip)  'need to use the B4XViews properties
            b.TextColor =xui.Color_White
'            b.col'the rest . You cannot use colorDrawable
        End If
    Next
End Sub
it is identical to what i've tried
it does the job but also take checkboxes as buttons...
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
it does the job but also take checkboxes as buttons...
Check this out:
B4X:
For Each b As B4XView In Panel1.GetAllViewsRecursive
        If b Is CheckBox Then
            b.SetColorAndBorder(xui.Color_white, 4dip, xui.Color_Cyan, 10dip)  'need to use the B4XViews properties
            b.Checked = True
            b.Text = "Zeev"
        else if b Is Button Then
            b.SetColorAndBorder(xui.Color_Green, 4dip, xui.Color_Yellow, 10dip)
            b.TextColor =xui.Color_Red           
        End If
    Next
1674164465987.png
 
Upvote 0
Solution

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
Check this out:
B4X:
For Each b As B4XView In Panel1.GetAllViewsRecursive
        If b Is CheckBox Then
            b.SetColorAndBorder(xui.Color_white, 4dip, xui.Color_Cyan, 10dip)  'need to use the B4XViews properties
            b.Checked = True
            b.Text = "Zeev"
        else if b Is Button Then
            b.SetColorAndBorder(xui.Color_Green, 4dip, xui.Color_Yellow, 10dip)
            b.TextColor =xui.Color_Red          
        End If
    Next
View attachment 138300
YES!!! YES!!!
that's it
THANK YOU!
 
Upvote 0
Top