javaandroidandroid-studioandroid-toastclipboardmanager

How to display the clipboard copied text inside a toast


I'm new to Android. I want to display the copied coupon code.

Here is code:

ClipboardManager clipboard = (ClipboardManager) mCtx.getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("Code", artist.getCoupon_code());
clipboard.setPrimaryClip(clip);
Toast.makeText(mCtx, "coupon code: "+clip+" is copied" , Toast.LENGTH_SHORT).show();

How to remove the red-underlined text and only display the coupon code i.e. green underlined text.

output:
output


Solution

  • The string you want may be found in

    clip.getItemAt(0).getText();
    

    or

    clipboard.getPrimaryClip().getItemAt(0).getText();
    

    or, because you are setting the clip right there, you can just use the value directly, in the string

    artist.getCoupon_code()
    

    Good luck!