Android open Settings Panel programmatically

Open Settings Panel

1. Overview

In this article, we will learn to open the settings panel programmatically in Android. The Settings.Panel is introduced in Android Q (API 29).

2. Open Settings panel programmatically in Android

Consider we have an app that won’t work without Internet connectivity. So whenever the user opens the application without an internet connection, we will redirect them to the device settings app to turn ON Mobile Data or WiFi. However, the user has to leave our app to navigate to the settings app.

To address the above problem of users needing to leave the current app and open settings application, therefore Android introduces the Settings panel, a floating UI with a fixed subset of settings.

Right now, the Settings panel supports the following settings options:

  1. Internet connectivity
  2. NFC
  3. Volume
  4. WiFi

Let’s learn to show each of these options from your app.

2.1. Internet connectivity Settings panel

You can use the action ACTION_INTERNET_CONNECTIVITY to show the internet connectivity settings panel for users.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
     val panelIntent = Intent(Settings.Panel.ACTION_INTERNET_CONNECTIVITY)
     resultLauncher.launch(panelIntent)
}

2.2. Android NFC Settings panel

To launch the NFC settings panel, use the action ACTION_NFC:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
     val panelIntent = Intent(Settings.Panel.ACTION_NFC)
     resultLauncher.launch(panelIntent)
}

2.3. Android WiFi settings panel

You can use the WiFi settings panel to change the Wifi or toggle. To show the dialog, invoke intent with the action ACTION_WIFI.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
      val panelIntent = Intent(Settings.Panel.ACTION_WIFI)
      resultLauncher?.launch(panelIntent)
}

2.4. Android Volume Settings panel

To show the Volume settings panel to adjust the volume, use the action ACTION_VOLUME.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
     val panelIntent = Intent(Settings.Panel.ACTION_VOLUME)
     resultLauncher?.launch(panelIntent)
}

2.5. Receive callback using ActivityResultLauncher

Since startActivityForResult is deprecated, we are using the ActivityResultLauncher.launch to launch the intent. The registerForActivityResult reduces complexity such as managing request codes with the startActivityForResult.

First, we had to invoke the new method registerForActivityResult to create the ActivityResultLauncher. Once the instance ActivityResultLauncher is ready, we can then use it to launch the Intents.

For example, the following registerForActivityResult method handles callback from the Settings panel.

var resultLauncher = registerForActivityResult(
    ActivityResultContracts.StartActivityForResult()) { result ->
    if (result.resultCode == Activity.RESULT_OK) {
       // perform check whether Wifi \ NFC \ Internet connection \ Volume
    }
}

If the user presses the “DONE” button in the settings panel dialog or device back button, then the RESULT_CANCELLED result code is returned back to the Activity or Fragment’s ActivityResultLauncher callback method. Inside your callback method, you can write code to check the Wi-Fi \ NFC \ internet connectivity.

Settings Panel Android - Volume
Settings Panel Android - Volume
Settings Panel Android - NFC
Settings Panel Android - NFC
Internet connectivity settings
Internet connectivity settings panel
Settings panel Android - Wifi
Settings panel Android - Wifi

3. Conclusion

To sum up, the Settings panel ease things for users and enhance user experience. To learn more about Android, refer to our articles.

2 thoughts on “Android open Settings Panel programmatically

Leave a Reply

Your email address will not be published. Required fields are marked *