Titanium Community Questions & Answer Archive

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

buttons in android

hi,
is it possible to use images to style buttons?
like, i want an image as background - on tap i would like the image to change to a different one.

i tried to use the parameter backgroundImage and backgroundSelectedImage and image, but they don't work like expected… at least in the emulator.

like: the backgroundSelectedImage has no effecta as far as i could see. and the parameter image just changes the whole button to that image - not just uses that image as icon.

var btnLeft = Ti.UI.createButton({
    backgroundImage:"/images/HouseLeft.png",
    backgroundSelectedImage:"/images/HouseLeft_selected.png",
    image:"/images/OneFamilyHouse.png",
    width:99,
    height:82
});
btnLeft.addEventListener("click", function(e){
    Ti.API.info("click Left");
});
— asked November 18th 2010 by marcin kolonko
  • android
  • backgroundimage
  • backgroundselectedimage
  • button
  • icon
0 Comments

3 Answers

  • Accepted Answer

    Marcin

    I use android, and so I can confirm that buttons do work as expected, including on the emulator. There is a good example in the KitchenSink called button.js. Look at the code for button "b3".

    So, if you run the KS > Controls > Button, and click and hold (but don't release) the big green button, you will see the subtle selected image effect. If you click it normally, it will disable the button for a specified timeout (achieved using the setTimeout() method).

    Most of the functionality you require is achieved using the background*Image properties rather than the image property.

    Take a look, and let me know if you have any questions about it.

    — answered November 18th 2010 by Paul Dowsett
    permalink
    5 Comments
    • that's strange.
      i just compiled kitchensink (android api's 2.0) and i only see one image: BUTT_grn_off.png.

      image doesn't change when holding nor on click… on my GSM Desire and emulator.

      witch api's do you use?

      — commented November 18th 2010 by marcin kolonko
    • just tried 2.1 - still no change

      — commented November 18th 2010 by marcin kolonko
    • Marcin - I'm using 2.2, with SDK 1.4.3. This it in action.

      — commented November 18th 2010 by Paul Dowsett
    • confirmed: it works on 2.2, but not below…

      — commented November 19th 2010 by marcin kolonko
    • Paul: The backgroundImage property does indeed work, but not the "image" property. I have been trying to find out how to make that icon to the left of the title works, but now I see that it doesn't even work in KitchenSink (tested on my android phone).

      When specifying the image property, the whole button except for the text disappears. Check your own code for the "chat" button in kitchensink:

      var b2 = Titanium.UI.createButton({
      title:'I am a Button',
      image:'../images/chat.png',
      width:200,
      height:40,
      top:60
      });

      Very frustrating as I really needed this feature.

      — commented October 10th 2011 by Adam Smith
  • Hi you can try this it may help u.
    var MydealButt = Ti.UI.createButton({
    backgroundImage:'../images/Mydealbutt.png',
    backgroundSelectedImage:'../images/MyButtonselc.png',
    //backgroundFocusedImage:'../images/MyButtonselc.png',
    // focusable:true,
    width:107ratio,
    height:50
    ratio1,
    bottom:0,
    left:0*ratio,
    });

    — answered January 12th 2012 by Gaurav Yadav
    permalink
    0 Comments
  • ' var MydealButt = Ti.UI.createButton({
    backgroundImage:'../images/Mydealbutt.png',
    backgroundSelectedImage:'../images/MyButtonselc.png',
    //backgroundFocusedImage:'../images/MyButtonselc.png',
    // focusable:true,
    width:107ratio,
    height:50
    ratio1,
    bottom:0,
    left:0*ratio,
    });
    '

    — answered January 12th 2012 by Gaurav Yadav
    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.