There has to be a cleaner / quicker way to do this ? I haven't given full code but I was just wondering I'm creating 6 buttons, there must be a way to do this with less code ?
var btn2 = Titanium.UI.createView({ width:BtnWidth, height:BtnHeight, top:FirstRowTop, left:RightBtnsLeft }); var btnLabel2 = Ti.UI.createLabel({ color: '#FFF', font: { fontSize:'18dp' }, shadowColor: '#aaa', shadowOffset: {x:5, y:5}, text: 'Button 2', textAlign: Ti.UI.TEXT_ALIGNMENT_CENTER, width: 'auto', height: 'auto' }); var btnImage = Ti.UI.createImageView({ image:'images/btn1.png', height:Ti.UI.FILL, width:Ti.UI.FILL, }); btn2.add(btnImage); btn2.add(btnLabel2); /*------------------------------------------------------------------------------------*/ var btn3 = Titanium.UI.createView({ width:BtnWidth, height:BtnHeight, top:SecondRowTop, left:LeftBtnsLeft }); var btnLabel3 = Ti.UI.createLabel({ color: '#FFF', font: { fontSize:'18dp' }, shadowColor: '#aaa', shadowOffset: {x:5, y:5}, text: 'Button 3', textAlign: Ti.UI.TEXT_ALIGNMENT_CENTER, width: 'auto', height: 'auto' }); var btnImage = Ti.UI.createImageView({ image:'images/btn1.png', height:Ti.UI.FILL, width:Ti.UI.FILL, }); btn3.add(btnImage); btn3.add(btnLabel3); /*------------------------------------------------------------------------------------*/ var btn4 = Titanium.UI.createView({ width:BtnWidth, height:BtnHeight, top:SecondRowTop, left:RightBtnsLeft }); var btnLabel4 = Ti.UI.createLabel({ color: '#FFF', font: { fontSize:'18dp' }, shadowColor: '#aaa', shadowOffset: {x:5, y:5}, text: 'Button 4', textAlign: Ti.UI.TEXT_ALIGNMENT_CENTER, width: 'auto', height: 'auto' }); var btnImage = Ti.UI.createImageView({ image:'images/btn1.png', height:Ti.UI.FILL, width:Ti.UI.FILL, }); btn4.add(btnImage); btn4.add(btnLabel4); /*------------------------------------------------------------------------------------*/ var btn5 = Titanium.UI.createView({ width:BtnWidth, height:BtnHeight, top:ThirdRowTop, left:LeftBtnsLeft }); var btnLabel5 = Ti.UI.createLabel({ color: '#FFF', font: { fontSize:'18dp' }, shadowColor: '#aaa', shadowOffset: {x:5, y:5}, text: 'Button 5', textAlign: Ti.UI.TEXT_ALIGNMENT_CENTER, width: 'auto', height: 'auto' }); var btnImage = Ti.UI.createImageView({ image:'images/btn1.png', height:Ti.UI.FILL, width:Ti.UI.FILL, }); btn5.add(btnImage); btn5.add(btnLabel5); /*------------------------------------------------------------------------------------*/ var btn6 = Titanium.UI.createView({ width:BtnWidth, height:BtnHeight, top:ThirdRowTop, left:RightBtnsLeft }); var btnLabel6 = Ti.UI.createLabel({ color: '#FFF', font: { fontSize:'18dp' }, shadowColor: '#aaa', shadowOffset: {x:5, y:5}, text: 'Button 6', textAlign: Ti.UI.TEXT_ALIGNMENT_CENTER, width: 'auto', height: 'auto' }); var btnImage = Ti.UI.createImageView({ image:'images/btn1.png', height:Ti.UI.FILL, width:Ti.UI.FILL, }); btn6.add(btnImage); btn6.add(btnLabel6); /*------------------------------------------------------------------------------------*/
1 Answer
Accepted Answer
The code that you've posted is really messed up, but it looks like you're pretty much using the same code over and over to create buttons. So instead of all that code over and over again, put it in a function that creates and returns the object.
function createButton(_args) { var button = Ti.UI.createButton(); return button; };then all you have to do to create the button is call the function for each one that you want to create.
Your Answer
Think you can help? Login to answer this question!