Android Application Development is the new boom in the programming industry these days. A lot of companies are planning to come up with their Android Applications for the customers and readers. A successful Android Application consists of various small code snippets that you implement in the application. These snippets can range from a simple copy to clipboard function or a function to share the content shown on the screen via some other app to the pretty intense game programming.
Today, I’ll teach you guys how to create a simple copy to clipboard function in your Android Application. In our code, we need to provide a string that will be copied to the clipboard and once it has been copied, a simple Toast message (or you can even create a custom toast message) will be displayed to the user stating that the text has been copied to the clipboard. Before we begin, the clipboard copying technique that we are using requires a minimum API level of 11. Make sure you application has that. If it does not have that, make sure to change that in the Android Manifest file.
Must read before proceeding:
Let’s get started by with our simple Application that will enable us to copy a string to the clipboard. For this tutorial, I am going to create a new application in Eclipse and I will have a text view, and 2 buttons in the layout. Once the button is pressed/clicked, it will copy the text of the Text View or of any String to the Clipboard and generate a simple toast.
Copy to Clipboard in Android
1. Create a new Application or use your existing one if you wish. Use the following settings to create a new Application, but you may change the name and other things as per your need. But make sure the Minimum API level is set to at least 11.
2. Next, in the Main Layout file, go to the XML view and paste the following lines of code. It will enable you to create a view with a Text View and 2 buttons, as we discussed.
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="20dp" android:layout_marginTop="20dp" android:text="Hello World" android:textSize="20sp" android:textStyle="bold" /> <Button android:id="@+id/copyButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/textView" android:text="Copy Text View Clipboard" /> <Button android:id="@+id/copyButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/copyButton1" android:text="Copy String to Clipboard" /> </RelativeLayout>
3. Now, open your Main Activity Java file in the src folder.
4. In this file, write down the following code in the onCreate function of the class.
final String toCopy = "Hello! This is a string to be copied."; final TextView tv = (TextView) findViewById(R.id.textView); Button b = (Button) findViewById(R.id.copyButton1); Button b2 = (Button) findViewById(R.id.copyButton2);
5. Next, we need to set up the on click listeners for the two buttons. The button “b” will copy the text of the Text View, whereas the button “b2” will copy the text of the String.
b.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // Code to Copy the content of Text View to the Clip board. ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); ClipData clip = ClipData.newPlainText("simple text", tv.getText()); clipboard.setPrimaryClip(clip); Toast.makeText(getApplicationContext(), "Text View Copied to Clipboard", Toast.LENGTH_LONG).show(); } }); b2.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // Code to Copy the content of String to the Clip board. ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); ClipData clip = ClipData.newPlainText("simple text", toCopy); clipboard.setPrimaryClip(clip); Toast.makeText(getApplicationContext(), "String copied to Clipboard", Toast.LENGTH_LONG).show(); } });
6. Lastly, press CTRL + SHIFT + O on your keyboard to import all required files. The complete Java Code for your reference is here.
package com.slashcoding.copyclip; import android.app.Activity; import android.content.ClipData; import android.os.Bundle; import android.content.ClipboardManager; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final String toCopy = "Hello! This is a string to be copied."; final TextView tv = (TextView) findViewById(R.id.textView); Button b = (Button) findViewById(R.id.copyButton1); Button b2 = (Button) findViewById(R.id.copyButton2); b.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // Code to Copy the content of Text View to the Clip board. ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); ClipData clip = ClipData.newPlainText("simple text", tv.getText()); clipboard.setPrimaryClip(clip); Toast.makeText(getApplicationContext(), "Text View Copied to Clipboard", Toast.LENGTH_LONG).show(); } }); b2.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // Code to Copy the content of String to the Clip board. ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); ClipData clip = ClipData.newPlainText("simple text", toCopy); clipboard.setPrimaryClip(clip); Toast.makeText(getApplicationContext(), "String copied to Clipboard", Toast.LENGTH_LONG).show(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
7. Well, that’s all. The coding part is complete. Now, run this application in an Android Virtual Device and see the text being copied to the clipboard.
When implementing this in your application, you can specify any string that you want to be copied. You can even fetch the data from the text view of that particular view, like we did. If you wish, you can download the Source Code from below.
DOWNLOAD SOURCE
Do let me know in the comments section if you face any errors. I will be more than happy to help you with those errors. Also, don’t forget to subscribe to Slash Coding for latest post updates via RSS Feeds, Facebook, Google+ or Twitter.