You are on page 1of 14

Android provides support for copy and paste feature using ClipBoardManager.

This example covers the latest feature


introduced in JellyBean, supporting the styled text.
Algorithm:
1.) Create a new project by File-> New -> Android Project name it ClipboardActivity.
2.) Write following into main.xml:
1 <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
2 xmlns:tools="http://schemas.android.com/tools"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 android:orientation="vertical">
6
7 <EditText
android:id="@+id/etCopy"
8 android:layout_width="fill_parent"
9 android:layout_height="wrap_content"
10 android:padding="@dimen/padding_medium"
11 android:gravity="top"
12 android:scrollHorizontally="false"
13 android:inputType="textMultiLine"
/>
14 <RadioGroup
15 android:id="@+id/rbgTextHTML"
16 android:orientation="horizontal"
17 android:layout_width="fill_parent"
android:layout_height="wrap_content"
18
>
19 <RadioButton
20 android:id="@+id/rbHtml"
21 android:layout_weight=".5"
22 android:layout_width="0dp"
android:layout_height="wrap_content"
23 android:checked="true"
24 android:text="@string/rbHtml"/>
25 <RadioButton
26 android:id="@+id/rbText"
27 android:layout_weight=".5"
android:layout_width="0dp"
28 android:layout_height="wrap_content"
29 android:text="@string/rbText"/>
30
31 </RadioGroup>
32 <LinearLayout
android:layout_width="match_parent"
33
android:layout_height="wrap_content"
34 android:orientation="horizontal">
35 <Button
36 android:layout_width="0dp"
37 android:layout_height="wrap_content"
android:layout_weight=".5"
38 android:onClick="copyHtml"
39 android:text="@string/btCopy"/>
40 <Button
41 android:layout_width="0dp"
android:layout_height="wrap_content"
42 android:layout_weight=".5"
android:onClick="pasteHtml"
43
android:text="@string/btPaste"/>
44 </LinearLayout>
45 <LinearLayout android:layout_width="fill_parent"
46 android:layout_height="wrap_content"
47 android:orientation="horizontal">
<Button
48 android:layout_width="0dp"
49 android:layout_weight=".5"
50 android:layout_height="wrap_content"
51 android:onClick="sendHtmlIntent"
52 android:text="@string/btSendHtmlIntent"/>
<Button
53 android:layout_width="0dp"
54 android:layout_weight=".5"
55 android:layout_height="wrap_content"
56 android:onClick="sendClipdataIntent"
android:text="@string/btSendClipdataIntent"/>
57 </LinearLayout>
58 <TextView
59 android:layout_width="fill_parent"
60 android:layout_height="wrap_content"
61 android:text="@string/tvCopiedText"/>
<EditText
62 android:id="@+id/etPaste"
63 android:layout_width="fill_parent"
64 android:layout_height="wrap_content"
65 android:inputType="textMultiLine"
66 android:gravity="top"
67 android:scrollHorizontally="false"
/>
68 <TextView
69 android:layout_width="fill_parent"
70 android:layout_height="wrap_content"
71 android:text="@string/tvcoerceText"/>
<EditText
72 android:id="@+id/etPasteCoerceText"
73 android:layout_width="fill_parent"
74 android:layout_height="wrap_content"
75 android:gravity="top"
76 android:scrollHorizontally="false"
77 android:inputType="textMultiLine"/>
</LinearLayout>
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
3.) Write following into strings.xml:
1
2 <?xml version="1.0" encoding="utf-8"?>
3 <resources>
4
5 <string name="app_name">ClipboardActivity</string>
6 <string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
7 <string name="title_activity_htmlintent">Html Intent Activity</string>
8 <string name="title_activity_clipdataintent">ClipData Intent Activity</string>
9 <string name="tvHtml"><![CDATA[<b>Link:</b> <a
10 href="http://www.google.com">Google</a>]]></string>
11 <string name="btCopy">Copy</string>
<string name="btPaste">Paste</string>
12 <string name="btSendHtmlIntent">send HTML Intent</string>
13 <string name="btSendClipdataIntent">send ClipData Intent</string>
14 <string name="rbHtml">Paste Html</string>
15 <string name="rbText">Paste Text</string>
<string name="tvCopiedText"><b><i>Copied text</i></b></string>
16 <string name="tvcoerceText"><b><i>Copied coerce Text</i></b></string>
17 <string name="tvIntentText"><b><i>Intent Text</i></b></string>
18 <string name="tvIntentHtml"><b><i>Intent Html</i></b></string>
19 <string name="tvIntentClipdataText"><b><i>Intent Clipdata Text</i></b></string>
20 <string name="tvIntentClipdataHtml"><b><i>Intent Clipdata Html</i></b></string>
</resources>
21
22
4.) Create and write following into layout/activity_htmlintent.xml:
1 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
2
xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 android:orientation="vertical" >
6
7 <TextView
android:layout_width="match_parent"
8 android:layout_height="wrap_content"
9 android:text="@string/tvIntentHtml" />
10
11 <EditText
12 android:id="@+id/etHtml"
13 android:layout_width="match_parent"
android:layout_height="wrap_content"
14 android:inputType="textMultiLine"
15 android:padding="@dimen/padding_medium"
16 android:scrollHorizontally="false" />
17
18
19
20 <TextView
21 android:layout_width="match_parent"
android:layout_height="wrap_content"
22 android:text="@string/tvIntentText" />
23
24 <EditText
25 android:id="@+id/etText"
26 android:layout_width="match_parent"
android:layout_height="wrap_content"
27
android:inputType="textMultiLine"
28 android:padding="@dimen/padding_medium"
29 android:scrollHorizontally="false" />
30
31 </LinearLayout>
32
33
5.) Create and write following into layout/activity_clipdataintent.xml:
1 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
2 xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 android:orientation="vertical" >
6
7 <TextView
android:layout_width="match_parent"
8 android:layout_height="wrap_content"
9 android:text="@string/tvIntentClipdataHtml" />
10
11 <EditText
12 android:id="@+id/etClipBoardHtml"
13 android:layout_width="match_parent"
android:layout_height="wrap_content"
14 android:inputType="textMultiLine"
15 android:padding="@dimen/padding_medium"
16 android:scrollHorizontally="false" />
17
18 <TextView
android:layout_width="match_parent"
19 android:layout_height="wrap_content"
20 android:text="@string/tvIntentClipdataText" />
21
22 <EditText
23 android:id="@+id/etClipBoardText"
24 android:layout_width="match_parent"
android:layout_height="wrap_content"
25 android:inputType="textMultiLine"
26 android:padding="@dimen/padding_medium"
27 android:scrollHorizontally="false" />
28
</LinearLayout>
29
30
31
32
33
6.) Create and write following into values/dimens.xml:
1 <resources>
2
3 <dimen name="padding_small">8dp</dimen>
4 <dimen name="padding_medium">8dp</dimen>
5 <dimen name="padding_large">16dp</dimen>
6
</resources>
7
7.) Write following into manifest file:
1 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2
package="com.example.clipboardactivity"
3 android:versionCode="1"
4 android:versionName="1.0" >
5
6 <uses-sdk
7 android:minSdkVersion="16"
android:targetSdkVersion="16" />
8
9 <application
10 android:allowBackup="true"
11 android:icon="@drawable/ic_launcher"
12 android:label="@string/app_name"
13 android:theme="@style/AppTheme" >
<activity
14 android:name="com.example.clipboardactivity.ClipboardActivity"
15 android:label="@string/app_name" >
16 <intent-filter>
17 <action android:name="android.intent.action.MAIN" />
18
<category android:name="android.intent.category.LAUNCHER" />
19 </intent-filter>
20 </activity>
21 <activity
22 android:name="com.example.clipboardactivity.ClipdataIntentActivity"
23 android:label="@string/title_activity_clipdataintent" >
</activity>
24 <activity
25 android:name="com.example.clipboardactivity.HtmlIntentActivity"
26 android:label="@string/title_activity_htmlintent" >
27 <intent-filter>
28 <action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
29 <!-- This activity will get launched when proper intent type
30 will match
31 in this case "text/html" -->
32 <data android:mimeType="text/html" />
</intent-filter>
33
34
35 </activity>
36 </application>
37
38 </manifest>
39
40
8.) Create and write following into src/Utility.java:
1 package com.example.clipboardactivity;
2
3 import android.content.Context;
4 import android.widget.Toast;
5
public class Utility {
6
7 public static void showToastMessage(Context context, String message, int
8 duration){
9 Toast.makeText(context, message, duration).show();
10 }
11 }
9.) Create and write following into src/HtmlIntentActivity.java:
1 package com.example.clipboardactivity;
2
3 import com.example.clipboardactivity.R;
4
import android.app.Activity;
5 import android.content.Intent;
6 import android.os.Bundle;
7 import android.widget.EditText;
8
9 public class HtmlIntentActivity extends Activity {
1
private EditText etHtml;
0 private EditText etText;
11 @Override
1 protected void onCreate(Bundle savedInstanceState) {
2 super.onCreate(savedInstanceState);
1 setContentView(R.layout.activity_htmlintent);
etHtml = (EditText) findViewById(R.id.etHtml);
3 etText = (EditText) findViewById(R.id.etText);
1
4 //Get the intent that started this activity
1 Intent intent = getIntent();
5 if (intent != null && intent.getType() != null
&& intent.getType().equals("text/html")) {
1 //This contition will full-fill when this application receive the
6 //intent who's type is "test/html". In this application
1 sendHtmlIntent
7 //method sends this type of Intent.
1 Bundle bundle = intent.getExtras();
if(bundle != null){
8
1
9
2
0
2
1
2
2
2
3
2
4
2
5 etHtml.setText(bundle.getCharSequence(Intent.EXTRA_HTML_TEXT)
2 );
6 etText.setText(bundle.getCharSequence(Intent.EXTRA_TEXT));
2 }
7 }
}
2 }
8
2
9
3
0
3
1
3
2
3
3
3
4
3
5
10.) Create and write following into src/ClipdataIntentActivity.java:
1 package com.example.clipboardactivity;
2
import android.app.Activity;
3 import android.content.ClipboardManager;
4 import android.content.Intent;
5 import android.os.Bundle;
6 import android.widget.EditText;
7 import android.widget.Toast;
import android.content.ClipData;
8 import android.content.ClipDescription;
9
10 import com.example.clipboardactivity.R;
11
12
13
14
15 public class ClipdataIntentActivity extends Activity {
private EditText etHtml;
16 private EditText etText;
17 ClipboardManager mClipboard;
18
19 @Override
20 protected void onCreate(Bundle savedInstanceState) {
21 super.onCreate(savedInstanceState);
setContentView(R.layout.activity_clipdataintent);
22 etHtml = (EditText) findViewById(R.id.etClipBoardHtml);
23 etText = (EditText) findViewById(R.id.etClipBoardText);
24
25 //Get the intent that started this activity
26 Intent intent = getIntent();
27 if (intent != null) {
ClipData clipdata = intent.getClipData();
28
29 if (clipdata != null
30 && clipdata.getDescription().hasMimeType(
31 ClipDescription.MIMETYPE_TEXT_HTML)) {
32
33 ClipData.Item item = clipdata.getItemAt(0);
34
etHtml.setText(item.getHtmlText());
35 etText.setText(item.getText());
36 } else {
37 Utility.showToastMessage(this,
38 "Intent clipdata doesn't have HTML",
39 Toast.LENGTH_SHORT);
}
40
41 }
42 }
43
44 }
45
46
47
11.) Run for output.
Steps:
1.) Create a project named ClipboardActivity and set the information as stated in the image.
Build Target: Android 4.0
Application Name: ClipboardActivity
Package Name: com. example. ClipboardActivity
Activity Name: ClipboardActivity
Min SDK Version: 4.0
2.) Open ClipboardActivity.java file and write following code there:
1 package com.example.clipboardactivity;
2
import android.app.Activity;
3 import android.content.ClipData;
4 import android.content.ClipDescription;
5 import android.content.ClipboardManager;
6 import android.content.Context;
7 import android.content.Intent;
import android.os.Bundle;
8 import android.text.Html;
9 import android.text.Spannable;
10 import android.view.View;
11 import android.widget.EditText;
12 import android.widget.RadioButton;
import android.widget.Toast;
13
14
15 public class ClipboardActivity extends Activity {
16
17 EditText etCopy;
18 EditText etPaste;
19 EditText etPasteCoerceText;
RadioButton rbText;
20 RadioButton rbHtml;
21 ClipboardManager mClipboard;
22
23 ClipboardManager.OnPrimaryClipChangedListener mPrimaryChangeListener = new
24 ClipboardManager.OnPrimaryClipChangedListener() {
25 /**
* This method is a callback. It get called when the primary clip
26 * on the clipboard changes.
*/
27
public void onPrimaryClipChanged() {
28 //Toast message will appear whenever the clipboad
29 //primary data changes.
30 Utility.showToastMessage(getApplicationContext(),
31 "Primary clipdata changed", Toast.LENGTH_SHORT);
}
32 };
33
34 @Override
35 public void onCreate(Bundle savedInstanceState) {
36 super.onCreate(savedInstanceState);
37 setContentView(R.layout.main);
etCopy = (EditText) findViewById(R.id.etCopy);
38 etPaste = (EditText) findViewById(R.id.etPaste);
39 etPasteCoerceText = (EditText) findViewById(R.id.etPasteCoerceText);
40 etCopy.setText(Html.fromHtml(getString(R.string.tvHtml)));
41 rbText = (RadioButton) findViewById(R.id.rbText);
rbHtml = (RadioButton) findViewById(R.id.rbHtml);
42
43 mClipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
44 mClipboard.addPrimaryClipChangedListener(mPrimaryChangeListener);
45 }
46
47 /**
48 * This method gets called when "Copy" button get pressed.
* @param view
49 */
50 public void copyHtml(View view) {
51 String htmlText = getHtmltxt(etCopy);
52 String plainText = getOnlyText(etCopy);
53
54 mClipboard.setPrimaryClip(ClipData.newHtmlText("HTML Text", plainText,
htmlText));
55
56 }
57 /**
58 * This method gets called when "Paste" button get pressed.
59 * @param view
*/
60 public void pasteHtml(View view) {
61 // Check if there is primary clip exsiting.
62 // If it does then echeck the mime type to make sure
63 // it has HTML content.
64 if (mClipboard.hasPrimaryClip()
&& mClipboard.getPrimaryClipDescription().hasMimeType(
65 ClipDescription.MIMETYPE_TEXT_HTML)) {
66 // Get the very first item from the clip.
67 ClipData.Item item = mClipboard.getPrimaryClip().getItemAt(0);
68
69 // If "Paste HTML" radio button is selected then paste
// HTML in the Textview.
70
if (rbHtml.isChecked()) {
71 etPaste.setText(item.getHtmlText());
72 } else {
// Paste the only text version.
73
etPaste.setText(item.getText());
74 }
75 // Paste the CoerceText .
76 etPasteCoerceText.setText(item.coerceToText(this));
77 }
}
78 /**
79 * This method gets called when "send Html Intent" button get pressed.
80 * @param view
81 */
82 public void sendHtmlIntent(View view) {
// This kind of intent can be handle by this application
83 // Or other application which handle text/html type Intent
84 Intent intent = new Intent(Intent.ACTION_SEND);
85
86 String htmlText = getHtmltxt(etCopy);
87 String text = getOnlyText(etCopy);
intent.putExtra(Intent.EXTRA_HTML_TEXT, htmlText);
88 intent.putExtra(Intent.EXTRA_TEXT, text);
89 intent.setType("text/html");
90 startActivity(Intent.createChooser(intent, null));
91 }
92
93 /**
* This method gets called when "send Clipdata Intent" button get pressed.
94 *
95 * @param view
96 */
97 public void sendClipdataIntent(View view) {
98 String htmlText = getHtmltxt(etCopy);
String plainText = getOnlyText(etCopy);
99 Intent intent = new Intent(this, ClipdataIntentActivity.class);
100 intent.setClipData(ClipData.newHtmlText(
101 "HTML text in Intent's clipdata", plainText, htmlText));
102 startActivity(intent);
}
103 @Override
104 protected void onDestroy() {
105 super.onDestroy();
106 //Remove the ClipChanged Listener to save the resources.
107 mClipboard.removePrimaryClipChangedListener(mPrimaryChangeListener);
}
108 /**
109 * This method get the EditText object and returns the HTML text. This
110 * method can only be run with those EditText which has spannable set and
111 * contains the HTML text.
*
112
* @param editText
113 * @return
114 */
115 private String getHtmltxt(EditText editText) {
116 Spannable spannable = (Spannable) editText.getText();
return Html.toHtml(spannable);
117 }
118
119
120
121
122
123
124
125
126
127
128
129
130
131 /**
132 * This method takes the EditText object which has spannable object with HTML
133 * text and returns the only text.
134 *
* @param editText
135 * @return
136 */
137 private String getOnlyText(EditText editText) {
138 return editText.getText().toString();
139 }
}
140
141
142
143
144
145
146
147
148
149
150
151
152
3.) Compile and build the project.
Output
Tags: Clipboard Activity Android, Clipboard Activity Android programming, Clipboard Activity example
android, Clipboard Activity in Android, Clipboard Activity tutorials

You might also like