anvil
anvil copied to clipboard
Supporting multiple screens
People often ask how Anvil supports multiple screens. Currently it only provides an isPortrait
helper.
All "media queries" (in terms of CSS) end up with checking current Configuration
, which can be retrieved from Resources
assiciated with the current Context
:
if (getContext().getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
// portrait layout
} else {
// landscape layout
}
To keep Anvil simple we can remove isPortrait
and make users write media queries manually. Or we can find a minimal subset of media queries to satisfy most needs.
Configuration allows you to detect:
- Orientation (landscape, portrait)
- Screen size category (small, normal, large, xlarge)
- Screen DPI
- Screen physical size in inches
- Screen geometry (square, normal, long, or specific aspect ratio)
From the above I find the most practical to detect orientation and to tell normal (phone) layouts from large (tablet) layouts. This means that isPortrait
is here to stay, but the screen size could be checked with different functions:
-
isTablet()
,isTv()
,isPhone()
-
isLarge()
,isXLarge()
-
displaySize()
that would returns size in inches, e.g.if (displaySize() > 6.5) { tablet(); } else { phone(); }
etc, etc. What are your thoughts?
P.S. Anko supports lots of configuration media queries - https://github.com/Kotlin/anko/blob/master/doc/ADVANCED.md#configuration-qualifiers while Scaloid supports only a few - https://github.com/pocorall/scaloid/blob/master/scaloid-common/src/main/st/org/scaloid/util/Configuration.scala
Would love to see some included utilities. I would say that a function returning a simple enum of type
enum SCREEN_SIZE{
TABLET_PORTRAIT,
TABLET_LANDSCAPE,
PHONE_PORTRAIT ,
PHONE_LANDSCAPE,
TV_LANDSCAPE
TV_PORTRAIT // Even possible?
}
is enough for most layouts, since you could then even perform a switch-case with fallthrough and default. For other use cases I would introduce a helper which returns the AspectRatio.