Search code examples
titaniumappceleratortitanium-mobile

How to change button states in Appcelerator Titanium


I've a series of buttons.

Each button has 2 different states: First State, Second State. The buttons changes into alternating states with each click.

In a scenario, if I click on Button1, it'll be in Second state. Then I click on Button2, Button2 will turn into Second State while Button1 (or any other buttons which is in the Second state) returns to the First State.

How do I do this in Appcelerator Titanium?

I've created the buttons like this

function createButtons(data){

    for (var i = 0; i < data.length; i++){
        //Creating each button
        var button  = Titanium.UI.createImageView({
            image:  data[i].path,
            value: 1
        });

        //Adding the buttons to the center view
        centerButtons.add(button);
    }
}

With each click, I'm changing the value of the button to either 1 or 2 to identify which state the button is in. The problem is, say when I click on Button1, I can change it's value but I don't know how to detect which other button(s) is/are already in the second state so that I can reset it to it's first state?


Solution

  • The following sample code will simply do your job. Here I have used buttons instead of imageView. You can change your code using it.

    var win = Ti.UI.createWindow({
        backgroundColor : 'white'
    });
    var currentView = Ti.UI.createView({
        backgroundColor : '#EFEFEF'
    });
    var button = [],top = 0;
    for (var i = 0; i < 5; i++){
        top += 80;
        //Creating each button
        button[i]  = Titanium.UI.createButton({
            color : 'red',
            top   : top,
            width : '80%',
            value : 1
        });
        button[i].title  = 'State ' + button[i].value;
        button[i].addEventListener('click',changeState);
        //Adding the buttons to the center view
        currentView.add(button[i]);
    }
    
    var buttonState  = Titanium.UI.createButton({
        color  : 'red',
        top    : top + 80,
        title  : 'View button states',
        width : '80%',
    });
    
    var lblStates  = Titanium.UI.createLabel({
        color  : 'red',
        layout: 'horizontal',
        top    : top + 160,
        text  : 'Click on show button to view the button states',
        width : '80%',
    });
    
    buttonState.addEventListener('click', showButtonStates);
    currentView.add(lblStates);
    currentView.add(buttonState);
    win.add(currentView);
    win.open();
    //Changing the state of the clicked button
    function changeState(e){
        e.source.value= 2;
        e.source.title  = 'State ' + e.source.value; 
        for(var i = 0;i<5;i++){
            if(e.source !== button[i]){
                button[i].value = 1;
                button[i].title  = 'State ' + button[i].value; 
            }
        }    
    }
    //To display the button state
    function showButtonStates(){
        lblStates.text = "";
        for(var i =0;i<5;i++){
            lblStates.text = lblStates.text + '\nbutton' + (i+1) + ' ---> state: ' + button[i].value; 
        }
    }