Why menu option is not tapping when I created all the method?

I am trying to create a delete menu Icon when I tap on that it opens a dialog asking whether to delete this item from the list or not but when I am tapping on that nothing happens.

File where delete menu is present

UpdateFragment.kt

package com.example.roomtutorial1.fragments.update

import android.app.AlertDialog
import android.os.Bundle
import android.text.TextUtils
import android.view.*
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import com.example.roomtutorial1.R
import com.example.roomtutorial1.data.UserViewModel
import com.example.roomtutorial1.model.User
import kotlinx.android.synthetic.main.fragment_update.*
import kotlinx.android.synthetic.main.fragment_update.view.*


class UpdateFragment : Fragment() {


    private val args by navArgs<UpdateFragmentArgs>()
    private lateinit var mUserViewModel: UserViewModel

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment


        val view = inflater.inflate(R.layout.fragment_update, container, false)

        mUserViewModel = ViewModelProvider(this).get(UserViewModel::class.java)

        view.updateFirstName.setText(args.currentUser.firstName)
        view.updateLastName.setText(args.currentUser.lastName)
        view.updateAge.setText(args.currentUser.age.toString())

        view.updateBtn.setOnClickListener {
            updateItem()
        }

        //Set menu
        setHasOptionsMenu(true)

        return view
    }

    private fun updateItem() {
        val firstName = updateFirstName.text.toString()
        val lastName = updateLastName.text.toString()
        val age = updateAge.text.toString()
        if (!(TextUtils.isEmpty(firstName) && TextUtils.isEmpty(lastName) && age.isEmpty())) {
            //Create User Object

            val updateUser = User(args.currentUser.id, firstName, lastName, age.toInt())
            //Update current user

            mUserViewModel.updateUser(updateUser)
            //Navigate back

            findNavController().navigate(R.id.action_updateFragment_to_listFragment)
            Toast.makeText(requireContext(), "Updated Successfully", Toast.LENGTH_SHORT).show()

        } else {
            Toast.makeText(requireContext(), "Please fill all the fields", Toast.LENGTH_SHORT)
                .show()
        }


    }

    override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
        inflater.inflate(R.menu.delete_menu, menu)

    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if (item.itemId == R.menu.delete_menu) {
            deleteUser()
        }
        return super.onOptionsItemSelected(item)
    }



    private fun deleteUser() {                                 //delete function
        val builder = AlertDialog.Builder(requireContext())
        builder.setPositiveButton("Yes") { _, _ ->
            mUserViewModel.deleteUser(args.currentUser)
            Toast.makeText(
                requireContext(),
                "Successfully Deleted ${args.currentUser.firstName}",
                Toast.LENGTH_SHORT
            ).show()

        }
        builder.setNegativeButton("No") { _, _ -> }

        builder.setTitle("Delete ${args.currentUser.firstName}?")
        builder.setMessage("Are you sure you want to delete ${args.currentUser.firstName}")
        builder.create().show()
    }


}

delete_menu.xml

<?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/menu_delete"
        android:title="Delete"
        android:icon="@drawable/ic_delete_24"
        android:iconTint="@color/white"
        app:showAsAction="ifRoom"
        />

</menu>

Note: Comment which more files you need and I will edit the question.

Answer

You’re trying to use delete_menu to compare the itemId while calling deleteUser() function, while that’s the name of the XML not the itemId.

Try this:

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    when (item.itemId) {
        R.id.menu_delete -> {
            deleteUser()
            return true
        }
        else -> return super.onOptionsItemSelected(item)
    }
}

This should fix it.