Wednesday 4 March 2015

Open links in browser from titanium web view

$.webview.url="http://www.w3schools.com/";
$.webview.addEventListener('beforeload', function(e) {
    if (e.url != $.webview.url) {
        e.bubble = false;
        $.webview.stopLoading();
        Ti.Platform.openURL(e.url);
    }
});

Monday 2 March 2015

Titanium - Pause and Resume events for android

win.addEventListener("open", function(e) {
    win.activity.addEventListener("resume", function() {

    });
    //Notice the pause event
    win.activity.addEventListener("pause", function() {

    });
});

Device Display informations Titanium

Ti.API.info('Ti.Platform.displayCaps.density: ' + Ti.Platform.displayCaps.density);
Ti.API.info('Ti.Platform.displayCaps.dpi: ' + Ti.Platform.displayCaps.dpi);
Ti.API.info('Ti.Platform.displayCaps.platformHeight: ' + Ti.Platform.displayCaps.platformHeight);
Ti.API.info('Ti.Platform.displayCaps.platformWidth: ' + Ti.Platform.displayCaps.platformWidth);
if((Ti.Platform.osname === 'iphone')||(Ti.Platform.osname === 'ipad')||(Ti.Platform.osname === 'android')){
  Ti.API.info('Ti.Platform.displayCaps.logicalDensityFactor: ' + Ti.Platform.displayCaps.logicalDensityFactor);
}
if(Ti.Platform.osname === 'android'){
  Ti.API.info('Ti.Platform.displayCaps.xdpi: ' + Ti.Platform.displayCaps.xdpi);
  Ti.API.info('Ti.Platform.displayCaps.ydpi: ' + Ti.Platform.displayCaps.ydpi);
}
http://docs.appcelerator.com/titanium/3.0/#!/api/Titanium.Platform.DisplayCaps

Tuesday 24 February 2015

Underline in Ti.UI.Label

index.js
function underline(_label, _word) {
    if (Ti.Platform.name === 'iPhone OS') {
        var text = _label.getText();
        var attr = Titanium.UI.iOS.createAttributedString({
            text : text,
            attributes : [{
                type : Titanium.UI.iOS.ATTRIBUTE_UNDERLINES_STYLE,
                value : Titanium.UI.iOS.ATTRIBUTE_UNDERLINE_STYLE_SINGLE,
                range : [text.indexOf(_word), _word.length]
            }]
        });
        _label.setAttributedString(attr); // Why doesn't this work?
    }

    if (Ti.Platform.osname === 'android') {
        var text = _label.text.split(_word);
        _label.setHtml(text[0] + '<u>' + _word + '</u>' + text[1]);
        _label.setText(undefined);
    }
}


// Now you can do this:
var label = Ti.UI.createLabel({
    text: 'This is a label with an underlined word in its text.'
});
underline(label, "underlined");
win.add(label)
Reference

Saturday 14 February 2015

Use Titanium Studio without internet. ( Bypass Titanium studio authentication )


To avoid authentication of titanium studio and open it offline :


1. Locate TitaniumStudio.ini (How to)

2. Add the following at the end of the file:


-Dtitanium.bypassAuthentication=true

Thursday 12 February 2015

Custom Toast Titanium

index.js
var titost = require('titoast');
titost.setMessages("New message for toast");
titost.showOnView($.index);
titoast.js
var currentView;
var toastShown = false;
var toastmsg = 'toast message variable not set.';
var duration =3000;

var container = Ti.UI.createView({
    height : Ti.UI.SIZE,
    width : Ti.UI.FILL,
    bottom : '0dp',
});

var labelContainer = Ti.UI.createView({
    height : Ti.UI.SIZE,
    width : '280dp',
    borderRadius : 5,
    backgroundColor : "#000",
    opacity : 0,
    top : '10dp',
    bottom : '10dp'
});

var label = Ti.UI.createLabel({
    left : "10dp",
    right : "10dp",
    top : '10dp',
    bottom : '10dp',
    height : Ti.UI.SIZE,
    color : "#FFF",
    textAlign : "center",
    font : {
        fontSize : 13,
        fontWeight : "bold"
    },
});

labelContainer.add(label);
container.add(labelContainer);

exports.setMessages = function (msg) {
    toastmsg=msg;
};

exports.showOnView = function(view) {
    if (!toastShown) {
        toastShown = true;
        Ti.API.info('shown');
        currentView = view;
        label.text = toastmsg;
        open();
        setTimeout(function() {
            close();
        }, duration);
    }
};

function open() {   
    currentView.add(container);
    labelContainer.animate({
        opacity : 0.8,
        duration : 250
    });
};

function close() {
    labelContainer.animate({
        opacity : 0,
        duration : 250
    }, function(_event) {
        toastShown=false;
        currentView.remove(container);
        currentView = null;
    });
};

Friday 16 January 2015

Image crop and resize

function cropFromMiddleAndResize(aBlob) {

    var newBlob = null;
    var px;
    var py;
    var width;
    var height;
    var mediawidth = aBlob.width;
    var mediaheight = aBlob.height;
    var resize = 150;

    if (mediawidth > mediaheight) {
        px = (mediawidth - mediaheight) / 2;
        py = 0;
        width = mediaheight;
        height = mediaheight;
    } else {
        py = (mediaheight - mediawidth) / 2;
        px = 0;
        width = mediawidth;
        height = mediawidth;
    }

    var tempimage = (OS_IOS) ? (aBlob.imageAsResized(mediawidth, mediaheight)) : (aBlob);
    //https://jira.appcelerator.org/browse/TIMOB-4865
    newBlob = tempimage.imageAsCropped({
        width : width,
        height : height,
        x : px,
        y : py
    });
    newBlob = newBlob.imageAsResized(resize, resize);

    return newBlob;

}