Skip to content

Instantly share code, notes, and snippets.

@codeforfun-jp
Created August 25, 2022 09:15
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/a4214023411cf849d764bfa5fcfada04 to your computer and use it in GitHub Desktop.
Save codeforfun-jp/a4214023411cf849d764bfa5fcfada04 to your computer and use it in GitHub Desktop.
【Java】How to create a context menu 3
package com.example.contextmenusample;
import androidx.annotation.NonNull;
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;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
registerForContextMenu(findViewById(R.id.rootLayout));
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
getMenuInflater().inflate(R.menu.context_menu, menu);
}
@Override
public boolean onContextItemSelected(@NonNull MenuItem item) {
// switch (item.getItemId()) {
// R.id.action_home ->
// R.id.action_user ->
// R.id.action_map ->
// }
String message = "「" + item.getTitle() + "」が押されました。";
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment