In android development, the phase comes where the app needs specific settings to be modified manually by the user. So at that time developer directs the user to open specific settings and modify them. So in this article, it has been discussed how to open specific settings and make the user change them easily.
Step 1: Create a new Empty Activity android project
- While creating a new android studio project give the name of the project as “Open specific settings”.
- You may refer: Android | How to Create/Start a New Project in Android Studio?
- Select Kotlin as the language, and layout name as activity_main.xml.
Step 2: You may change the color combination of the application
- You may change the color combination of the base theme of the application. To change it open app -> src -> main -> res -> values -> colors.xml.
- Invoke the following code in colors.xml.
XML
<? xml version = "1.0" encoding = "utf-8" ?> < resources > < color name = "colorPrimary" >#0f9d58</ color > < color name = "colorPrimaryDark" >#006d2d</ color > < color name = "colorAccent" >#55cf86</ color > </ resources > |
- You may refer to the following image to get the colors.xml file.
Step 3: Working with the activity_main.xml
- Here, Buttons are used to open each of the specific settings. There are many different settings in android out of all of the seven that are most commonly used. So seven buttons are added to the activity layout.
- Invoke the following code inside activity_main.xml:
XML
<? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical" tools:context = ".MainActivity" tools:ignore = "HardcodedText" > <!--Make sure to give appropriate IDs to all buttons so that can be easily handled--> <!--Button to open wireless settings--> < Button android:id = "@+id/wireless_settings" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_margin = "8dp" android:backgroundTint = "@color/colorPrimary" android:text = "Open Wireless Settings" android:textColor = "@android:color/white" /> <!--Button to open wifi settings--> < Button android:id = "@+id/wifi_settings" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_margin = "8dp" android:backgroundTint = "@color/colorPrimary" android:text = "Open Wi-Fi Settings" android:textColor = "@android:color/white" /> <!--Button to open bluetooth settings--> < Button android:id = "@+id/bluetooth_settings" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_margin = "8dp" android:backgroundTint = "@color/colorPrimary" android:text = "Open Bluetooth Settings" android:textColor = "@android:color/white" /> <!--Button to open date settings--> < Button android:id = "@+id/date_settings" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_margin = "8dp" android:backgroundTint = "@color/colorPrimary" android:text = "Open Date Settings" android:textColor = "@android:color/white" /> <!--Button to open input method settings--> < Button android:id = "@+id/input_method_settings" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_margin = "8dp" android:backgroundTint = "@color/colorPrimary" android:text = "Open Input Method Settings" android:textColor = "@android:color/white" /> <!--Button to open display settings--> < Button android:id = "@+id/display_settings" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_margin = "8dp" android:backgroundTint = "@color/colorPrimary" android:text = "Open Display Settings" android:textColor = "@android:color/white" /> <!--Button to open Location settings--> < Button android:id = "@+id/location_settings" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_margin = "8dp" android:backgroundTint = "@color/colorPrimary" android:text = "Open Location Settings" android:textColor = "@android:color/white" /> </ LinearLayout > |
The following output UI is produced:
Step 4: Working with the MainActivity.kt file
- You may refer: Buttons in Kotlin to know how to handle the button clicks through setOnClickListener using Kotlin.
- In this case, implicit intents are used you may refer: Android | Implicit and Explicit Intents with Examples for implicit intents.
- Invoke the following code to handle all the buttons.
Kotlin
import android.content.Intent import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.provider.Settings.* import android.view.View import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super .onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Handle wireless settings button wireless_settings?.setOnClickListener(View.OnClickListener{ val i = Intent(ACTION_WIRELESS_SETTINGS) startActivity(i) }) // Handle wifi settings button wifi_settings?.setOnClickListener(View.OnClickListener{ val i = Intent(ACTION_WIFI_SETTINGS) startActivity(i) }) // Handle bluetooth settings button bluetooth_settings?.setOnClickListener(View.OnClickListener{ val i = Intent(ACTION_BLUETOOTH_SETTINGS) startActivity(i) }) // Handle date settings button date_settings?.setOnClickListener(View.OnClickListener{ val i = Intent(ACTION_DATE_SETTINGS) startActivity(i) }) // Handle input method settings button input_method_settings?.setOnClickListener(View.OnClickListener{ val i = Intent(ACTION_INPUT_METHOD_SETTINGS) startActivity(i) }) // Handle display settings button display_settings?.setOnClickListener(View.OnClickListener{ val i = Intent(ACTION_DISPLAY_SETTINGS) startActivity(i) }) // Handle location settings button location_settings?.setOnClickListener(View.OnClickListener{ val i = Intent(ACTION_LOCATION_SOURCE_SETTINGS) startActivity(i) }) } } |
Note: Make sure to import the android.provider.Settings package to import all the settings class in the project:
import android.provider.Settings.*