Skip to content

Instantly share code, notes, and snippets.

@manumaticx
Last active July 11, 2020 14:07
Show Gist options
  • Save manumaticx/c173974097c36d08eedc to your computer and use it in GitHub Desktop.
Save manumaticx/c173974097c36d08eedc to your computer and use it in GitHub Desktop.
Fading ActionBar in Titanium

Fading Actionbar

This is a quick example of how to create a fading actionbar effect like this in Appcelerator Titanium

fadingactionbar

How this works

This is actually very simple. The trick is putting the Actionbar in overlay mode by configuring the theme with windowActionBarOverlay:true. Then all you need to do is updating the color of the Actionbar, in this case by scrolling a ScrollView.

License

The MIT License (MIT)

Copyright (c) 2015 Manuel Lehner

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
var abx = require('com.alcoapps.actionbarextras');
function onOpen(e) {
// initial state: full transparency
abx.setBackgroundColor('#00009688');
}
function onScroll(e){
// Okay, let me explain what's going on here:
var alpha = "";
// I want to apply the fading effect to the first 500 pixels of scrolling
// So, I'm only setting the alpha channel for 0 <= y < 500
if (e.y < 500 && e.y >= 0){
//calculate the opacity factor and convert it to hexadecimal
alpha = (Math.round(e.y / 500 * 255).toString(16));
// add leading zero if needed
(alpha.length === 1) && (alpha = '0' + alpha);
}
// update actionbar color
abx.setBackgroundColor('#'+ alpha +'009688');
}
$.index.open();
".container": {
title: "Fading Actionbar",
theme: 'Theme.OverlayActionbar',
backgroundColor:"white"
},
".verticalScroll":{
scrollType: 'vertical',
layout: 'vertical',
top: 0,
width: Ti.UI.FILL,
height: Ti.UI.SIZE
},
".header":{
width: Titanium.UI.FILL,
height: 300,
backgroundImage: '/images/night-clouds.jpg'
},
".title":{
color: '#000',
width: Titanium.UI.FILL,
height: Titanium.UI.SIZE,
top: 12,
left: 8,
font: {
fontSize: '20dp',
fontFamily: 'Roboto',
fontWeight: 'bold'
}
},
".text":{
color: '#000',
width: Titanium.UI.FILL,
height: Titanium.UI.SIZE,
top: 8,
left: 8,
right: 8,
font: {
fontSize: '16dp',
fontFamily: 'Roboto'
}
}
<Alloy>
<Window class="container" onOpen="onOpen">
<ScrollView class="verticalScroll" onScroll="onScroll">
<View class="header" />
<Label class="title" text="Lorem Ipsum" />
<Label class="text">
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.
Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.
Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis.
At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, At accusam aliquyam diam diam dolore dolores duo eirmod eos erat, et nonumy sed tempor et et invidunt justo labore Stet clita ea et gubergren, kasd magna no rebum. sanctus sea sed takimata ut vero voluptua. est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur
</Label>
</ScrollView>
</Window>
</Alloy>
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="Theme.OverlayActionbar" parent="@style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowActionBarOverlay">true</item>
<!-- Support library compatibility -->
<item name="windowActionBarOverlay">true</item>
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primaryDark</item>
</style>
<color name="primary">#009688</color>
<color name="primaryDark">#00796B</color>
</resources>
@chmiiller
Copy link

Supercool! Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment