Android Question Iterating though views and accessing their properties

Discussion in 'Android Questions' started by MitchBu, May 11, 2019.

  1. MitchBu

    MitchBu Active Member Licensed User

    Here is what I tried to do :
    Code:
    For Each v As View In Activity.GetAllViewsRecursive
            
    If v Is EditText And Diko.ContainsKey(v.tag) Then
                    v.TextColor = 
    0xFF6361FF
            
    End If
        
    Next
    Of course it does not work since v is a view and not an EditText. But, would it be possible somehow to cast v to EditText so I can access the view's properties ?
     
  2. agraham

    agraham Expert Licensed User

    Try
    Code:
    For Each v As View In Activity.GetAllViewsRecursive
            
    If v Is EditText And Diko.ContainsKey(v.tag) Then
                    
    Dim vedt as EditText = v
                    vedt.TextColor = 
    0xFF6361FF
            
    End If
        
    Next
     
    José J. Aguilar and MitchBu like this.
  3. MitchBu

    MitchBu Active Member Licensed User

    As often, asking a question leads to the answer.

    I finally did this :
    Code:
    For Each E As EditText In Activity.GetAllViewsRecursive
            
    If e Is EditText Then e.TextColor = 0xFF6361FF
        
    Next
    I test e against EditText, because Labels get fetched, and trying to set the TextColor property triggered an error.
     
  4. agraham

    agraham Expert Licensed User

    That's very interesting. I would have expected it to throw an error if there were any views other than EditTexts in the activity as the generated Java loop code casts each view in turn to an EditText. I wonder why that cast doesn't error for your Labels :confused:
    Code:
    _e = new anywheresoftware.b4a.objects.EditTextWrapper();
    ...
    for (; index1 < groupLen1;index1++){
    _e.setObject((android.widget.EditText)(group1.Get(index1)));
     
    MitchBu likes this.
  5. MitchBu

    MitchBu Active Member Licensed User

    This is a better solution than what I was doing. Thank you agraham :)
     
  6. MitchBu

    MitchBu Active Member Licensed User

    You were right, agraham. It did eventually trigger an error, which seems to be random.
     
    mangojack likes this.
  7. Erel

    Erel Administrator Staff Member Licensed User

    I like to use B4XView (XUI library) for such cases:
    Code:
    For Each E As B4XView In Activity.GetAllViewsRecursive
            
    If e Is EditText Then e.TextColor = 0xFF6361FF
    Next
    This code is valid.
     
    MitchBu likes this.
  8. MitchBu

    MitchBu Active Member Licensed User

    What is the advantage of using B4XView as opposed to View ?
     
  9. agraham

    agraham Expert Licensed User

    I had to dig into the Java in XUI.jar to see why! :confused:
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice