Search code examples
androidtitaniumappcelerator

How do I make an OptionDialog with checkboxes/Switches (Android Appcelerator)


I want to create an OptionDialog like this on Android (each row having a label on the left and a Switch on the right):

example optiondialog

But the code I am using is not working:

var optsView = Ti.UI.createView({
    backgroundColor : '#ffffff',
});

var opts = ["aaa", "bbb", "ccc"];
for (var i = 0; i < opts.length; i++)
{
    var row = Ti.UI.createView({
        backgroundColor: "#ffffff"
    });

    var label = Ti.UI.createLabel({
        left: "8dp",
        color: 'black',
        size: '14sp',
        title: opts[i]
    });

    var check = Ti.UI.createSwitch({
        style : Ti.UI.Android.SWITCH_STYLE_CHECKBOX,
        value : false,
        right: "8dp"
    }); 

    row.add(label);
    row.add(check);

    optsView.add(row);
}

var option = Ti.UI.createOptionDialog({
    title: 'Configuration',
    androidView: optsView
});

option.show();

The app will crash and show this on the console:

[ERROR] :  TiExceptionHandler: (main) [6049,7004] ----- Titanium Javascript Runtime Error -----
[ERROR] :  TiExceptionHandler: (main) [0,7004] - In undefined:124,16
[ERROR] :  TiExceptionHandler: (main) [0,7004] - Message: Uncaught java.lang.String cannot be cast to java.util.HashMap
[ERROR] :  TiExceptionHandler: (main) [0,7004] - Source:         option.show();

I guess my problem is on the for loop. I tried commenting that piece of code and my app will not crash, but I do not know what is going wrong.


Solution

  • My app was crashing because I typed "size: '14sp'" instead of "font: { fontSize: '14sp' }" on the Label constructor.