Fork me on GitHub

Sunday, September 1, 2013

Syntactic sugar for multiple device configuration

The most common question about Scaloid that I received is that how to adapt multiple device configuration.

Basically, a layout written in Scaloid is just an ordinary Scala code, so you can just freely composite the layout according to the device configuration:
if(width > 900 dip) STextView("The display is wider than 900 dips!")

This is simple, concise and intuitive in compared to XML description. Moreover, this promotes the 'responsive design', which adapts various screen dimensions with single layout description.

The only problem was that we have to manually prepare required variables, such as width in the example above. To address this issue, I wrote a package of helper methods that can be used to determine device configurations such as screen layout and resolution in dot-per-inch. All you need is just importing org.scaloid.util.Configuration._.
import org.scaloid.util.Configuration._

if(long) SButton("This button is shown only for a long screen "
  + "dimension ("+ width + ", " + height + ")")
if(landscape) this += new SLinearLayout {
  SButton("Buttons for")
  SButton("landscape layout")
  if(dpi >= HDPI) SButton("You have a high resolution display!")
}
The source code is pretty straightforward. Look at the source code to figure out how it works.

No comments:

Post a Comment