Create custom picker control - titanium

See screenshot of custom picker:
I know there isn't a way to style the picker control in Titanium. This picker only needs to work on iOS (iPad only). I was thinking I could hack the TableView control to use instead of the picker to achieve the style desired. Is that reasonable? How would I snap the TableViewRows so one is always in the center like seen in typical picker controls?

that's a tough one i would say you just only need views and labels and the swipe event (you can recognize if some one swipe up and down ) and just changes the labels. you can do this with a callback function i hope this code will help you (we have created a custom picker with this code)
using alloy
XML
<View id="numOfIntrusionsPickerContainer" class="compositeWrapperPicker noMargins" >
<View id="numOfIntrusionsButtonContainer" class="horizontalWrapper"></View>
CSS
".compositeWrapperPicker" : {
height: Ti.UI.SIZE,
layout: "composite",
width:Ti.UI.SIZE
},
".horizontalWrapper" : {
width: Ti.UI.SIZE,
height: Ti.UI.SIZE,
layout: "horizontal"
},
JS
// INSTANTIATION
var args = arguments[0] || {};
var widthValue = 120;
var pickerEnabled = true;
if(args.width != null){
widthValue = args.width;
}
if(args.isEnabled != null){
pickerEnabled = args.isEnabled;
}
// STYLING
// ADDITIONS
// pressed arg can be: true,false,null. false & null are equal
// true = 'yes' is Pressed at creation, false/null = 'no' is pressed
var btn_main = Ti.UI.createButton({
top: 5,
bottom: 0,
left:0,
right:5,
height: 45,
enabled: pickerEnabled,
width: widthValue,
borderRadius: 5,
backgroundImage: "/images/bttn_gray_flex.png",
backgroundSelectedImage : "/images/bttn_gray_press_flex.png",
backgroundFocusedImage : "/images/bttn_gray_press_flex.png"
});
var picker_divider = Ti.UI.createImageView({
right: 43,
bottom:2,
touchEnable:false,
focusable:false,
image: "/images/Divider_picker.png"
});
var picker_arrows = Ti.UI.createImageView({
right: 16,
top: 17,
touchEnabled: false,
focusable: false,
image: "/images/bttn_selector_arrow.png"
});
$.pickerContainer.add(btn_main);
$.pickerContainer.add(picker_divider);
$.pickerContainer.add(picker_arrows);
// CODE
// LISTENERS
if(args.callBack != null){
btn_main.addEventListener("click",function(_event){
args.callBack(_event);
});
}

Related

SwiftUI - context menu background colour

Context menu background not updated
I am trying to update background colour.
Background colour is updated for view, but it is not getting updated for the context menu
Context menu shows previous colour which was set.
can someone help me with this.
thanks in advance
this is the code i used
import SwiftUI
struct ContextMenu: View {
/*List of items =*/
#State var bgColor = Color.gray
var body: some View {
HStack {
Rectangle().frame(width: 120, height: 120).opacity(0.01).border(Color.black, width: 1).contextMenu{
VStack {
Button("Orange",action: {
self.bgColor = Color.orange
})
Button("Green",action: {
self.bgColor = Color.green
})
Button("Red",action: {
self.bgColor = Color.red
})
}
}
}.frame(width:UIScreen.main.bounds.width, height: 200).background(bgColor)
}
}
Context menu caches content and reuse it all the time. Here is possible solution to force update it.
Tested with Xcode 11.4 / iOS 13.4
HStack {
Rectangle().fill(bgColor) // << use same color
.frame(width: 120, height: 120)
.border(Color.black, width: 1)
.contextMenu{
VStack {
Button("Orange",action: {
self.bgColor = Color.orange
})
Button("Green",action: {
self.bgColor = Color.green
})
Button("Red",action: {
self.bgColor = Color.red
})
}
}.id(UUID()) // << force recreate context menu
}.frame(width:UIScreen.main.bounds.width, height: 200).background(bgColor)

Alloy and require external JS

