SwarmOnline Menu Mobile
  • Home
  • About
  • Services
  • Portfolio
  • Blog
  • Contact

Recent Posts

  • PhoneGap and the TestFlight SDK – Taking it Further!
  • Integrating the TestFlight SDK into your PhoneGap Application
  • Retrieving References to Internal View Components
  • Launching Sencha Insights
  • Documenting your Sencha apps with JSDuck

Recent Comments

  • Stuart on Ext.ux.TouchCalendar – A Sencha Touch Calendar component
  • Stuart on Localising Sencha Touch and Ext JS applications with Ux.locale.Manager
  • Manuel Rodriguez on Localising Sencha Touch and Ext JS applications with Ux.locale.Manager
  • Remi Bloch on Ext.ux.TouchCalendar – A Sencha Touch Calendar component
  • Remi Bloch on Ext.ux.TouchCalendar – A Sencha Touch Calendar component

Archives

  • May 2013
  • March 2013
  • February 2013
  • December 2012
  • September 2012
  • August 2012
  • November 2011
  • October 2011
  • May 2011
  • March 2011
  • February 2011
  • January 2011
  • November 2010

Categories

  • Development Tips
  • Ext JS
  • News
  • Sencha Touch
  • Sencha Touch Extensions & Plugins
  • Web Design
  • Web Design & Development Blog

HOW TO: Dynamically Change Layout Based on the User’s Device with Sencha Touch

2
03 Mar
2011
DeviceSpecificLayout

Sencha Touch

Sencha Touch Extensions & Plugins

Web Design & Development Blog

Occasionally we will want to tailor the layout of our app to suit the device that the User is viewing it on. Generally we want to do this to take advantage of the increased screen real estate made available by devices such as tablets compared to the usual phone or iPod. For example, showing a list of search results alongside a map rather than having them one behind the other in a card layout.

Device Specific Views - Tutorial Files (11kb) (609)

or view the live demo (please use Safari, Google Chrome or your Smartphone!)

Demo – Dynamically Changing Layouts

Step 1 : Basic Card Layout

The theory behind achieving this is pretty simple. We decide if we’re on a phone or a tablet and then alter the defined ‘layout‘ config accordingly. We’ll set up a simple example with a list and a map in a card layout initially, with a button in a docked toolbar that will switch between them.

[javascript]

Ext.ns(‘SwarmOnline’);

SwarmOnline.CardPanel = Ext.extend(Ext.Panel, {

initComponent: function(){
var layoutCustom = ‘card’;

var itemCustom = [{
xtype: 'list',
itemTpl: '{firstName} {lastName}',
indexBar: true,
store: store,
disableSelection: true
}, {
xtype: 'map'
}];

var dockedItems = [{
dock: 'top',
xtype: 'toolbar',
items: [{
text: 'Switch',
handler: this.onSwitchTap,
scope: this
}] }];

Ext.apply(this, {
fullscreen: true,
layout: layoutCustom,
items: itemCustom,
dockedItems: dockedItems
});

SwarmOnline.CardPanel.superclass.initComponent.call(this);
},

onSwitchTap: function(){
this.setActiveItem(this.getActiveItem() === this.items.get(0) ? 1 : 0);
}
});

var cardPanel = new SwarmOnline.CardPanel();

[/javascript]

If you open the demo for this step (by clicking the above) you will see a list and then a map if you click the Switch button. That’s a great start and is how most apps would display such data but what if the user is using an iPad? That list will be stretched hugely in the x-axis and will probably look pretty naff, so why not show both?

Before we move on, note that we defined the items and layout config options in variables and then put them into the Ext.apply call, we did this so we can fiddle with them before doing the final configuration.

Step 2 : Dynamically Change the Layout

So what we want to do is display the list at one side of the screen and the map alongside it so they can be viewed simultaneously giving a much better experience to the user and also improving the appearance of the list greatly. To do this we add a new method to decide what sort of device we’re on and then alter the ‘layout‘ variable depending on the result. In this case if it isn’t a Phone/iPod then we’re going to make it use an hbox layout with a 1/3 flex.

[javascript highlight="4,11,26-30"]

initComponent: function(){
var layout = ‘card’;
var items = [{
flex: 1,
xtype: 'list',
itemTpl: '{firstName} {lastName}',
indexBar: true,
store: store,
disableSelection: true
}, {
flex: 3,
xtype: 'map'
}];

var dockedItems = [{
dock: 'top',
xtype: 'toolbar',
items: [{
text: 'Switch',
handler: this.onSwitchTap,
scope: this
}] }];

if (!this.isPhone()) {
layout = {
type: ‘hbox’,
align: ‘stretch’,
pack: ‘justify’
};
dockedItems = [];
}
}

[/javascript]

..and then the method to decide what device the user is on…

[javascript] …
isPhone: function(){
return Ext.is.Phone || Ext.is.iPod;
}
…
[/javascript]

As you can see on Line 4 and 11 we add a flex config option – this will be ignored if we are using a card layout so it can be set straight away no matter what the device. We then call our isPhone method, and if it isn’t a Phone/iPod then we change the value of the layout variable to be an hbox config (Line 26-30) and reset the dockedItems array so the Switch button doesn’t show. If you run this code (or click the Step 2 header) then you should see (assuming your on a PC/Mac and not a phone) the List and Map side by side.

So there we go, dynamic layouts based on the user’s device – easy!

Please post a comment or email us if you have any problems or suggestions!

Device Specific Views - Tutorial Files (11kb) (609)

or view the live demo (please use Safari, Google Chrome or your Smartphone!)

Demo – Dynamically Changing Layouts

 

Tweet

2
  1. john - Reply

    August 20, 2012 at 6:28 pm

    Not sure if you understand difference between dynamically changing layout and dynamic rendering based on enviroment?

    Because you are not in any way changing layout dynamically.
    You actually require to change layout of already rendered view to say that you able to change it dynamically , duh?
    And no , you can not do it. You only can create new view.

    • Andy - Reply

      August 20, 2012 at 11:57 pm

      Thanks for raising this point John. The purpose of this post is to provide an example to Sencha Touch 1 users on switching the layout depending on the device type displaying it.

      We could produce a similar result in Sencha Touch 2 by creating separate profiles for device specific UI.

Leave a Comment - Cancel reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

About SwarmOnline

Based in Glasgow’s West End, SwarmOnline provide web development, cross platform mobile app development and training services for local and international organisations. We have expert knowledge of Sencha technologies and have been Sencha Partners since 2011. We specialise in creating dynamic, innovative and practical solutions.

Meet the Team...

Newsletter

Subscribe to our newsletter and we’ll keep you up-to-date. We don't do spam.

Get in touch!

Email: [email protected]   
Skype: andrew-swarmonline stuart-swarmonline   
Phone: 0141 438 2231   


© 2013 SwarmOnline Ltd. All Rights Reserved.

SwarmOnline Ltd is a Limited Company registered in Scotland, with company number SC411633. Our Registered Office is 1-2 249 Byres Road, Glasgow G12 8UB.