Forbid click on a Share if it did not click on the Copy first

Cule :

My code reads like this:

public boolean onOptionsItemSelected(MenuItem item){

    switch (item.getItemId()) {

        case R.id.share:

            // send copy text
            Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
            sharingIntent.setType("text/plain");
            String shareBody = returnedText.getText().toString();
            String shareSub = "";
            sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, shareSub);
            sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
            startActivity(Intent.createChooser(sharingIntent, "Share using"));
            Toast.makeText(getApplicationContext(),getString(R.string.send_text), Toast.LENGTH_SHORT).show();

            break;

    }

    switch (item.getItemId()) {

        case R.id.copy:

            // copy text to clipboard
            ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
            clipboard.setText(returnedText.getText());
            Toast.makeText(getApplicationContext(),getString(R.string.copy_text), Toast.LENGTH_SHORT).show();

            break;

    }

    return super.onOptionsItemSelected(item);

}

What do I need? Forbid click on a Share if they did not click on the Copy first. If they did not click first to Copy, the app to inform the Toast message that they must Copy the text, and then, when they clicked on the Copy, the menu item to Share has its own function.

madhu s :

try this not sure if it works.

  1. make ClipboardManager global.
  2. in case R.id.share:

    if (clipboardManager.hasPrimaryClip()){
            // do what ever.
    }else {
            Toast.makeText(getApplicationContext(),"please click on copy first and try 
    again",Toast.LENGTH_LONG).show();
    }
    

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=167135&siteId=1