Right now I have a function in my alloy.js file that is a global.
Alloy.Globals.indicator = function(parent)
{
var view = Ti.UI.createView({
width: '100%',
height: '100%',
backgroundColor: '#000',
opacity: 0.6,
visible: false
});
function osIndicatorStyle()
{
style = Ti.UI.iPhone.ActivityIndicatorStyle.PLAIN;
if ('iPhone OS' !== Ti.Platform.name) style = Ti.UI.ActivityIndicatorStyle.DARK;
return style;
};
var activityIndicator = Ti.UI.createActivityIndicator({
style: osIndicatorStyle(),
height: Ti.UI.FILL,
width: 100
});
view.add(activityIndicator);
parent.add(view);
function openIndicator()
{
view.visible = true;
activityIndicator.show();
}
view.openIndicator = openIndicator;
function closeIndicator()
{
activityIndicator.hide();
view.visible = false;
}
view.closeIndicator = closeIndicator;
return view;
};
I'd rather not have this large function as a global and instead import it to the files I need using require.
I have searched and cannot figure out first, where to place this file and second how to actually "require" it.
All this simply does is create a view that acts as a modal view with a activity indicator. The function also includes two function to show and hide it.
Make a folder called "lib" inside the "app" folder.
Inside this folder, make a file called whatever you like e.g. functions.js:
var functionName = function(){
//your function code here
}
exports.functionName = functionName;
In your controller:
var functions = require('functions');
functions.functionName();
You might want to also look at Widgets which are re-usable components complete with view/controller/styles as I think this would fit your requirement slightly better.
Link to docs

Changing layout onOrientationChange

Is there a currently supported way to change layouts according to device orientation?
The following code returns this error:
onOrientationChange: function () {
this.remove();
this.setLayout( Ext.Viewport.getOrientation() == 'landscape' ?
{ type: 'hbox', pack: 'center', align: 'stretch' } : { type: 'vbox', align: 'stretch', pack: 'center' } );
},
"Replacing a layout after one has already been initialized is not currently supported."
Please any one give me suggestion.
Check out the code for the Sencha Meetcha app on GitHub.
What you may have to do, since replacing a layout is unsupported, is use some CSS tricks and when the orientation changes, remove the old cls and apply the new one.
I haven't tried this myself but the demo app seems to do layout changes very well.
Code snippet included
orientationCls: ['home-portrait', 'home-landscape'],
doOrientation: function() {
var me = this,
orientation = Ext.Viewport.getOrientation(),
letter = orientation.charAt(0),
items = this.getItems().getRange(),
i = 0,
ln = items.length,
item, el, top, left;
me.element.removeCls(me.orientationCls);
me.element.addCls('home-' + orientation);
for (; i < ln; i++) {
item = items[i];
top = item.initialConfig[letter + 'top'];
left = item.initialConfig[letter + 'left'];
el = item.element;
if (el) {
el.setBox({
top: top,
left: left
});
}
}
}
Hope this helps

window background image not showing on iphone Titanium

i have created tab group with single tab and window for tab , i have set background image for tab window, image is showing properly on android but in iPhone i cant see any image any solution to this problem?
var tabGroup = Titanium.UI.createTabGroup({
navBarHidden: true,
layout: 'vertical'
});
Ti.UI.Clipboard.setText('');
var db = require('dbhelper');
db.callDb();
var windowTitle = Ti.UI.createLabel({
color: '#fff',
text: 'IEMR LITE'
});
var win1 = Titanium.UI.createWindow({
backgroundColor: '#fff'
});
if (Ti.Platform.name === 'iPhone OS') {
win1.titleControl = windowTitle;
win1.barImage = 'images/actionbar3.png';
win1.hideTabBar();
} else {}
win1.backgroundImage = 'images/default_portrait.png';
I have tried this code and its works for me. also make sure you are providing correct image path.
var tabGroup = Titanium.UI.createTabGroup({
navBarHidden : true,
layout : 'vertical'
});
Ti.UI.Clipboard.setText('');
//var db = require('dbhelper');
//db.callDb();
var windowTitle = Ti.UI.createLabel({
color : '#fff',
text : 'IEMR LITE'
});
var win1 = Titanium.UI.createWindow({
backgroundColor : '#fff'
});
if (Ti.Platform.name === 'iPhone OS') {
win1.titleControl = windowTitle;
win1.barImage = '1.png';
win1.hideTabBar();
} else { }
win1.backgroundImage = '2.png';
var tab1 =Ti.UI.createTab({
window : win1
});
tabGroup.addTab(tab1);
tabGroup.open();
Try deleting your build folder and building for iPhone again. Sometimes after adding an image (or other binary resource), it doesn't get linked to the underlying built project properly until you do a clean build.

