Skip to content

Instantly share code, notes, and snippets.

@codeforfun-jp
Created August 25, 2022 08:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codeforfun-jp/6a3c2fc1463b7776b6fe1376f4b50718 to your computer and use it in GitHub Desktop.
Save codeforfun-jp/6a3c2fc1463b7776b6fe1376f4b50718 to your computer and use it in GitHub Desktop.
【Kotlin】How to create a context menu 3
package com.example.contextmenusample
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.ContextMenu
import android.view.MenuItem
import android.view.View
import android.widget.Toast
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
registerForContextMenu(findViewById(R.id.rootLayout))
}
override fun onCreateContextMenu(
menu: ContextMenu?,
v: View?,
menuInfo: ContextMenu.ContextMenuInfo?
) {
super.onCreateContextMenu(menu, v, menuInfo)
menuInflater.inflate(R.menu.context_menu, menu)
}
override fun onContextItemSelected(item: MenuItem): Boolean {
// when (item.itemId) {
// R.id.action_home ->
// R.id.action_user ->
// R.id.action_map ->
// }
Toast.makeText(this, "「${item.title}」が押されました。", Toast.LENGTH_SHORT).show()
return true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment