List of usage examples for android.view ContextMenu setHeaderTitle
public ContextMenu setHeaderTitle(CharSequence title);
From source file:net.nightwhistler.pageturner.activity.ReadingFragment.java
@Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { // This is a hack to give the longclick handler time // to find the word the user long clicked on. if (this.selectedWord != null) { final CharSequence word = this.selectedWord.getText(); final int startIndex = this.selectedWord.getStartOffset(); final int endIndex = this.selectedWord.getEndOffset(); String header = String.format(getString(R.string.word_select), selectedWord.getText()); menu.setHeaderTitle(header); if (isDictionaryAvailable()) { android.view.MenuItem item = menu.add(getString(R.string.dictionary_lookup)); item.setOnMenuItemClickListener(new OnMenuItemClickListener() { @Override//w w w. j av a2 s . c o m public boolean onMenuItemClick(android.view.MenuItem item) { lookupDictionary(word.toString()); return true; } }); } menu.add(R.string.highlight).setOnMenuItemClickListener(new OnMenuItemClickListener() { @Override public boolean onMenuItemClick(android.view.MenuItem menuItem) { highLight(startIndex, endIndex, word.toString()); return false; } }); android.view.MenuItem newItem = menu.add(getString(R.string.wikipedia_lookup)); newItem.setOnMenuItemClickListener(new OnMenuItemClickListener() { @Override public boolean onMenuItemClick(android.view.MenuItem item) { lookupWikipedia(word.toString()); return true; } }); android.view.MenuItem newItem3 = menu.add(getString(R.string.lookup_wiktionary)); newItem3.setOnMenuItemClickListener(new OnMenuItemClickListener() { @Override public boolean onMenuItemClick(android.view.MenuItem item) { lookupWiktionary(word.toString()); return true; } }); android.view.MenuItem newItem2 = menu.add(getString(R.string.google_lookup)); newItem2.setOnMenuItemClickListener(new OnMenuItemClickListener() { @Override public boolean onMenuItemClick(android.view.MenuItem item) { lookupGoogle(word.toString()); return true; } }); this.selectedWord = null; } }
From source file:net.zorgblub.typhon.fragment.ReadingFragment.java
@Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { // This is a hack to give the longclick handler time // to find the word the user long clicked on. if (this.selectedWord != null && !this.config.isRikaiEnabled()) { final CharSequence word = this.selectedWord.getText(); final int startIndex = this.selectedWord.getStartOffset(); final int endIndex = this.selectedWord.getEndOffset(); String header = String.format(getString(R.string.word_select), selectedWord.getText()); menu.setHeaderTitle(header); if (isDictionaryAvailable()) { android.view.MenuItem item = menu.add(getString(R.string.dictionary_lookup)); onMenuPress(item).thenDo(() -> lookupDictionary(word.toString())); }//from ww w .jav a 2 s . com menu.add(R.string.highlight).setOnMenuItemClickListener(item -> { highLight(startIndex, endIndex, word.toString()); return false; }); android.view.MenuItem lookUpWikipediaItem = menu.add(getString(R.string.wikipedia_lookup)); onMenuPress(lookUpWikipediaItem).thenDo(() -> lookupWikipedia(word.toString())); android.view.MenuItem lookUpWiktionaryItem = menu.add(getString(R.string.lookup_wiktionary)); lookUpWiktionaryItem.setOnMenuItemClickListener(item -> { lookupWiktionary(word.toString()); return true; }); android.view.MenuItem lookupGoogleItem = menu.add(getString(R.string.google_lookup)); lookupGoogleItem.setOnMenuItemClickListener(item -> { lookupGoogle(word.toString()); return true; }); this.selectedWord = null; } }
From source file:com.aujur.ebookreader.activity.ReadingFragment.java
@Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { // This is a hack to give the longclick handler time // to find the word the user long clicked on. if (this.selectedWord != null) { final CharSequence word = this.selectedWord.getText(); final int startIndex = this.selectedWord.getStartOffset(); final int endIndex = this.selectedWord.getEndOffset(); String header = String.format(getString(R.string.word_select), selectedWord.getText()); menu.setHeaderTitle(header); if (isDictionaryAvailable()) { android.view.MenuItem item = menu.add(getString(R.string.dictionary_lookup)); item.setOnMenuItemClickListener(new OnMenuItemClickListener() { @Override//from w w w . j a v a 2 s .c o m public boolean onMenuItemClick(android.view.MenuItem item) { lookupDictionary(word.toString()); return true; } }); } menu.add(R.string.highlight).setOnMenuItemClickListener(new OnMenuItemClickListener() { @Override public boolean onMenuItemClick(android.view.MenuItem menuItem) { highLight(startIndex, endIndex, word.toString()); return false; } }); android.view.MenuItem newItem = menu.add(getString(R.string.wikipedia_lookup)); newItem.setOnMenuItemClickListener(new OnMenuItemClickListener() { @Override public boolean onMenuItemClick(android.view.MenuItem item) { lookupWikipedia(word.toString()); return true; } }); android.view.MenuItem newItem3 = menu.add(getString(R.string.lookup_wiktionary)); newItem3.setOnMenuItemClickListener(new OnMenuItemClickListener() { @Override public boolean onMenuItemClick(android.view.MenuItem item) { lookupWiktionary(word.toString()); return true; } }); android.view.MenuItem newItem2 = menu.add(getString(R.string.google_lookup)); newItem2.setOnMenuItemClickListener(new OnMenuItemClickListener() { @Override public boolean onMenuItemClick(android.view.MenuItem item) { lookupGoogle(word.toString()); return true; } }); android.view.MenuItem newItem4 = menu.add(getString(R.string.direitonet_lookup)); newItem4.setOnMenuItemClickListener(new OnMenuItemClickListener() { @Override public boolean onMenuItemClick(android.view.MenuItem item) { lookupLawDictionary(word.toString()); return true; } }); this.selectedWord = null; } }
From source file:com.bernard.beaconportal.activities.activity.MessageList.java
@Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo; if (v == getListView()) { if (info.position > mAdapter_Accounts.getCount() + 5) { getMenuInflater().inflate(R.menu.folder_context, menu); FolderInfoHolder folder = (FolderInfoHolder) mAdapter .getItem(info.position - (mAdapter_Accounts.getCount() + 6)); menu.setHeaderTitle(folder.displayName); } else if (info.position < mAdapter_Accounts.getCount()) { menu.setHeaderTitle(R.string.accounts_context_menu_title); if (mAdapter_Accounts == null) { Log.d("info =", "mAdapter_Accounts = null"); }// ww w . j a v a 2 s . c o m BaseAccount account = (BaseAccount) mergeadapter.getItem(info.position); if ((account instanceof Account) && !((Account) account).isEnabled()) { getMenuInflater().inflate(R.menu.disabled_accounts_context, menu); } else { getMenuInflater().inflate(R.menu.accounts_context, menu); } if (account instanceof SearchAccount) { for (int i = 0; i < menu.size(); i++) { android.view.MenuItem item = menu.getItem(i); item.setVisible(false); } } else { EnumSet<ACCOUNT_LOCATION> accountLocation = accountLocation(account); if (accountLocation.contains(ACCOUNT_LOCATION.TOP)) { menu.findItem(R.id.move_up).setEnabled(false); } else { menu.findItem(R.id.move_up).setEnabled(true); } if (accountLocation.contains(ACCOUNT_LOCATION.BOTTOM)) { menu.findItem(R.id.move_down).setEnabled(false); } else { menu.findItem(R.id.move_down).setEnabled(true); } } } } }
From source file:com.github.dfa.diaspora_android.ui.ContextMenuWebView.java
@Override protected void onCreateContextMenu(ContextMenu menu) { super.onCreateContextMenu(menu); HitTestResult result = getHitTestResult(); MenuItem.OnMenuItemClickListener handler = new MenuItem.OnMenuItemClickListener() { public boolean onMenuItemClick(MenuItem item) { HitTestResult result = getHitTestResult(); String url = result.getExtra(); switch (item.getItemId()) { //Save image to external memory case ID_SAVE_IMAGE: { boolean writeToStoragePermitted = true; if (android.os.Build.VERSION.SDK_INT >= 23) { int hasWRITE_EXTERNAL_STORAGE = parentActivity .checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE); if (hasWRITE_EXTERNAL_STORAGE != PackageManager.PERMISSION_GRANTED) { writeToStoragePermitted = false; if (!parentActivity.shouldShowRequestPermissionRationale( Manifest.permission.WRITE_EXTERNAL_STORAGE)) { new AlertDialog.Builder(parentActivity).setMessage(R.string.permissions_image) .setPositiveButton(context.getText(android.R.string.yes), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (android.os.Build.VERSION.SDK_INT >= 23) parentActivity.requestPermissions(new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, MainActivity.REQUEST_CODE__ACCESS_EXTERNAL_STORAGE); } }) .setNegativeButton(context.getText(android.R.string.no), null).show(); }//from w ww .j a va 2s .c om parentActivity.requestPermissions( new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, MainActivity.REQUEST_CODE__ACCESS_EXTERNAL_STORAGE); } } if (writeToStoragePermitted) { if (url != null) { Uri source = Uri.parse(url); DownloadManager.Request request = new DownloadManager.Request(source); File destinationFile = new File(Environment.getExternalStorageDirectory() + "/Pictures/Diaspora/" + System.currentTimeMillis() + ".png"); request.setDestinationUri(Uri.fromFile(destinationFile)); ((DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE)).enqueue(request); Toast.makeText(context, context.getText(R.string.share__toast_saved_image_to_location) + " " + destinationFile.getAbsolutePath(), Toast.LENGTH_LONG).show(); } } } break; case ID_SHARE_IMAGE: if (url != null) { boolean writeToStoragePermitted = true; if (android.os.Build.VERSION.SDK_INT >= 23) { int hasWRITE_EXTERNAL_STORAGE = parentActivity .checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE); if (hasWRITE_EXTERNAL_STORAGE != PackageManager.PERMISSION_GRANTED) { writeToStoragePermitted = false; if (!parentActivity.shouldShowRequestPermissionRationale( Manifest.permission.WRITE_EXTERNAL_STORAGE)) { new AlertDialog.Builder(parentActivity).setMessage(R.string.permissions_image) .setPositiveButton(context.getText(android.R.string.yes), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (android.os.Build.VERSION.SDK_INT >= 23) parentActivity.requestPermissions(new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, MainActivity.REQUEST_CODE__ACCESS_EXTERNAL_STORAGE); } }) .setNegativeButton(context.getText(android.R.string.no), null).show(); } else { parentActivity.requestPermissions( new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, MainActivity.REQUEST_CODE__ACCESS_EXTERNAL_STORAGE); } } } if (writeToStoragePermitted) { final Uri local = Uri.parse(Environment.getExternalStorageDirectory() + "/Pictures/Diaspora/" + System.currentTimeMillis() + ".png"); new ImageDownloadTask(null, local.getPath()) { @Override protected void onPostExecute(Bitmap result) { Uri myUri = Uri.fromFile(new File(local.getPath())); Intent sharingIntent = new Intent(); sharingIntent.setAction(Intent.ACTION_SEND); sharingIntent.putExtra(Intent.EXTRA_STREAM, myUri); sharingIntent.setType("image/png"); sharingIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); context.startActivity(Intent.createChooser(sharingIntent, getResources().getString(R.string.action_share_dotdotdot))); } }.execute(url); } } else { Toast.makeText(context, "Cannot share image: url is null", Toast.LENGTH_SHORT).show(); } break; case ID_IMAGE_EXTERNAL_BROWSER: if (url != null) { Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); context.startActivity(intent); } break; //Copy url to clipboard case ID_COPY_LINK: if (url != null) { ClipboardManager clipboard = (ClipboardManager) context .getSystemService(Context.CLIPBOARD_SERVICE); clipboard.setPrimaryClip(ClipData.newPlainText("text", url)); Toast.makeText(context, R.string.share__toast_link_address_copied, Toast.LENGTH_SHORT) .show(); } break; //Try to share link to other apps case ID_SHARE_LINK: if (url != null) { Intent sendIntent = new Intent(); sendIntent.setAction(Intent.ACTION_SEND); sendIntent.putExtra(Intent.EXTRA_TEXT, url); sendIntent.setType("text/plain"); context.startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.context_menu_share_link))); } break; } return true; } }; //Build context menu if (result.getType() == HitTestResult.IMAGE_TYPE || result.getType() == HitTestResult.SRC_IMAGE_ANCHOR_TYPE) { // Menu options for an image. menu.setHeaderTitle(result.getExtra()); menu.add(0, ID_SAVE_IMAGE, 0, context.getString(R.string.context_menu_save_image)) .setOnMenuItemClickListener(handler); menu.add(0, ID_IMAGE_EXTERNAL_BROWSER, 0, context.getString(R.string.context_menu_open_external_browser)) .setOnMenuItemClickListener(handler); menu.add(0, ID_SHARE_IMAGE, 0, context.getString(R.string.context_menu_share_image)) .setOnMenuItemClickListener(handler); } else if (result.getType() == HitTestResult.ANCHOR_TYPE || result.getType() == HitTestResult.SRC_ANCHOR_TYPE) { // Menu options for a hyperlink. menu.setHeaderTitle(result.getExtra()); menu.add(0, ID_COPY_LINK, 0, context.getString(R.string.context_menu_copy_link)) .setOnMenuItemClickListener(handler); menu.add(0, ID_SHARE_LINK, 0, context.getString(R.string.context_menu_share_link)) .setOnMenuItemClickListener(handler); } }
From source file:com.github.dfa.diaspora_android.web.ContextMenuWebView.java
@Override protected void onCreateContextMenu(ContextMenu menu) { super.onCreateContextMenu(menu); HitTestResult result = getHitTestResult(); MenuItem.OnMenuItemClickListener handler = new MenuItem.OnMenuItemClickListener() { public boolean onMenuItemClick(MenuItem item) { HitTestResult result = getHitTestResult(); String url = result.getExtra(); switch (item.getItemId()) { //Save image to external memory case ID_SAVE_IMAGE: { boolean writeToStoragePermitted = true; if (android.os.Build.VERSION.SDK_INT >= 23) { int hasWRITE_EXTERNAL_STORAGE = parentActivity .checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE); if (hasWRITE_EXTERNAL_STORAGE != PackageManager.PERMISSION_GRANTED) { writeToStoragePermitted = false; if (!parentActivity.shouldShowRequestPermissionRationale( Manifest.permission.WRITE_EXTERNAL_STORAGE)) { new AlertDialog.Builder(parentActivity).setMessage(R.string.permissions_image) .setPositiveButton(context.getText(android.R.string.yes), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (android.os.Build.VERSION.SDK_INT >= 23) parentActivity.requestPermissions(new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, MainActivity.REQUEST_CODE__ACCESS_EXTERNAL_STORAGE); } }) .setNegativeButton(context.getText(android.R.string.no), null).show(); }// ww w . ja v a2 s .com parentActivity.requestPermissions( new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, MainActivity.REQUEST_CODE__ACCESS_EXTERNAL_STORAGE); } } if (writeToStoragePermitted) { if (url != null) { Uri source = Uri.parse(url); DownloadManager.Request request = new DownloadManager.Request(source); File destinationFile = new File(Environment.getExternalStorageDirectory() + "/Pictures/Diaspora/" + System.currentTimeMillis() + ".png"); request.setDestinationUri(Uri.fromFile(destinationFile)); ((DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE)).enqueue(request); Toast.makeText(context, context.getText(R.string.share__toast_saved_image_to_location) + " " + destinationFile.getAbsolutePath(), Toast.LENGTH_LONG).show(); } } } break; case ID_SHARE_IMAGE: if (url != null) { boolean writeToStoragePermitted = true; if (android.os.Build.VERSION.SDK_INT >= 23) { int hasWRITE_EXTERNAL_STORAGE = parentActivity .checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE); if (hasWRITE_EXTERNAL_STORAGE != PackageManager.PERMISSION_GRANTED) { writeToStoragePermitted = false; if (!parentActivity.shouldShowRequestPermissionRationale( Manifest.permission.WRITE_EXTERNAL_STORAGE)) { new AlertDialog.Builder(parentActivity).setMessage(R.string.permissions_image) .setPositiveButton(context.getText(android.R.string.yes), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (android.os.Build.VERSION.SDK_INT >= 23) parentActivity.requestPermissions(new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, MainActivity.REQUEST_CODE__ACCESS_EXTERNAL_STORAGE); } }) .setNegativeButton(context.getText(android.R.string.no), null).show(); } else { parentActivity.requestPermissions( new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, MainActivity.REQUEST_CODE__ACCESS_EXTERNAL_STORAGE); } } } if (writeToStoragePermitted) { final Uri local = Uri.parse(Environment.getExternalStorageDirectory() + "/Pictures/Diaspora/" + System.currentTimeMillis() + ".png"); new ImageDownloadTask(null, local.getPath()) { @Override protected void onPostExecute(Bitmap result) { Uri myUri = Uri.fromFile(new File(local.getPath())); Intent sharingIntent = new Intent(); sharingIntent.setAction(Intent.ACTION_SEND); sharingIntent.putExtra(Intent.EXTRA_STREAM, myUri); sharingIntent.setType("image/png"); sharingIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); context.startActivity(Intent.createChooser(sharingIntent, getResources().getString(R.string.action_share_dotdotdot))); } }.execute(url); } } else { Toast.makeText(context, "Cannot share image: url is null", Toast.LENGTH_SHORT).show(); } break; case ID_IMAGE_EXTERNAL_BROWSER: if (url != null) { Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); context.startActivity(intent); } break; //Copy url to clipboard case ID_COPY_LINK: if (url != null) { ClipboardManager clipboard = (ClipboardManager) context .getSystemService(Context.CLIPBOARD_SERVICE); clipboard.setPrimaryClip(ClipData.newPlainText("text", url)); Toast.makeText(context, R.string.share__toast_link_address_copied, Toast.LENGTH_SHORT) .show(); } break; //Try to share link to other apps case ID_SHARE_LINK: if (url != null) { Intent sendIntent = new Intent(); sendIntent.setAction(Intent.ACTION_SEND); sendIntent.putExtra(Intent.EXTRA_TEXT, url); sendIntent.setType("text/plain"); context.startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.context_menu_share_link))); } break; } return true; } }; //Build context menu if (result.getType() == HitTestResult.IMAGE_TYPE || result.getType() == HitTestResult.SRC_IMAGE_ANCHOR_TYPE) { // Menu options for an image. menu.setHeaderTitle(result.getExtra()); menu.add(0, ID_SAVE_IMAGE, 0, context.getString(R.string.context_menu_save_image)) .setOnMenuItemClickListener(handler); menu.add(0, ID_IMAGE_EXTERNAL_BROWSER, 0, context.getString(R.string.context_menu_open_external_browser)) .setOnMenuItemClickListener(handler); menu.add(0, ID_SHARE_IMAGE, 0, context.getString(R.string.context_menu_share_image)) .setOnMenuItemClickListener(handler); } else if (result.getType() == HitTestResult.ANCHOR_TYPE || result.getType() == HitTestResult.SRC_ANCHOR_TYPE) { // Menu options for a hyperlink. menu.setHeaderTitle(result.getExtra()); menu.add(0, ID_COPY_LINK, 0, context.getString(R.string.context_menu_copy_link)) .setOnMenuItemClickListener(handler); menu.add(0, ID_SHARE_LINK, 0, context.getString(R.string.context_menu_share_link)) .setOnMenuItemClickListener(handler); } }