-1

How can I click on a button (phone number) and it copies it and show me that it was copied?

activity_main.xml

<Button
android:id="@+id/phonenumberhotel"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Tel.: 00000 0 000 0000"/>

MainActivity.java

package com.iss.dfdfd;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.Bundle;
import android.text.ClipboardManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

public class PageOneHotel extends Activity {


    Button phonenumberhotel;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.hotel);
        Toast.makeText(getBaseContext(), "Phone number has been copied", Toast.LENGTH_LONG).show();
        addButtonListener();
        addListenerOnButton();

    }


    public void addListenerOnButton() {

        phonenumberhotel = (Button) findViewById(R.id.phonenumberhotel);

        phonenumberhotel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO add action here
            }
        });
        ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); 
        ClipData clip = ClipData.newPlainText("label", "Copied");


    }
}

I did that, the number is not copied, only a msg appears from the toast .. I just want to click on the button and it tells me the number is copied. When I go to the dialer, it pastes nothing .. help :)

2 Answers2

2

Here is the fragment of code which copies text to clipboard in Android: How to copy text programmatically in my Android app?

When creating your button add .setOnClickListener() with your own listener which in his method onClick() calls given fragment of code.

Text of Button can be obtained via button.getText().toString()

Community
  • 1
  • 1
dpassy
  • 363
  • 1
  • 10
1

public class PageOneHotel extends Activity {

final Button phonenumberhotel;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.hotel);
    addButtonListener();
    addListenerOnButton();

}


public void addListenerOnButton() {

    phonenumberhotel = (Button) findViewById(R.id.phonenumberhotel);

    phonenumberhotel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
             ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
             String number = phonenumberhotel.getText().toString()
             ClipData clip = ClipData.newPlainText("Phone number",number);   
             clipboard.setPrimaryClip(clip);
             Toast.makeText(getBaseContext(), "Phone number has been copied", Toast.LENGTH_LONG).show();
        }
    });


}

}

MrJM
  • 1,214
  • 1
  • 12
  • 26
  • thank you, if you come first with the solution I can mark you :/ – Maryam Jabir Jan 21 '15 at 09:06
  • You are almost there. I edited my answer to match your updated question. If this solves your problem please mark it as accepted :) – MrJM Jan 21 '15 at 11:32
  • Hello MrJm It gives me another two errors :/ .. http://oi57.tinypic.com/flf3u0.jpg – Maryam Jabir Jan 22 '15 at 09:07
  • Hello, there is indeed an error. getText needs to be getText() so you will get String number = phonenumberhotel.getText().toString(); For the clipboard.setPrimaryClip make sure you have the right imports import android.content.ClipData; import android.content.ClipboardManager; – MrJM Jan 22 '15 at 09:26
  • I have an error again but the first one is done .. the error is with the clipboard.setPrimaryClip .. also you explained it but it doest work http://oi61.tinypic.com/2w68g0o.jpg .. I added these import though, they add themselves automatically .. http://oi57.tinypic.com/2rqisgi.jpg ... help? :) – Maryam Jabir Jan 22 '15 at 14:06
  • You still have an old import for ClipboardManager. If you remove import android.text.ClipboardManager; you will be fine ;) If not, can you show the error message you are getting? – MrJM Jan 23 '15 at 09:09
  • Hello MrJm .. I tried it but it didn't copy as in http://oi59.tinypic.com/vql8hw.jpg .. I removed "clip" with warning but it didn't copy as in http://oi61.tinypic.com/s2uzkk.jpg ..I removed "clipboard" with warning but it didn't copy as in http://oi59.tinypic.com/24b2fcx.jpg .. I removed "number" with warning but it didn't copy as in http://oi58.tinypic.com/2qco4m0.jpg .. it looks like this http://oi59.tinypic.com/2wlqz4j.jpg .. no warnings .. no errors :/ – Maryam Jabir Jan 24 '15 at 08:19
  • You don't need to make changes to your method. This works as it should. What you did is remove the copy functionality, this is why it's not working. All you have to do is fix the imports on top of your class. Change this line: "import android.text.ClipboardManager;" to " import android.content.ClipboardManager;" – MrJM Jan 25 '15 at 15:03
  • Thank you, I fixed it, it created two java files "ClipData" and "ClipboardManager" .. so the application worked and gave me no error but when I run on my mobile, it gives me an error when I tick to copy :/ – Maryam Jabir Jan 27 '15 at 06:54
  • p.s. I am using Android 2.2 – Maryam Jabir Jan 31 '15 at 06:43