Titanium Community Questions & Answer Archive

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

Toolbar button click doesn't backgroundImage change...

Hello,

I don't understand why my buttonFavorite's backgroundImage doesn't change when I click on ?

var buttonFavorite = Titanium.UI.createButton({
  backgroundImage:'images/favorite.png',
  height:22,
  width:23
});

win.toolbar = [buttonFavorite];

buttonFavorite.addEventListener('click', function() {
  win.toolbar[0].backgroundImage='images/unfavorite.png';
  // or
  // buttonFavorite.backgroundImage='images/unfavorite.png'
});
— asked October 6th 2010 by MILLET Alexandre
  • backgroundimage
  • button
  • win.toolbar
0 Comments

2 Answers

  • Thanks John, that was exactly what I was looking for; here is somewhat more complete example:

    
    var play = Titanium.UI.createButton({
        systemButton: Titanium.UI.iPhone.SystemButton.PLAY,
    });
    
    var pause = Titanium.UI.createButton({
        systemButton: Titanium.UI.iPhone.SystemButton.PAUSE,
    });
    
    MainTests.toolbar1 = Titanium.UI.createToolbar({
        items: [play],
        top: 0,
        borderTop: true,
        borderBottom: false,
        translucent: true,
        barColor: '#999'
    });
    
    MainTests.detailWindow.add(MainTests.toolbar1);
    
    play.addEventListener('click',
    function(e)
    {
        MainTests.toolbar1.items = [pause];
    });
    
    pause.addEventListener('click',
    function(e)
    {
        MainTests.toolbar1.items = [play];
    });
    
    — answered February 15th 2011 by Dirk Krause
    permalink
    0 Comments
  • I tried a couple ways to do this and I didn't find an elegant solution but this works. It seems the only way to force the image to change seems to be creating a new button and putting it back into the toolbar. BTW - I use the image properly, not backgroundImage when I put images into buttons.

    var buttonFavorite = Titanium.UI.createButton({
      image:'images/favorite.png',
      height:22,
      width:23
    });
    
    win.toolbar = [buttonFavorite];
    
    buttonFavorite.addEventListener('click', function() {
      var buttonFavorite = Titanium.UI.createButton({
        image:'images/unfavorite.png',
        height:22,
        width:23
      });
    
      win.toolbar = [buttonFavorite];
    });
    
    — answered October 7th 2010 by John McKnight
    permalink
    0 Comments
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.