Home > OS >  Clipboard does not work and return null(Android/Java)
Clipboard does not work and return null(Android/Java)

Time:12-26

Im making a program that everytime you click a button, it will show what text do you copy in the TextView
But it return null when I call (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
I already copy some available text ("ABC") but its not work
I don't know if clipboard need any user permission? In my manifest I don't write anything about permission
Please help me \

Here is my code:

  ClipboardManager clipBoard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);

Button predictButton = (Button) mFloatingView.findViewById(R.id.predict);
predictButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Log.i("CLICK","work");
        if (clipBoard != null && clipBoard.getPrimaryClip() != null && clipBoard.getPrimaryClip().getItemCount() > 0) {
            Log.i("CLIPBOARD","work");
            ClipData clipData = clipBoard.getPrimaryClip();
            ClipData.Item item = clipData.getItemAt(0);
            String text = item.getText().toString();

            final TextView textViewToChange = (TextView) mFloatingView.findViewById(R.id.predict_text);
            textViewToChange.setText(text);
        }
    }
});

And here is my log:

I/ViewRootImpl@748418f[]: ViewPostIme pointer 1
I/CLICK: work
I/ViewRootImpl@748418f[]: ViewPostIme pointer 0
I/ViewRootImpl@748418f[]: ViewPostIme pointer 1
I/CLICK: work

CodePudding user response:

Since on android 10 or higher, u cant access to clipboard otherwise its will return null

  • Related