Search code examples
appceleratorappcelerator-mobileappcelerator-titaniumappcelerator-alloy

Titanium appcelerator android and ios platform height in pixels and dp


I am about to test an app with the appcelerator titanium framework, i have to center a view on the screen :

my index.xml :

<Alloy>
<!-- Anddroid Window -->
<Window id="index" platform="android">
    <Require type="view" id="firstscreen" src="firstscreen"/>
</Window>

<!-- iOS Window -->
<NavigationWindow id="nav" platform="ios">
    <Window id="win1" backgroundColor="white">
        <Require type="view" id="firstscreen" src="firstscreen"/>
    </Window>
</NavigationWindow>

the firstscreen xml :

<Alloy>
	<ScrollView scrollingEnabled="false" contentWidth="Ti.UI.FILL">
		<ImageView class="fullBgImage" image="/images/login/bg2.png" />
		<View layout="vertical">
			<View id="loginView" class="heightAuto" layout="vertical">
				<ImageView id="localImage" image="/images/logo.png"  />
				<Label class="logoLabel" text="Karma" />
				<Label class="logoSlogan" text="Faits confiance à votre Karma pour booster votre carrière" />
				<View class="btn btnVert"  onClick="openCandidat"><Label class="btnLabel" text="Je recherche un job" /></View>
				<View class="btn btnBlanc" ><Label class="btnLabel bleu" text="Je suis recruteur" /></View>
			</View>
		</View>
	</ScrollView>
</Alloy>

The index.js : The formula to achieve is : viewTopPosition = (platformheight - viewheight)/2

if (OS_ANDROID) {
	$.index.addEventListener('open', after_win_load);
	$.index.open();
} else {
	$.nav.addEventListener('open', after_win_load);
	$.nav.open();
}

function after_win_load() {
	// Platform height
	var platformHeight = Ti.Platform.displayCaps.platformHeight;
	// The view
	var firstscreen = $.firstscreen;
	/* Taille du logo */
	firstscreenViewHeight = firstscreen.loginView.size;
	/* Centrer la vue verticalement */
	firstScreenTop = platformHeight - firstscreenViewHeight.height;
	//firstscreen.top = firstScreenTop;
	
	var style = $.createStyle({
		classes : 'firstScreenTop',
		apiName : 'View',
		top : firstScreenTop / 2
	});

	firstscreen.loginView.applyProperties(style);
}

On iOs it looks great, view is centered vertically, but in android the : Ti.Platform.displayCaps.platformHeight is a too big pixels value = 1920

on my tiapp.xml file i already specified :

<property name="ti.ui.defaultunit" type="string">dp</property>

I know that android is pixels unit but iOs use dp unit, so how i to achieve this please ? someone has an idea? For now for android i replaced the

var platformHeight = Ti.Platform.displayCaps.platformHeight;

to

var platformHeight = Ti.Platform.displayCaps.dpi;

But i ask myself if it will be good for all android screen resolution? and if this is a best practice?

Thank for your helps.


Solution

  •  var densityFactor = OS_IOS ? 1 : Ti.Platform.displayCaps.logicalDensityFactor;
    
     var platformHeight = Ti.Platform.displayCaps.platformHeight / densityFactor;
    

    or you can directly set the top to 50%

    var style = $.createStyle({
        classes : 'firstScreenTop',
        apiName : 'View',
        top : "50%"
    });