Android Code Snippet Change EditText Colors

Discussion in 'Code Snippets' started by Erel, Dec 26, 2017.

  1. Erel

    Erel Administrator Staff Member Licensed User

    [​IMG]

    1. Add:
    Code:
    #AdditionalJar: com.android.support:support-compat
    2.
    Code:
    Sub SetBackgroundTintList(View As View,Active As Int, Enabled As Int)
       
    Dim States(2,1As Int
       States(
    0,0) = 16842908     'Active
       States(1,0) = 16842910    'Enabled
       Dim Color(2As Int = Array As Int(Active,Enabled)
       
    Dim CSL As JavaObject
       CSL.InitializeNewInstance(
    "android.content.res.ColorStateList",Array As Object(States,Color))
       
    Dim jo As JavaObject
       jo.InitializeStatic(
    "android.support.v4.view.ViewCompat")
       jo.RunMethod(
    "setBackgroundTintList"Array(View, CSL))
    End Sub
    Example:
    Code:
    SetBackgroundTintList(EditText1, Colors.Red, 0xFF0020FF)
    It will only be changed on Android 5+.
     
  2. ilan

    ilan Expert Licensed User

  3. Erel

    Erel Administrator Staff Member Licensed User

    There is no simple way to change the cursor color. The only non-hacky way to do it is by creating the EditText fields with a XML layout and loading the layout with XmlLayoutBuilder.
     
  4. ilan

    ilan Expert Licensed User

  5. noeleon

    noeleon Member Licensed User

    Can it be done in manifest, changing the BackgroundTintList ?
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    Might be. I'm not sure. Anyway it is very simple to use this code. If you want to replace the color of all EditTexts:
    Code:
    For Each v As View in Activity.GetAllViewsRecursive
     
    If v Is EditText Then
      SetBackgroundTintList(v, ...)
     
    End If
    Next
     
    noeleon likes this.
  7. noeleon

    noeleon Member Licensed User

    yes it's a lot easier than manually editing layout xml files which i'm too lazy to try.
     
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