Open In App

Android – Clipboard Manager with Example

Last Updated : 31 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Clipboard Manager performs copy-and-paste operations within android applications. With the help of this user is able to copy and paste data across the different applications in android. In this article, we will look at How to implement Clipboard Manager in Android 13. A sample video is given below to get an idea about what we are going to do in this article.

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.

Step 2: Updating SDK version in build.gradle file

Navigate to Gradle Scripts>module level build.gradle file and add change compile SDK and target SDK to 33. After that simply sync your project to install it.

Step 3: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.

XML




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
    <!-- edit text on below line-->
    <EditText
        android:id="@+id/txtCopy"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/btnCopy"
        android:layout_centerHorizontal="true"
        android:layout_margin="10dp"
        android:hint="Type something..." />
  
    <!--Creating a button on below line-->
    <Button
        android:id="@+id/btnCopy"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_margin="10dp"
        android:text="Copy to Clipboard"
        android:textAllCaps="false" />
  
</RelativeLayout>


Step 4: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.

Kotlin




package com.example.gptapp
  
import android.R.attr
import android.annotation.SuppressLint
import android.app.LocaleManager
import android.app.StatusBarManager
import android.content.*
import android.content.pm.PackageManager
import android.graphics.drawable.Icon
import android.net.Uri
import android.net.wifi.WifiManager
import android.os.Build
import android.os.Bundle
import android.os.LocaleList
import android.os.PersistableBundle
import android.provider.MediaStore
import android.util.Log
import android.view.inputmethod.EditorInfo
import android.widget.*
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import com.android.volley.RequestQueue
import com.android.volley.Response
import com.android.volley.RetryPolicy
import com.android.volley.VolleyError
import com.android.volley.toolbox.JsonObjectRequest
import com.android.volley.toolbox.Volley
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.textfield.TextInputEditText
import com.squareup.picasso.Picasso
import org.json.JSONObject
import java.util.*
import java.util.jar.Manifest
  
class MainActivity : AppCompatActivity() {
  
    // creating variables on below line.
    lateinit var copyBtn: Button
  
    @RequiresApi(Build.VERSION_CODES.N)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        copyBtn = findViewById(R.id.btnCopy)
  
        // Action when the copy button is clicked
        copyBtn.setOnClickListener {
            
            // initializing clip board manager on below line.
            val clipboardManager =
                applicationContext.getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
              
            // initializing clip data on below line.
            val clipData = ClipData.newPlainText(
                "Clip Data",
                "Clip Data"
            )
                .apply {
                    // on below line adding description
                    description.extras = PersistableBundle().apply {
                        // only available for Android13 or higher
                        putBoolean(ClipDescription.MIMETYPE_TEXT_PLAIN, true)
                        // use raw string for older versions
                        // android.content.extra.IS_SENSITIVE
                    }
                }
              
              // on below line setting primary clip for clip board manager.
            clipboardManager.setPrimaryClip(clipData)
              
              // displaying toast message as text copied to clip board.
            Toast.makeText(applicationContext, "Copied to Clipboard", Toast.LENGTH_SHORT).show()
        }
    }
}


Now run your application to see its output of it. 

Output:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads