Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

switchbox vs checkbox

is the switchbox the only way to realize a checkbox? what about the nice hook and the plain and small design. i already saw apps using normal checkboxes. is the only way to create them within native objective-c code?

— asked October 31st 2010 by dev 1605
  • checkbox
  • checkboxes
  • switchbox
0 Comments

9 Answers

  • A little change to mitchell amihod solution using unicodes.
    It works perfectly on Android and iOS

    var checkbox = Ti.UI.createButton({
        title: '',
        top: 10,
        right: 10,
        width: 30,
        height: 30,
        borderColor: '#666',
        borderWidth: 2,
        borderRadius: 3,
        backgroundColor: '#aaa',
        backgroundImage: 'none',
        color: '#fff',
        font:{fontSize: 25, fontWeight: 'bold'},
        value: false //value is a custom property in this casehere.
    });
    
    //Attach some simple on/off actions
    checkbox.on = function() {
        this.backgroundColor = '#007690';
        this.title='\u2713';
        this.value = true;
    };
    
    checkbox.off = function() {
        this.backgroundColor = '#aaa';
        this.title='';
        this.value = false;
    };
    
    checkbox.addEventListener('click', function(e) {
        if(false == e.source.value) {
            e.source.on();
        } else {
            e.source.off();
        }
    });
    

    Regards
    Davide

    — answered October 27th 2012 by Davide Cuttini
    permalink
    4 Comments
    • Remove backgroundImage: 'none' because now it will search for an image 'none' which doesn't exists.

      — commented November 26th 2012 by Jesse R
    • +1 helped me many times

      — commented February 21st 2013 by Muhammad Wahhab Mirza
    • +1. Neat, very helpful.

      — commented June 9th 2013 by Thierry Godfroid
    • Very neat, thank you

      — commented August 8th 2014 by Ganna Kozynenko
  • Undocumented APIs FTW!

    Simply add the following to your createSwitch function (Android only as far as I can tell, sorry iOS devs!):

    style:Titanium.UI.Android.SWITCH_STYLE_CHECKBOX

    So, for example, the following will create a checkbox instead of the standard switch:

    var basicSwitch = Titanium.UI.createSwitch({
        style:Titanium.UI.Android.SWITCH_STYLE_CHECKBOX,
        value:false
    });
    

    This GitHub commit to the SDK tipped me off (thank you Google!).

    — answered June 29th 2011 by Steven Mirabito
    permalink
    0 Comments
  • Here's a no images required technique I'm trying out with a project.
    Works on iphone.

    //I use the character viewer in osx to pick the checkbox
    //you could use any symbol
    var checkbox = Ti.UI.createButton({
        title: '?',
        top: 10,
        right: 10,
        width: 30,
        height: 30,
        borderColor: '#666',
        borderWidth: 2,
        borderRadius: 15,
        backgroundColor: '#aaa',
        backgroundImage: 'none',
        color: '#fff',
        font:{fontSize: 25, fontWeight: 'bold'},
        value: false //value is a custom property in this casehere.
    });
    
    //Attach some simple on/off actions
    checkbox.on = function() {
        this.backgroundColor = '#159902';
        this.value = true;
    };
    
    checkbox.off = function() {
        this.backgroundColor = '#aaa';
        this.value = false;
    };
    
    checkbox.addEventListener('click', function(e) {
        if(false == e.source.value) {
            e.source.on();
        } else {
            e.source.off();
        }
    });
    
    — answered August 18th 2011 by mitchell amihod
    permalink
    5 Comments
    • the ? is a checkbox. it works in preview mode, but not when posted.

      ?
      

      — commented August 18th 2011 by mitchell amihod
    • I assume you mean that the '?' is the unicode symbol for a checkmark? Like this one: http://www.fileformat.info/info/unicode/char/2713/index.htm

      — commented August 18th 2011 by Jonathan Abrams
    • exactly. for some reason, Q&A site shows it properly in preview mode, but strips it out when posting.

      — commented August 18th 2011 by mitchell amihod
    • Since the whole idea behind Titanium is to be native, why don't you just use the native iOS switch (the famous On/Off slider)? It seems to me that that would be easier.

      — commented August 18th 2011 by Steven Mirabito
    • sometimes on/off isn't the semantics you need.

      — commented October 13th 2011 by mitchell amihod
  • There you go guys! Hope works for u

    
    var self = Ti.UI.createView({
            backgroundColor:'white'
        });
    
        var check_box = Ti.UI.createView({
            backgroundImage:'checkbox.png',
            width:20,
            height:20
        });
    
        check_box.addEventListener('click',function(){
    
            if(check_box.getBackgroundImage() == 'checkbox.png')
            {
                check_box.setBackgroundImage('checkbox-checked.png');
            }
            else if(check_box.getBackgroundImage() == 'checkbox-checked.png')
            {
                check_box.setBackgroundImage('checkbox.png');
            }
        });
    
        self.add(check_box);
    

    alt text

    alt text

    — answered November 22nd 2012 by Sid Joshi
    permalink
    0 Comments
  • The only way I found was to fake it with images.

    var paidSwitch = Titanium.UI.createSwitch({
      top:240,
      left:50,
      value:false,
      visible:false
    });
    view.add(paidSwitch);
    var paidCheckbox = Titanium.UI.createImageView({
      image:'images/unchecked.png',
      width:30,
      height:30,
      top:240,
      left:50
    });
    paidCheckbox.addEventListener('click',function(e) {
      if(paidSwitch.value==false) {
        paidSwitch.value = true;
        paidCheckbox.image = 'images/checked.png';
      } else {
        paidSwitch = false;
        paidCheckbox.image = 'images/unchecked.png';
      }
    });
    view.add(paidCheckbox);
    

    You can use whatever you want for the checked/unchecked images. I ran a standard app in the emulator, took a screenshot, and cut out the checked/unchecked images with Photoshop. Here's what I created: checked and unchecked

    Tim

    — answered October 31st 2010 by Tim Poulsen
    permalink
    4 Comments
    • This is great Tim, thanks.
      Just to point out there appears to be the .value missing from paidSwitch = false;
      I have also noticed that I need to click twice for the toggle to take effect on the iPhone although this works perfectly on the android. I'll poke about and see if I can work out why.
      Cheers

      — commented December 10th 2010 by andy lovegrove
    • I am having issues with this I am not having with switches alone. Originally, I did this sort of thing with switches alone, and all worked. I'd like to use a simulated checkbox, though.

      screenshot

      First of all, the switches are still visible, despite setting visible:false. I am creating my table rows programmatically: http://pastie.org/1760375

      First of all, I have a function that needs to check how many switches were turned on, and then disable the remaining switches (so that, for example, only 3 items in a table can be chosen at a time). This function does not work with the image control, as I see no way to disable the image controls (seems to be no enabled property).

      How could I emulate this limit behavior with the checkbox toggle method?

      — commented April 5th 2011 by Steve Lombardi
    • The above code is not working for me!

      — commented August 5th 2011 by Balu C
    • Change the last 'paidSwitch' to 'paidSwitch.value' and that will fix the multiple toggle issue.

      — commented December 21st 2011 by Chris K
  • It works better on android to not use the switch…

    var eachCheckbox = Titanium.UI.createImageView({
      image:'unchecked.png',
      width:30,
      height:30,
      top:toppos,
      left:5
    });
    
    eachCheckbox.addEventListener('click',function(e) {
      if(eachCheckbox.image== 'unchecked.png') {
        eachCheckbox.image = 'checked.png';
      } else {
        eachCheckbox.image = 'unchecked.png';
      }
    });
    

    Otherwise you can only check the box and uncheck it once.
    I haven't tested it on ipod.

    — answered November 27th 2010 by Stan Thompson
    permalink
    0 Comments
  • How would you implement this for a tableview…where there are multiple rows…and a checkbox in each of them??

    ~Brian

    — answered March 29th 2011 by Brian Dittmer
    permalink
    2 Comments
    • you would get the source of the event and then find it's parent which would give you the row information

      — commented March 29th 2011 by Aaron Saunders
    • How can we get the source of the event for the particular row selected from multiple rows?

      — commented July 29th 2011 by Balu C
  • Thanks Steven….this yields a box with a horizontal line in it that can be toggled. Wondering if there is a version with an actual checkmark?

    — answered August 4th 2011 by Brian Dittmer
    permalink
    1 Comment
    • The code above should produce the checkbox you're looking for…. please make sure you're on the latest version of the Titanium SDK and that you pasted the code mentioned above into your code correctly. What you're getting is the default switch, not a checkbox.

      — commented August 4th 2011 by Steven Mirabito
  • Thanks Steve…..we're still using mobile sdk 1.6.2 – so I will check it out in 1.7x (we're waiting for 1.8 to arrive to upgrade)

    ~Brian

    — answered August 4th 2011 by Brian Dittmer
    permalink
    5 Comments
    • whoops my mis-read…not the mobile SDK. verifying code now :-)

      — commented August 4th 2011 by Brian Dittmer
    • Works like a charm…sorry for the earlier confusion on our part.

      — commented August 4th 2011 by Brian Dittmer
    • Great! Glad I could help!

      — commented August 4th 2011 by Steven Mirabito
    • Steve – that checkbox now works great…but when we added it to our row view….rowtouch stopped working…. did you run into that? Here's a post with the code:

      http://developer.appcelerator.com/question/124049/row-click-stops-working-if-have-native-checkbox

      — commented August 17th 2011 by Brian Dittmer
    • Actually, I have run into that. I have absolutely no idea why, but I will try the solution mentioned in that thread.

      — commented August 18th 2011 by Steven Mirabito
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.