Skip to content
Advertisement

Android copy text from edittext

I’m having some trouble whilst following a beginner android dev program. I build an Android App and I have created EditText and Button.

My idea, users can put PhoneNumber in EditText, and if they click the Copy button, they can copy shortUrl with PhonNumber that inputed! So, I have create a String with Url. and if the users put the number will get it in the Url! Example:

users typed: +1716322765 if user click “The Button”, will get “https://google.me/+1716322765”

So, I wrote this code, but does not execute.

    whatsLink = "https://google.me/";

    phoneNumber = (EditText) findViewById(R.id.edt);
    letsChatting = (Button) findViewById(R.id.btn1);
    letsCopy = (Button) findViewById(R.id.btn2);


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

            String copy = phoneNumber.getText().toString();
            if (copy.isEmpty()) {
                ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                ClipData clipdata = ClipData.newPlainText("Data", phoneNumber.getText().toString());
                clipboard.setPrimaryClip(clipdata);
            }
        }

    });

Advertisement

Answer

You want if(!copy.isEmpty()). Otherwise you would only copy if there was no phone number entered.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement