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:
<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" 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>
3.) Write following into strings.xml:
<?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:
<?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" /> <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:
<?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>
6.) Create and write following into values/dimens.xml:
<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:
<?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> </activity> </application> </manifest>
8.) Create and write following into src/Utility.java:
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:
package com.example.clipboardactivity; import com.example.clipboardactivity.R; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.EditText; public class HtmlIntentActivity extends Activity { private EditText etHtml; private EditText etText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_htmlintent); etHtml = (EditText) findViewById(R.id.etHtml); etText = (EditText) findViewById(R.id.etText); //Get the intent that started this activity Intent intent = getIntent(); if (intent != null && intent.getType() != null && intent.getType().equals("text/html")) { //This contition will full-fill when this application receive the //intent who's type is "test/html". In this application sendHtmlIntent //method sends this type of Intent. Bundle bundle = intent.getExtras(); if(bundle != null){ etHtml.setText(bundle.getCharSequence(Intent.EXTRA_HTML_TEXT)); etText.setText(bundle.getCharSequence(Intent.EXTRA_TEXT)); } } } }
10.) Create and write following into src/ClipdataIntentActivity.java:
package com.example.clipboardactivity; import android.app.Activity; import android.content.ClipboardManager; import android.content.Intent; import android.os.Bundle; import android.widget.EditText; import android.widget.Toast; import android.content.ClipData; import android.content.ClipDescription; import com.example.clipboardactivity.R; 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:
package com.example.clipboardactivity; import android.app.Activity; import android.content.ClipData; import android.content.ClipDescription; import android.content.ClipboardManager; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.text.Html; import android.text.Spannable; import android.view.View; import android.widget.EditText; import android.widget.RadioButton; import 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 * 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()); } 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); } /** * 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
Top 10 Android App Development Trends | 2020 Guide
9 Popular Cross-Platform Tools for App Development in 2019
20 Best iOS App Development Tutorials and Online Learning Resources
Top 15 Best Android Apps For C Programming | 2018 Exclusive
The Best 15 Mobile Game Development Platforms & Tools in 2018
The Top Web Development Frameworks in 2018
10 Top Web Development Frameworks In 2017
Android Studio Introduction