Open In App

How to Use Quick Settings Placement API in Android 13?

Improve
Improve
Like Article
Like
Save
Share
Report

Quick Settings tile is a quick setting that is present in the android device notification bar from where we can easily enable and disable the device or app settings such as Bluetooth or wifi. In this article, we will take a look at How to add a Quick Tile to our android device using Quick Settings Placement API. 

Step-By-Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.

Step 2: Updating SDK version in build.gradle file.

Navigate to Gradle Scripts>module level build.gradle file and add change compile SDK and target SDK to 33. After that simply sync your project to install it.

Step 3: Working with activity_main.xml.

Navigate to app>res>layout>activity_main.xml and add the below code to it. Comments are added in the code to get to know in detail. 

XML




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
 
    <!--creating button on add quick tile settings-->
    <Button
        android:id="@+id/idBtnQuickTile"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_margin="20dp"
        android:text="Add quick tile"
        android:textAlignment="center"
        android:textAllCaps="false"
        app:background="@color/edt_back_color"
        app:backgroundTint="@color/edt_back_color" />
</RelativeLayout>


Step 4: Create a new class for AppTileService. 

Navigate to app>java>your app’s package name > Right click on it > New > Java/Kotlin class and name it as AppTileService and add the below code to it. Comments are added in the code to get to know it in detail. 

Kotlin




import android.content.Intent
import android.os.Build
import android.service.quicksettings.Tile
import android.service.quicksettings.TileService
import androidx.annotation.RequiresApi
 
@RequiresApi(Build.VERSION_CODES.N)
class AppTileService : TileService() {
    // adding on start listening for tile service on below line.
    override fun onStartListening() {
        super.onStartListening()
        // on below line adding method to apply for tile.
        qsTile?.apply {
            state = Tile.STATE_ACTIVE
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                subtitle = "Open App"
            }
            // on below line calling method to update tile.
            updateTile()
        }
    }
 
    override fun onClick() {
        super.onClick()
        // on below line opening our apps main activity on clicking on the tile.
        startActivityAndCollapse(
            Intent(this, MainActivity::class.java).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        )
    }
}


Step 5: Working with MainActivity.kt file.

Navigate to app>java>your app’s package name>MainActivity.kt file and add the below code to it. Comments are added in the code to get to know it in detail. 

Kotlin




import android.R.attr
import android.annotation.SuppressLint
import android.app.StatusBarManager
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.graphics.drawable.Icon
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.MediaStore
import android.widget.Button
import android.widget.ImageView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
 
class MainActivity : AppCompatActivity() {
    // creating variables on below line.
    lateinit var quickTileBtn: Button
 
    @SuppressLint("WrongConstant")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        // initializing variables on below line.
        quickTileBtn = findViewById(R.id.idBtnQuickTile)
 
        // adding click listener for button on below line.
        quickTileBtn.setOnClickListener {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                // initializing status bar manager on below line.
                val statusBarManager =
                    getSystemService(Context.STATUS_BAR_SERVICE) as StatusBarManager
                // adding request and tile service for status bar manager on below line.
                statusBarManager.requestAddTileService(
                    // adding app's package components on below line.
                    ComponentName(
                        "com.example.gptapp",
                        "com.example.gptapp.AppTileService",
                    ),
                    // adding app name on below line.
                    resources.getString(R.string.app_name),
                    // adding app icon on below line.
                    Icon.createWithResource(this, R.mipmap.ic_launcher),
                    {},{}
                )
            } else {
                // on below line displaying toast message.
                Toast.makeText(this, "`requestAddTileService` can only be called in Android 13/Tiramisu.",Toast.LENGTH_SHORT,).show()
            }
        }
    }
}


Output:



Last Updated : 03 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads