Android

Copy and Paste App

Posted on Posted in Android, Tutorials

How to Develop a Copy and Paste App.

Welcome to Eloneth Android App Development Tutorials for beginners – Part 7.

This video tutorial demonstrates how to create a copy and paste app.

Android provides the clipboard framework for copying and pasting different types of data. The data could be text, images, binary stream data or other complex data types.

Android provides the library of ClipboardManagerClipData and ClipData.item to use the copying and pasting framework. In order to use clipboard framework, you need to put data into clip object, and then put that object into system wide clipboard.

In order to use clipboard , you need to instantiate an object of ClipboardManager by calling the getSystemService() method.

 

You can find the source code which includes steps for developing this app below the video tutorial.

 

 

    Image of the app

Android

 

                                     Video Tutorial

Steps to Develop the App.

If you prefer to develop the app through the below method rather than video tutorial, you can go through the steps. If you are new to programming, I will recommend that you type out the codes instead of doing copy  and paste. In my opinion, you will get a better understanding of the  codes by doing that.

 

Step 1:

Create a project in android studio with the following details:

Project Name: CopyPasteApp

 MinImum SDK: API 19: Android  4.4 kitkat.

Package name: com.eloneth.  You can use any package name of your choice.

Click next, select empty activity, click next and click finish.

 

Step 2:

activity_main.xml

After creating the new project, open the activity_main.xml file and delete the codes that are inside.  Paste in the below codes.


<?xml version="1.0" encoding="utf-8"?>
<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="com.eloneth.copypasteapp.MainActivity">

  <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Eloneth Tutorials"
      android:id="@+id/textView"
      android:layout_centerHorizontal="true"
      android:textColor="#25c"
      android:textSize="35dp"
      />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:layout_marginTop="79dp"
        android:layout_below="@+id/textView"
        android:hint="Enter text"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editText2"
        android:hint="Text will be pasted here"
        android:layout_centerVertical="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Copy Text"
        android:id="@+id/button"
        android:layout_below="@+id/editText2"
        android:layout_alignParentStart="true"
        android:layout_marginTop="70dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Paste Text"
        android:id="@+id/button2"
        android:layout_alignTop="@+id/button"
        android:layout_alignParentEnd="true" />


</RelativeLayout>

 

Step 3:

Note: If you used a different package name, do not forget to change com.eloneth to your package name in
the <Relativelayout, i.e tools:context=”com.eloneth.copypasteapp.MainActivity”>.

 

Step 4:

MainActivity.java

This is the MainActivity.java  class code. There are comments that explain the codes.

 

package com.eloneth.copypasteapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;





public class MainActivity extends AppCompatActivity {

    EditText ed1,ed2;
    Button b1,b2;

    //Declare ClipboardManager and Clipdata classes
    private ClipboardManager myClipboard;
    private ClipData myClip;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ed1=(EditText)findViewById(R.id.editText);
        ed2=(EditText)findViewById(R.id.editText2);

        b1=(Button)findViewById(R.id.button);
        b2=(Button)findViewById(R.id.button2);

        //Initialize clipBoardManager class
        myClipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);

        //When b1 button is clicked, perform the below action, i.e save user input in clipBoardManager class
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text;
                //Get user input, convert it to string and save in text
                text = ed1.getText().toString(); 
                //Call newPlainText method from ClipData class and pass in the user input
                myClip = ClipData.newPlainText("text", text);
                //Save the user input in ClipBoardManage class 
                myClipboard.setPrimaryClip(myClip);
                //Toast shows a pop up text that allows us know that the text has been copied
                Toast.makeText(getApplicationContext(), "Text Copied",Toast.LENGTH_SHORT).show();//Make a toast
            }
        });

        //Retrieve user input from clipBoardManager class when b2 btn is clicked and paste it ed2
        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Use getPrimaryClip to get the data from clipBoardManager class and save it in clipData class thu it object abc
                ClipData abc = myClipboard.getPrimaryClip();
                //use getItemAt method to get the data from abc and save it in clipDate.Item object 
                ClipData.Item item = abc.getItemAt(0);
                //Get the data and convert it to string
                String text = item.getText().toString();
                //Display/paste the data on ed2 EditText
                ed2.setText(text);
                //Make a toast to display the action
                Toast.makeText(getApplicationContext(), "Text Pasted",Toast.LENGTH_SHORT).show();
            }
        });
    }
}

 

If you run your app on an emulator or a real device, you should see the app.

If you find this tutorial useful, subscribe to my  newsletter to receive notification whenever I release new tutorials. You can also  share the video, like my page and leave a comment. Thanks for watching.

You can also contact me if you need a new website for your business or an upgrade of an existing one.

 

Watch more tutorials:

How to install android studio development environment (Article).Part 1. 

How to create your first android application, Part 2.

Describing some Folders and Components in Android Studio. Part 3.

How to Create an Emulator for Testing our App. Part 4.

How to develop a simple Facebook-liked-Messenger. Part 6. 

How to Convert a Website into Android App using Android Studio. Part 8.

Leave a Reply

Your email address will not be published.