0

Today, I was making a function in my application. This function is to use a floating action button to copy a phone number typed in a autocomplete textview. But after reading several documents of the clipboard manager, I was confused. Because the clipdoard.settext is deprecated in the Android Studio, I change it into this method. But I can't copy and paste via the two methods. So I failed to reach this function in the way which is showed in the code block. Can anybody help me? I am a starter, and I will be very grateful if someone could give me a solution.

Here is the code I wrote .

import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.ImageView;
import android.widget.TextView;

public class merge extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.merge);
    FloatingActionButton floatingActionButton = findViewById(R.id.floatingActionButtonCall);
    AutoCompleteTextView autoCompleteTextView = findViewById(R.id.autoCompleteTextView3);
    CharSequence phone = autoCompleteTextView.getText().toString();
    floatingActionButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ClipboardManager clipboard = (ClipboardManager)
                    getSystemService(Context.CLIPBOARD_SERVICE);
            ClipData clip = ClipData.newPlainText(phone);
            clipboard.setPrimaryClip(clip);
        }
    });
}

......
Zeng Tianyu
  • 53
  • 1
  • 1
  • 11

1 Answers1

2

Inside your onclick change this line:

floatingActionButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            ClipboardManager clipboard = (ClipboardManager)
                    getSystemService(Context.CLIPBOARD_SERVICE);
            ClipData clip = ClipData.newPlainText("Copied Text", autoCompleteTextView.getText().toString()); // get text from edit text
            clipboard.setPrimaryClip(clip);
        }
});
Mohammed Junaid
  • 1,362
  • 14
  • 18