Professional Documents
Culture Documents
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
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
<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>
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
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>
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
</activity>
</application>
</manifest>
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();
}
}
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));
}
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
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;
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
/**
* 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();
}
Tags: Clipboard Activity Android, Clipboard Activity Android programming, Clipboard Activity example
android, Clipboard Activity in Android, Clipboard Activity tutorials