Attaching an Event Listener

I've just finished creating a meticulously generated grid of icons (imageViews) and now I need to be able to do something with them. What I'm finding, though, is that the event listener I'm trying to bind isn't getting bound. Window loads, my icons are displayed nicely, but they aren't clickable.
Can anyone see what I'm missing? The code below is a fully functional (except for the part that doesn't function) file. You should be able to copy it into a test app and load it right up (may be iPhone-only at the moment).
Any insight would be much appreciated.
// this sets the background color of the master UIView (when there are no windows/tab groups on it)
Ti.UI.setBackgroundColor('#000');
//
// create base UI tab and root window
//
var win = Ti.UI.createWindow({
backgroundColor:'#fff',
layout: 'vertical',
navBarHidden: true,
});
// icon grid
var icons = [
{ image: '/images/ico_generic.png', label: 'Hospital Locations', url: 'http://google.com' },
{ image: '/images/ico_generic.png', label: 'Tobacco Free Campus', url: 'http:://robwilkerson.org' },
{ image: '/images/ico_generic.png', label: 'ER Wait Times', url: 'http://letmegooglethatforyou.com' },
{ image: '/images/ico_generic.png', label: 'Make a Donation', url: 'http://flickr.com/photos/robwilkerson' },
{ image: '/images/ico_generic.png', label: 'Condition Search', url: 'http://facebook.com' },
{ image: '/images/ico_generic.png', label: 'Video Library', url: 'http://google.com/reader' },
{ image: '/images/ico_generic.png', label: 'Financial Help', url: 'http://stackoverflow.com' },
{ image: '/images/ico_generic.png', label: 'Patient Forms', url: 'http://github.com' }
];
// put the grid in a scrollable view
var iconGrid = Ti.UI.createScrollView({
layout: 'vertical',
});
// incoming properties we want customizable
var cols = 3;
var icoW = 57;
var icoH = 57;
// Grid
var xSpacer = 10; // horizontal space b/t icons
var ySpacer = 10; // vertical space b/t icons
var rows = Math.ceil( icons.length / cols ); // how many rows?
// Container width = 1/3 of the viewport minus the icon widths and spacers
var containerW = Math.floor( ( Ti.Platform.displayCaps.platformWidth - ( xSpacer * ( cols + 1 ) ) ) / 3 );
// Container height = icon height + label spacer + label height
var containerH = icoH + ySpacer + 15;
// Row height = icon height + top spacer + bottom spacer + label spacer + 15 (label height)
var rowH = containerH + ( 2 * ySpacer );
// Incrementing values
var i = 0;
var viewHeight = 0;
for( var y = 0; y < rows; y++ ) {
var thisRow = Ti.UI.createView({
className: 'grid',
layout: 'horizontal',
height: rowH,
touchEnabled: false,
});
viewHeight += rowH;
for( var x = 0; x < cols && i < icons.length; x++ ) {
var container = Ti.UI.createView({
left: xSpacer,
height: containerH,
top: ySpacer,
width: containerW,
});
var icon = Ti.UI.createImageView({
left: ( containerW - icoW ) / 2,
height: icoH,
image: icons[i].image,
top: 0,
width: icoW,
});
var label = Ti.UI.createLabel({
// borderColor: '#00f',
font: { fontSize: 12 },
height: 15,
text: icons[i].label,
textAlign: 'center',
top: icoH + ySpacer,
width: containerW,
});
icon.addEventListener( 'click', function( e ) {
alert( 'Icon ' + i + ' was clicked' );d
});
container.add( icon );
container.add( label );
thisRow.add( container );
i++;
}
iconGrid.add( thisRow );
iconGrid.height = viewHeight;
}
win.add( iconGrid );
win.open();
You can also apply an event listener to the "view" itself. The reason being is, if you constantly add the same event listener to every single view, you'll cause the device's memory to become smaller and smaller, especially in cases where you'll have a larger data set.
My suggestion to you is this:
Add your own property to the imageView, like an "id" or something. So something like:
Ti.UI.createImageView({image: 'path/to/image.png', id: 'array_key'});
Once you've done that, you can add an event listener to the parent view, in this case your imageView.
view.addEventListener('click', function(e) {
alert(e.source.id + ' was clicked');
});
That way you have one event listener that can handle all the imageView events.
This one's on me. In my learning process, I went through a couple of different solutions to display a grid of icons. In one of the early iterations, I had to disable touch for the row (it was a tableView attempt). Several iterations later I got the display right, but disabling touch access on the row killed my ability to "click" the icons.
I was so far down the road that I didn't even realize that property was still in place until a new set of eyes pointed it out to me. Once I removed that property on thisRow, the event listeners got bound properly.
I am adding some line of code. What I have done is like created the grid of images and when you click, you will be able to that image.
{
"body": [
{
"type": "photo",
"order": 1,
"photos": [
{
"thumbnail": "http://www.flower.com/version_2.0/files/photos/thumbnails/745178756-_-1331130219.jpg",
"photo": "http://www.flower.com/version_2.0/files/photos/745178756-_-1331130219.jpg"
},
{
"thumbnail": "http://www.flower.com/version_2.0/files/photos/thumbnails/58062938-_-1337463040.jpg",
"photo": "http://www.flower.com/version_2.0/files/photos/58062938-_-1337463040.jpg"
},
{
"thumbnail": "http://www.flower.com/version_2.0/files/photos/thumbnails/1368715237-_-1337463149.jpg",
"photo": "http://www.flower.comversion_2.0/files/photos/1368715237-_-1337463149.jpg"
},
]
},
],
"status": true
}
It was response I was getting from the server.
Now for Making it is in grid and for clickable image, I am going to paste the code below. Note grid is done for 320 px width.
var xhr = Ti.Network.createHTTPClient({
onload : function(e) {
var response = JSON.parse(this.responseText);
var myObjectString = JSON.stringify(response);
Titanium.API.info('myObjectString--->: ' + myObjectString)
var myArray = response.body;
var objectArray = [];
var k = 5;
for (var i = 0; i < myArray[0].photos.length/5; i++) {
var l = 0+i*5; var m = 0 for (var j = l; j < k; j++) {
var thumb = Ti.UI.createImageView({
image:myArray[0].photos[j].thumbnail,
largeImage:myArray[0].photos[j].photo,
height:60,
tag:j,
width:60,
top:5*(i+1)+60*i,
left:3*(m+1)+60*m,
});
objectArray.push(thumb);
m++;
scroll.add(thumb);
thumb.addEventListener('click' ,function(e)
{
for(var i =0;i<objectArray.length;i++)
{
if(e.source.tag==objectArray[i].tag)
{
var LargeImageView = Ti.UI.createWindow({
backButtonTitle:'Image',
barColor:'#000',
backgroundColor: '#fff',
backgroundImage:'./Images/background.png',
url:'/More/DetailsImage.js',
image:objectArray[i].largeImage,
ImageArray:objectArray,
index:i,
});
Titanium.UI.currentTab.open(LargeImageView,{animated:true,modal:true});
break;
}
}
}); } l=k+5; k=k+5;
} } });