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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/etCopy"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="@dimen/padding_medium"
android:gravity="top"
android:scrollHorizontally="false"
android:inputType="textMultiLine"
/>
<RadioGroup
android:id="@+id/rbgTextHTML"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<RadioButton
android:id="@+id/rbHtml"
android:layout_weight=".5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/rbHtml"/>
<RadioButton
android:id="@+id/rbText"
android:layout_weight=".5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/rbText"/>
</RadioGroup>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:onClick="copyHtml"
android:text="@string/btCopy"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"

42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87

android:layout_weight=".5"
android:onClick="pasteHtml"
android:text="@string/btPaste"/>
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_weight=".5"
android:layout_height="wrap_content"
android:onClick="sendHtmlIntent"
android:text="@string/btSendHtmlIntent"/>
<Button
android:layout_width="0dp"
android:layout_weight=".5"
android:layout_height="wrap_content"
android:onClick="sendClipdataIntent"
android:text="@string/btSendClipdataIntent"/>
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/tvCopiedText"/>
<EditText
android:id="@+id/etPaste"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:gravity="top"
android:scrollHorizontally="false"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/tvcoerceText"/>
<EditText
android:id="@+id/etPasteCoerceText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:scrollHorizontally="false"
android:inputType="textMultiLine"/>
</LinearLayout>

88
89
90
91
92
93
3.) Write following into strings.xml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

<?xml version="1.0" encoding="utf-8"?>


<resources>
<string name="app_name">ClipboardActivity</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_htmlintent">Html Intent Activity</string>
<string name="title_activity_clipdataintent">ClipData Intent Activity</string>
<string name="tvHtml"><![CDATA[<b>Link:</b> <a
href="http://www.google.com">Google</a>]]></string>
<string name="btCopy">Copy</string>
<string name="btPaste">Paste</string>
<string name="btSendHtmlIntent">send HTML Intent</string>
<string name="btSendClipdataIntent">send ClipData Intent</string>
<string name="rbHtml">Paste Html</string>
<string name="rbText">Paste Text</string>
<string name="tvCopiedText"><b><i>Copied text</i></b></string>
<string name="tvcoerceText"><b><i>Copied coerce Text</i></b></string>
<string name="tvIntentText"><b><i>Intent Text</i></b></string>
<string name="tvIntentHtml"><b><i>Intent Html</i></b></string>
<string name="tvIntentClipdataText"><b><i>Intent Clipdata Text</i></b></string>
<string name="tvIntentClipdataHtml"><b><i>Intent Clipdata Html</i></b></string>
</resources>

4.) Create and write following into layout/activity_htmlintent.xml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/tvIntentHtml" />
<EditText
android:id="@+id/etHtml"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:padding="@dimen/padding_medium"
android:scrollHorizontally="false" />

17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/tvIntentText" />
<EditText
android:id="@+id/etText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:padding="@dimen/padding_medium"
android:scrollHorizontally="false" />
</LinearLayout>

5.) Create and write following into layout/activity_clipdataintent.xml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/tvIntentClipdataHtml" />
<EditText
android:id="@+id/etClipBoardHtml"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:padding="@dimen/padding_medium"
android:scrollHorizontally="false" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/tvIntentClipdataText" />
<EditText
android:id="@+id/etClipBoardText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:padding="@dimen/padding_medium"
android:scrollHorizontally="false" />
</LinearLayout>

29
30
31
32
33
6.) Create and write following into values/dimens.xml:

1
2
3
4
5
6
7

<resources>
<dimen name="padding_small">8dp</dimen>
<dimen name="padding_medium">8dp</dimen>
<dimen name="padding_large">16dp</dimen>
</resources>

7.) Write following into manifest file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

<?xml version="1.0" encoding="utf-8"?>


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.clipboardactivity"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.clipboardactivity.ClipboardActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.clipboardactivity.ClipdataIntentActivity"
android:label="@string/title_activity_clipdataintent" >
</activity>
<activity
android:name="com.example.clipboardactivity.HtmlIntentActivity"
android:label="@string/title_activity_htmlintent" >
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<!-- This activity will get launched when proper intent type
will match
in this case "text/html" -->
<data android:mimeType="text/html" />
</intent-filter>

33
34
35
36
37
38
39
40

</activity>
</application>
</manifest>

8.) Create and write following into src/Utility.java:

1
2
3
4
5
6
7
8
9
10
11

package com.example.clipboardactivity;
import android.content.Context;
import android.widget.Toast;
public class Utility {
public static void showToastMessage(Context context, String message, int
duration){
Toast.makeText(context, message, duration).show();
}
}

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) {
super.onCreate(savedInstanceState);
2
setContentView(R.layout.activity_htmlintent);
1
etHtml = (EditText) findViewById(R.id.etHtml);
3
etText = (EditText) findViewById(R.id.etText);
1
4
//Get the intent that started this activity
Intent intent = getIntent();
1
if (intent != null && intent.getType() != null
5
&& 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
//method sends this type of Intent.
7
Bundle bundle = intent.getExtras();
1
if(bundle != null){

8
1
9
2
0
2
1
2
2
2
3
2
4
2
5
2 );
6
2
7
2 }
8
2
9
3
0
3
1
3
2
3
3
3
4
3
5

etHtml.setText(bundle.getCharSequence(Intent.EXTRA_HTML_TEXT)
etText.setText(bundle.getCharSequence(Intent.EXTRA_TEXT));
}

10.) Create and write following into src/ClipdataIntentActivity.java:

1
2
3
4
5
6
7
8
9
10

package com.example.clipboardactivity;
import
import
import
import
import
import
import
import

