In this tutorial, we’ll learn how to implement our own custom tiles in the quick settings.

This has been possible since Android Nougat. In our android application below, we’ll be creating a counter system which would keep a count of how many times you have opened the quick settings. It will display this in the Quick Setting Tile that we will implement.

Quick Settings Tile

The Android System provides a set of predefined tiles which are switches to easily access certain features in our smartphone. With an easy access to these switches, we can toggle features such as wifi, Bluetooth, mobile data, GPS etc.

Following is how it looks like:

android quick setting tiles

Clicking the sort button would show you the list of all tiles. You can drag the tiles into the top 10 visible tiles that would be shown in the notification tray.

The Quick Settings Tile is a new API that was released with Android Nougat.
A Tile has the following three states majorly:

  • STATE_ACTIVE
  • STATE_INACTIVE
  • STATE_UNAVAILABLE – the click action is disabled on the tile in this case

To create a tile in our android application we need to create a Service which extends TileService.
A TileService is a bound service. Hence it is tied to the Android System.

You need to add the TileService with the permissions in the AndroidManifest.xml file.

The TileService has four important methods:

  • onClick() – When the tile is clicked.
  • onStartListening() – When you open the notification tray
  • onStopListening() – When you close the notification tray.
  • onTileAdded() – When the tile is added into the Quick tiles. This happens when the application is installed for the first time. This is the place where you set the settings on the tile in its initial state.
  • onTileRemoved() – When the tile is removed from the Quick Tiles.

We must set the UI of the tile. A Tile has three important components:

After these have been set, you must call updateTile() on the Tile instance to update the UI in the Quick Settings.

We can launch activities by creating the intent method in the onClick() method of the TileService class.
startActivity just starts the activity but doesn’t close the notification tray holding the Quick Settings.
In order to close the tray, call startActivityAndCollapse method. It collapses the notification tray.

In the following section, we’ll be creating an android application which increments a counter variable each time you open the Quick Settings. It shows the count of the number of times you have opened the quick settings. A good way to keep a track of your productivity and how much many times you are opening the quick settings. We’ll store the count in SharedPreferences.

android-quick-settings-tile-project-structure (1)

Code

Update your AndroidManifest.xml with the following code:

Inside the Service above, we’ve set the icon and label for the Tile.
Inside the activity tag, we’ve set it to the single instance. This prevents creating multiple instances of the activity everytime startActivity is triggered.

The code for the MyTileService.java class is given below:

Inside onStartListening we check if the state is active or not. If it is, we update the counter and save it shared preferences.

Inside the onClick() we start the activity and dismiss the Quick Settings notification tray.

Tile tile = getQsTile(); is used to retrieve the Tile instance.

The code for the activity_main.xml layout is given below:

The code for the MainActivity.java class is given below:

Inside the onResume() we update the TextView with the latest counter value from SharedPreferences.
onNewIntent() gets called everytime startActivity is executed with the Activity already running.

The output of the application in action is given below:

android quick tile output

This brings an end to this tutorial. You can download the project from the link below.

By admin

Leave a Reply

%d bloggers like this: