Titanium Community Questions & Answer Archive

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

backgroundImage problem

I'm new to Titanium and can't make buttons gain the desired background at creation time. I can change the background during events, but not initially. Anyone got any ideas?

I've got the following code to create the button:

var a1 = Titanium.UI.createButton({
    title:'Hit',
    backgroundColor:'#33ccff',
    backgroundImage:'images/BUTT_drk_off.png',
    height:40,
    width:200,
    top:100,
    color:'#fff'
});

If I change backgroundImage in my event listener then it works, and I can change back and forth between my images fine. It's just the initial state that doesn't seem to work for me.

Running the resultant code on the Android emulator, BTW.

— asked March 12th 2010 by John Pettigrew
  • backgroundimage
  • button
0 Comments

4 Answers

  • Accepted Answer

    For some reason,it seems you have to use 'image' when dealing with Android buttons.

    var a1 = Titanium.UI.createButton({
        title:'Hit',
        backgroundColor:'#33ccff',
        image:'images/BUTT_drk_off.png'
    });
    

    backgroundImage appears to only work on iPhone.

    — answered March 12th 2010 by Jacob Williams
    permalink
    1 Comment
    • hi Jacob Williams i am also facing the same problem .i try your code but when i run i am seeing an black window and button is not visible .plz help.

      — commented March 1st 2012 by Rizwan Khan
  • Hmm. Actually, although I can indeed set the button image with image, I can't then change it in response to events! So it's one step forwards and one back!

    — answered March 12th 2010 by John Pettigrew
    permalink
    0 Comments
  • To clarify you are running into issues in creating the button with the desired image? If that is the case I'd recommend looking at the KitchenSink example button.js it has some nice examples like the below.

    Another area to look into is if any of the events you are using to manage your button images might be firing during the load process. This could be causing issues with image when the page is loaded.

    var b3 = Titanium.UI.createButton({
        color:'#fff',
        backgroundImage:'../images/BUTT_grn_off.png',
        backgroundSelectedImage:'../images/BUTT_grn_on.png',
        backgroundDisabledImage: '../images/BUTT_drk_off.png',
        top:110,
        width:301,
        height:57,
        font:{fontSize:20,fontWeight:'bold',fontFamily:'Helvetica Neue'},
        title:'Click Me'
    });
    
    — answered March 12th 2010 by Ben Bahrenburg
    permalink
    0 Comments
  • Ben,
    It's not the creation but setting an image that's the problem. Indeed, I can (as I said) change the image. I just can't get a different image to appear from creation time, just the default appearance.

    Jacob,
    It's odd but I can change the image using backgroundImage on Android. Setting it with image does work, but of course I then need to change it with image also. Many thanks for this. Now to try and code so that it'll work on iPhone as well as Android.

    Hope this bug gets sorted soon!

    — answered March 12th 2010 by John Pettigrew
    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.