Android on Chrome OS — Developer Toolbox

A handy toolbox of code-snippets to optimize your app for Chrome OS!

Emilie Roberts
Mar 16, 2018 · 5 min read

Since their introduction, Chromebooks have forged a strong identity as lightweight, easy-to-use, and secure browser-based laptops. With the ability to run Android, a huge ecosystem of applications and vast new functionality is now available on supported Chromebooks. While this is great news for developers, it comes with two important considerations:

  1. Developers have a responsibility to their users and to their brand to make their apps work well on Chrome OS.
  2. Developers have an opportunity to take advantage of large-format screens, keyboard/mouse input, stylus capabilities, and enhanced processing power while reaching a whole new population of educational, enterprise, and home users.

In most cases, Chrome OS optimization is easy and yields big rewards in usability and functionality for your users. As an added bonus, ensuring your app can handle keyboard and mouse input correctly can go a long way in making your app more Accessible to users who have difficulty with typical smartphone interfaces.

In this post, I present brief code-snippets for some of the most common Chrome OS specific optimizations to get your app looking great on Chrome OS. In future posts I will address some of these optimizations in more detail, like my post on Implementing Drag and Drop. More info about project setup and developing Android for Chrome OS can be found in the official Building Apps for Chrome OS documentation.

Was this post useful to you? Are there Chrome OS optimizations that I have missed here or that you would like me to go into more detail on? Please drop me a line!

Getting Started

Keyboard Input

Enter Key

Arrow-key and Tab-key Navigation

The default arrow key navigation mappings usually work as expected. If you need to specify these manually, use:

For the Tab key use:

Change Selected Item Highlight Color

In res/values/styles.xml under AppTheme

If you are working with ImageViews, you may not want the entire image to be selected when it is in focus. By using a selector, you can create a border drawable that only appears when an item is selected.

Create res/drawable/box_border.xml

And then set this drawable as the BackgroundResource for the View

Ctrl-based Shortcuts

The dispatchKeyShortcutEvent method will work for Ctrl- commands and, on Android O and later, for Alt- and Shift- commands as well. Replace KEYCODE_Z in the example below with the keycode you need.

If you want to specifically target a Ctrl- command and not include Shift- and Alt- commands for the same key, add an extra metaState check. See the hasModifiers documentation.

What about a meta-key combination? Here is what a Ctrl-Shift-z command looks like.

Mouse/Touchpad Input

Right Click

ContextMenu: See the Creating Contextual Menus documentation.

ContextClickListener: See the Right-Click Support documentation.

Tooltips / Hover Text

Hover Effects

Mouse Scroll Wheel Action

Drag and Drop

For more detailed information about setting this up and implementing the other half of drag and drop (the drag part), check out my post on Implementing Drag and Drop. Also see the official Drag and Drop documentation.


Maintaining State with Architecture Components

The code sample below demonstrates how to store and automatically update an integer (number of times a button has been clicked) but this same setup can also be used for complex data structures, media controllers, and more.

First extend ViewModel:

And then in onCreate of the main Activity:

Happy coding!

All code found here is licensed under the Apache 2.0 license. Nothing here is part of any official Google product.

Android Developers

The official Android Developers publication on Medium

Thanks to Daniel Galpin

Emilie Roberts

Written by

Partner Developer Advocate at Google. Canadian, vegan, roller derby athlete.

Android Developers

The official Android Developers publication on Medium

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade