How to change the Android Overflow menu icon programmatically using Kotlin?

Kotlin AndroidApps/ApplicationsMobile Development

This example demonstrates how to change the Android Overflow menu icon programmatically.

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Step 2 − Add the following code to res/layout/activity_main.xml.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
   android:orientation="vertical"
   android:padding="4dp"
   tools:context=".MainActivity">
   <androidx.appcompat.widget.Toolbar
      android:id="@+id/tootBar"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>
</LinearLayout>

Step 3 − Add the following code to src/MainActivity.kt

import android.graphics.Color
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
import androidx.appcompat.widget.Toolbar
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
   lateinit var toolBar: Toolbar
   override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_main)
      title = "KotlinApp"
      toolBar = findViewById(R.id.tootBar)
      toolBar.title = "MyToolBar"
      toolBar.setTitleTextColor(Color.WHITE)
   }
   override fun onCreateOptionsMenu(menu: Menu?): Boolean {
      val inflater = menuInflater
      inflater.inflate(R.menu.menu, menu)
      return true
   }
   override fun onOptionsItemSelected(item: MenuItem): Boolean {
      return when (item.itemId) {
         R.id.option1 -> {
            Toast.makeText(
               applicationContext, "Bluetooth Option Selected",
               Toast.LENGTH_SHORT
            ).show()
            true
         }
         R.id.option2 -> {
            Toast.makeText(applicationContext, "Call Option Selected", Toast.LENGTH_SHORT)
            .show()
            true
         }
         R.id.option3 -> {
            Toast.makeText(applicationContext, "About Option Selected", Toast.LENGTH_SHORT)
            .show()
            true
         }
         R.id.option4 -> {
            Toast.makeText(applicationContext, "Chat Option Selected", Toast.LENGTH_SHORT)
            .show()
            true
         }
         else -> super.onOptionsItemSelected(item)
      }
   }
   override fun onPrepareOptionsMenu(menu: Menu): Boolean {
      invalidateOptionsMenu()
      menu.findItem(R.id.option2).isVisible = false
      menu.findItem(R.id.option4).isVisible = true
      return super.onPrepareOptionsMenu(menu)
   }
}

Step 4 − Create a Android Resource directory (menu) ⇒ Create a menu resource file and add the following code −

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto">
   <item
      android:id="@+id/option1"
      android:orderInCategory="1"
      android:title="Bluetooth"
      android:icon="@drawable/ic_baseline_bluetooth_24"
      app:showAsAction="ifRoom" />
   <item
      android:id="@+id/option2"
      android:orderInCategory="2"
      android:title="Call"
      android:icon="@drawable/ic_baseline_call_24"
      app:showAsAction="ifRoom|collapseActionView" />
   <item android:id="@+id/option4"
      android:title="Chat"
      android:visible="false"
      android:orderInCategory="3"
      android:icon="@drawable/ic_baseline_chat_24"
      app:showAsAction="ifRoom|collapseActionView"/>
   <item
      android:id="@+id/option3"
      android:title="About"
      app:showAsAction="never" />
</menu>

Step 5 − Add the following code to androidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.q11">
   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:supportsRtl="true"
      android:theme="@style/AppTheme">
      <activity android:name=".MainActivity">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>

Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click the Run Play Icon icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen

raja
Published on 18-Aug-2020 12:24:07
Advertisements