0

i want to paste android clipboard text to my edit text.

var button = MainActivity.This.FindViewById<Button> (Resource.AddLinks.btn_Paste);
EditText txt_Address = MainActivity.This.FindViewById<EditText> (Resource.AddLinks.txt_Address);
button.Click += (sender,e) =>
{
    txt_Address.Text=//How to Paste Android Clipbord?

};
Ali Yousefi
  • 2,355
  • 2
  • 32
  • 47

1 Answers1

6

It's like anything else you want to do with Xamarin.Android. You need to first find out how do it in on native Android/Java. Then convert it into C#.

Here's a link to an Android solution Android copy/paste from clipboard manager

And here is that example in C#:

//for copy
var clipboard = (ClipboardManager)GetSystemService(ClipboardService);
var clip = ClipData.NewPlainText("your_text_to_be_copied");

clipboard.PrimaryClip = clip;


// And paste it
var clipboard = (ClipboardManager)GetSystemService(ClipboardService);

var pasteData = "";

if (!(clipboard.HasPrimaryClip)) 
{
    // If it does contain data, decide if you can handle the data.

} 
else if (!(clipboard.PrimaryClipDescription.HasMimeType(ClipDescription.MimetypeTextPlain)))
{

    // since the clipboard has data but it is not plain text

} 
else 
{
    //since the clipboard contains plain text.
    var item = clipboard.PrimaryClip.GetItemAt(0);

    // Gets the clipboard as text.
    pasteData = item.Text;
}

Basic rule of thumb for translating Java to C#.

  • Setters and Getters are usually translated to Properties
    • String text = item.getText(); => var text = item.Text;
    • item.setText(text); => item.Text = text;
  • ANDROID_CONSTANTS are usually translated to Enums or Class const fields
    • Context.CLIPBOARD_SERVICE => Context.ClipboardService
    • MIMETYPE_TEXT_PLAIN => ClipDescription.MimetypeTextPlain

See http://docs.xamarin.com/guides/android/advanced_topics/api_design/ for more information.

Community
  • 1
  • 1
Kiliman
  • 18,460
  • 3
  • 39
  • 38
  • i test this code but ClipData class does not exist in my solution or my namespace...how to use this in my application? – Ali Yousefi Mar 21 '14 at 22:42
  • What is your compile API level set as? If you're less than 11 then you can just use `clipboard.Text` ClipData was added in Honeycomb. Again you really need to look at the Android docs. – Kiliman Mar 22 '14 at 00:57
  • android API changed a little - instead ClipData.NewPlainText("your_text_to_be_copied"); we must write ClipData.NewPlainText("label", "your_text_to_be_copied"). Where label is User-visible label for the clip data, a tag for data stored in clipboard – denis.sugakov May 18 '16 at 23:35
  • What is the paste event? I want to run some code when a paste occurred. – Mohammad Afrashteh Apr 09 '18 at 11:38