android.app.Activity;
android.content.ClipboardManager;
android.content.Intent;
android.os.Bundle;
android.widget.EditText;
android.widget.Toast;
android.content.ClipData;
android.content.ClipDescription;

import com.example.clipboardactivity.R;

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

public class ClipdataIntentActivity extends Activity {


private EditText etHtml;
private EditText etText;
ClipboardManager mClipboard;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_clipdataintent);
etHtml = (EditText) findViewById(R.id.etClipBoardHtml);
etText = (EditText) findViewById(R.id.etClipBoardText);
//Get the intent that started this activity
Intent intent = getIntent();
if (intent != null) {
ClipData clipdata = intent.getClipData();
if (clipdata != null
&& clipdata.getDescription().hasMimeType(
ClipDescription.MIMETYPE_TEXT_HTML)) {
ClipData.Item item = clipdata.getItemAt(0);
etHtml.setText(item.getHtmlText());
etText.setText(item.getText());
} else {
Utility.showToastMessage(this,
"Intent clipdata doesn't have HTML",
Toast.LENGTH_SHORT);
}
}
}
}

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

package com.example.clipboardactivity;
import
import
import
import
import
import
import
import
import
import
import
import
import

android.app.Activity;
android.content.ClipData;
android.content.ClipDescription;
android.content.ClipboardManager;
android.content.Context;
android.content.Intent;
android.os.Bundle;
android.text.Html;
android.text.Spannable;
android.view.View;
android.widget.EditText;
android.widget.RadioButton;
android.widget.Toast;

public class ClipboardActivity extends Activity {


EditText etCopy;
EditText etPaste;
EditText etPasteCoerceText;
RadioButton rbText;
RadioButton rbHtml;
ClipboardManager mClipboard;
ClipboardManager.OnPrimaryClipChangedListener mPrimaryChangeListener = new
ClipboardManager.OnPrimaryClipChangedListener() {
/**
* This method is a callback. It get called when the primary clip

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71

* on the clipboard changes.


*/
public void onPrimaryClipChanged() {
//Toast message will appear whenever the clipboad
//primary data changes.
Utility.showToastMessage(getApplicationContext(),
"Primary clipdata changed", Toast.LENGTH_SHORT);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
etCopy = (EditText) findViewById(R.id.etCopy);
etPaste = (EditText) findViewById(R.id.etPaste);
etPasteCoerceText = (EditText) findViewById(R.id.etPasteCoerceText);
etCopy.setText(Html.fromHtml(getString(R.string.tvHtml)));
rbText = (RadioButton) findViewById(R.id.rbText);
rbHtml = (RadioButton) findViewById(R.id.rbHtml);

mClipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);


mClipboard.addPrimaryClipChangedListener(mPrimaryChangeListener);

/**
* This method gets called when "Copy" button get pressed.
* @param view
*/
public void copyHtml(View view) {
String htmlText = getHtmltxt(etCopy);
String plainText = getOnlyText(etCopy);
mClipboard.setPrimaryClip(ClipData.newHtmlText("HTML Text", plainText,
htmlText));
}
/**
* This method gets called when "Paste" button get pressed.
* @param view
*/
public void pasteHtml(View view) {
// Check if there is primary clip exsiting.
// If it does then echeck the mime type to make sure
// it has HTML content.
if (mClipboard.hasPrimaryClip()
&& mClipboard.getPrimaryClipDescription().hasMimeType(
ClipDescription.MIMETYPE_TEXT_HTML)) {
// Get the very first item from the clip.
ClipData.Item item = mClipboard.getPrimaryClip().getItemAt(0);
// If "Paste HTML" radio button is selected then paste
// HTML in the Textview.
if (rbHtml.isChecked()) {
etPaste.setText(item.getHtmlText());

72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117

} else {
// Paste the only text version.
etPaste.setText(item.getText());
}
// Paste the CoerceText .
etPasteCoerceText.setText(item.coerceToText(this));
}
}
/**
* This method gets called when "send Html Intent" button get pressed.
* @param view
*/
public void sendHtmlIntent(View view) {
// This kind of intent can be handle by this application
// Or other application which handle text/html type Intent
Intent intent = new Intent(Intent.ACTION_SEND);
String htmlText = getHtmltxt(etCopy);
String text = getOnlyText(etCopy);
intent.putExtra(Intent.EXTRA_HTML_TEXT, htmlText);
intent.putExtra(Intent.EXTRA_TEXT, text);
intent.setType("text/html");
startActivity(Intent.createChooser(intent, null));
}
/**
* This method gets called when "send Clipdata Intent" button get pressed.
*
* @param view
*/
public void sendClipdataIntent(View view) {
String htmlText = getHtmltxt(etCopy);
String plainText = getOnlyText(etCopy);
Intent intent = new Intent(this, ClipdataIntentActivity.class);
intent.setClipData(ClipData.newHtmlText(
"HTML text in Intent's clipdata", plainText, htmlText));
startActivity(intent);
}
@Override
protected void onDestroy() {
super.onDestroy();
//Remove the ClipChanged Listener to save the resources.
mClipboard.removePrimaryClipChangedListener(mPrimaryChangeListener);
}
/**
* This method get the EditText object and returns the HTML text. This
* method can only be run with those EditText which has spannable set and
* contains the HTML text.
*
* @param editText
* @return
*/
private String getHtmltxt(EditText editText) {
Spannable spannable = (Spannable) editText.getText();
return Html.toHtml(spannable);
}

118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152

/**
* This method takes the EditText object which has spannable object with HTML
* text and returns the only text.
*
* @param editText
* @return
*/
private String getOnlyText(EditText editText) {
return editText.getText().toString();
}

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