B4A Class Switch view (Android 4+)

Discussion in 'Additional libraries, classes and official updates' started by Erel, Mar 18, 2014.

  1. Erel

    Erel Administrator Staff Member Licensed User

    upload_2014-3-18_9-44-0.png

    This class wraps the native Switch view using JavaObject. This view was added in Android 4+. You can either add the view with the designer as a CustomView (don't forget to set Custom Type field) or add it by code.

    See the attached example.

    Edit: Switch is available in the ViewsEx library.
     
    Last edited: Jun 23, 2016
  2. M. Giray Ozkan

    M. Giray Ozkan Member Licensed User

    Dear Erel,

    Thank you very much!
     
  3. woniol

    woniol Active Member Licensed User

    Thanks
    I was waiting for it for long time.

    I added two more subs for setting and getting Enabled property for this class:
    Code:
    Public Sub setEnabled(b As Boolean)
        joSwitch.RunMethod(
    "setEnabled"Array As Object(b))
    End Sub

    Public Sub getEnabled As Boolean
        
    Return joSwitch.RunMethod("isEnabled"Null)
    End Sub
    and this:
    Code:
    Switch.Enabled = base.Enabled
    to the DesignerCreateView Sub
     
    Last edited: Mar 18, 2014
  4. Theera

    Theera Well-Known Member Licensed User

    It's better than the old strategy. I like it.
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    @woniol it is actually simpler. You can access all of the standard properties through sw.Switch.
    For example:
    Code:
    sw.Switch.Enabled = false
     
    bobh likes this.
  6. Theera

    Theera Well-Known Member Licensed User

    Hi all,
    How do I design alike iOS's ? (Angle of it have radius)
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    Can you post a screenshot?
     
  8. Theera

    Theera Well-Known Member Licensed User

    I mean that switch in picture post#1 is look strong,I would like to can design look like iOS's ,it look like pretty.(Can I use nine-patch with the custom view? or other strategies)
     
  9. Erel

    Erel Administrator Staff Member Licensed User

    This is how the native Switch view looks. If you like to use your own images then you can use ToggleButton and change its background:
    StateListDrawable example
     
    Theera likes this.
  10. Andris

    Andris Active Member Licensed User

    Erel, in this code,

    Code:
    Sub Activity_Create(FirstTime As Boolean)
        
    Activity.LoadLayout("1")
        sw.Initialize(Me, 
    "sw")
        
    Activity.AddView(sw.Switch, 10dip10dip200dip50dip)
        sw.TextOff = 
    "aaa"
        sw.TextOn = 
    "bbb"
    End Sub
    these 2 lines

    Code:
    sw.TextOff = "aaa"
        sw.TextOn = 
    "bbb"
    don't seem to do anything. I assume they are only to set text other than OFF and ON, but they don't work for me. Is this because the necessary sub in the class (setTextOn) is not being referenced correctly?
     
  11. DonManfred

    DonManfred Expert Licensed User

    Change
    to
    in Manifesteditor worked for me
     
  12. Erel

    Erel Administrator Staff Member Licensed User

    It should change the text (see the screenshot above). I've just tested it here on Android 4.4.2 and it works. Which version are you using?
     
  13. Andris

    Andris Active Member Licensed User

    My Galaxy S3 is running Android 4.3.

    I just tried changing the target SDK version in Manifest Editor to "13." It does then work as it should. But with "14" it does not.
     
    Last edited: May 5, 2014
  14. luke2012

    luke2012 Well-Known Member Licensed User

    If I understood well, this library is suitable from Android ICS to KitKat.
    So we have to catch the OS version and if it is before Android 4 we have to use other views intead of this, right ?
     
  15. Erel

    Erel Administrator Staff Member Licensed User

    Yes.
     
  16. Brian Robinson

    Brian Robinson Active Member Licensed User

    Is it possible to change the colour of the "on" state for this switch?
     
  17. Erel

    Erel Administrator Staff Member Licensed User

    You will need to create new images and use them instead (with StateListDrawable).
     
  18. rleiman

    rleiman Well-Known Member Licensed User

    Hi,

    I tried it and the switch buttons show up but there is no text when I tap them.

    I used this in the manifest editor:

    Code:
    'This code will be applied to the manifest file during compilation.
    'You do not need to modify it in most cases.
    'See this link for for more information: http://www.basic4ppc.com/forum/showthread.php?p=78136
    AddManifestText(
    <uses-sdk android:minSdkVersion=
    "4" android:targetSdkVersion="11"/>
    <supports-screens android:largeScreens=
    "true"
        android:normalScreens=
    "true"
        android:smallScreens=
    "true"
        android:anyDensity=
    "true"/>)
    SetApplicationAttribute(android:icon, 
    "@drawable/icon")
    SetApplicationAttribute(android:
    label"$LABEL$")
    'End of default text.
    Did I miss something else?
     
  19. Erel

    Erel Administrator Staff Member Licensed User

    Not even the ON and OFF text?
     
  20. rleiman

    rleiman Well-Known Member Licensed User

    Hi Erel,

    No, the ON and OFF text is not showing up, only the switch view. I can tap them and they will toggle on and off but no text is showing up.
     
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