Skip to content

Instantly share code, notes, and snippets.

@tikitikipoo
Created January 5, 2012 14:37
Show Gist options
  • Save tikitikipoo/1565519 to your computer and use it in GitHub Desktop.
Save tikitikipoo/1565519 to your computer and use it in GitHub Desktop.
Androidにおけるメニューボタン押下時のメニューの表示サンプル
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.getItem(0).setEnabled(true);
menu.getItem(1).setEnabled(true);
menu.getItem(2).setEnabled(true);
menu.getItem(3).setEnabled(true);
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 0, 0, R.string.add_channel);
menu.add(0, 1, 1, R.string.channel_list);
menu.add(0, 2, 2, R.string.reloading);
menu.add(0, 3, 3, R.string.contact_author);
menu.getItem(0).setIcon(R.drawable.add_channel);
menu.getItem(1).setIcon(R.drawable.setting);
menu.getItem(2).setIcon(R.drawable.refresh);
menu.getItem(3).setIcon(R.drawable.about);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case 0:
// do something
break;
case 1:
Intent intent = new Intent();
intent.setClass(SampleActivity.this, Setting.class);
startActivity(intent);
break;
case 2:
// do something
break;
case 3:
new AlertDialog.Builder(SampleActivity.this)
.setMessage(getString(R.string.app_name) + " " + softVersion + "\n" + getString(R.string.author) + "")
.setIcon(R.drawable.ic_launcher)
.setTitle(getString(R.string.about))
.setPositiveButton(getString(R.string.report_problem), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"});
sendIntent.putExtra(Intent.EXTRA_TEXT, "write your suggestion here");
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "NewsReader issue report");
sendIntent.setType("message/rfc822");
startActivity(Intent.createChooser(sendIntent, "Title:"));
}
})
.setNeutralButton(getString(R.string.back), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.show();
break;
}
return super.onOptionsItemSelected(item);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment