Kotlin Android Options Menu: Settings Menu→←

Shola Slick Akinrolie
3 min readNov 2, 2020

Kotlin as one of the fastest-growing programming languages in the world has deemed it fit to win the hearts of the mobile developers around the globe. This is due to its simplicity, conciseness, interoperability et al. Many developers have classified the amazing language as a language for everyone.
Here is a quick sample of how an “Android Options Menu” is quickly made with a few lines of code.

Android Options Menu is the collection of menu items for an activity. The options menu allows placing actions that impact globally on the application.

Options Menu is created by overriding the onCreateOptionsMenu() function in the MainActivity of the App. The menu resource is inflated by and calling the inflate() method of MenuInflater class. To act on menu items, override the onOptionsItemSelected() function.

In this article, we will add the options menu items on the action bar at the top-right of your Android App. Clicking on the menu shows the option menu items on which we can perform the relevant actions.
The most popular of the options is “Settings” where different App actions can be carried out.

Kotlin Android Options Menu

Create an android project and select the Basic Activity. This activity auto generates codes for menu option and Toolbar.

activity_main.xml
Add the following code in the activity_main.xml file in the layout directory. This code is auto-generated while creating Basic Activity.

content_main.xml

Add the following code in the content_main.xml file in the layout directory. In this layout, you can place your UI components.

strings.xml

Add the following code in the strings.xml file.

Sample:

<resources>
<string name=”app_name”>Kotlin Options Menu</string>
<string name=”action_settings”>Settings</string>
<string name=”action_share”>Share</string>
<string name=”action_exit”>Exit</string>
<string name=”action_exit”>Privacy</string>
<string name=”action_exit”>Help & Support</string>

</resources>

menu_main.xml

Add the following code in the menu_main.xml file in menu directory. Add the item tag which creates the menu item for options menu.

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.slickboss.raffledrawswagdeciderapp.MainActivity"
>
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never"
/>

<item
android:id="@+id/add_persons"55
android:orderInCategory="100"
android:title="@string/share"
app:showAsAction="never"
/>

<item
android:id="@+id/reset_settings"
android:orderInCategory="100"
android:title="@string/exit"
app:showAsAction="never"
/>
<item
android:id="@+id/about_settings"
android:orderInCategory="100"
android:title="@string/privacy"
app:showAsAction="never"
/>
<item
android:id="@+id/contact"
android:orderInCategory="100"
android:title="Help & Support"
app:showAsAction="never"
/>
</menu>

MainActivity.kt

Add the following code in the MainActivity.kt class. In this class, we override the function onCreateOptionsMenu() and call the inflate() of MenuInflater class which inflates the menu and adds items to the action bar.

To perform action on each items of options, menu overrides the onOptionsItemSelected() function.

override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
return when (item.itemId) {

R.id.action_settings -> {
/* Toast.makeText(applicationContext, "Reset Settings coming soon", Toast.LENGTH_LONG)
.show()*/
intent
= Intent(this, Settings::class.java)
startActivity(intent)
true
}

R.id.add_persons -> {
Toast.makeText(applicationContext, "Add Settings coming soon", Toast.LENGTH_LONG).show()
return true
}

R.id.reset_settings -> {
Toast.makeText(applicationContext, "Reset Settings coming soon", Toast.LENGTH_LONG).show()
return true
}

R.id.about_settings -> {
Toast.makeText(applicationContext, "About settings coming soon", Toast.LENGTH_LONG).show()
return true
}

R.id.contact -> {
Toast.makeText(applicationContext, "Contact coming soon", Toast.LENGTH_LONG).show()
return true
}

else -> super.onOptionsItemSelected(item)

// else -> R.id.settings -> true
}




}

Demo App Screenshot:

Demo Integration on Swag Decider App: Github Link Here

Catch up with Slick on Twitter @meetslick for new Articles on Kotlin.

--

--

Shola Slick Akinrolie

Simplifying Products and Technology for Developers and Users Consumption, Adoption and Happiness🔥• Software Engr • Developer Advocate •