- Android - Intents/Filters
- Android - Fragments
- Android - Content Providers
- Android - Broadcast Receivers
- Android - Services
- Android - Activities
- Android - Resources
- Android - Hello World Example
- Android - Application Components
- Android - Architecture
- Android - Environment Setup
- Android - Overview
- Android - Home
Android - User Interface
- Android - Custom Components
- Android - Styles and Themes
- Android - Event Handling
- Android - UI Controls
- Android - UI Layouts
Android Advanced Concepts
- Publishing Android Application
- Android - Phone Calls
- Android - Sending SMS
- Android - Sending Email
- Location Based Services
- Android - Notifications
- Android - Drag and Drop
Android Useful Examples
- Android - XML Parsers
- Android - Widgets
- Android - Wi-Fi
- Android - WebView Layout
- Android - UI Testing
- Android - UI Patterns
- Android - UI Design
- Android - Twitter Integration
- Android - TextureView
- Android - Text to Speech
- Android - Testing
- Android - Support Library
- Android - SQLite Database
- Android - Spelling Checker
- Android - SIP Protocol
- Android - Shared Preferences
- Android - Session Management
- Android - Sensors
- Android - SDK Manager
- Android - Screen Cast
- Android - RSS Reader
- Android - RenderScript
- Android - Push Notification
- Android - ProgressBar
- Android - Progress Circle
- Android - PHP/MySQL
- Android - NFC Guide
- Android - Network Connection
- Android - Navigation
- Android - Multitouch
- Android - MediaPlayer
- Android - Login Screen
- Android - Localization
- Android - Loading Spinner
- Android - Linkedin Integration
- Android - JSON Parser
- Android - JetPlayer
- Android - Internal Storage
- Android - ImageSwitcher
- Android - Image Effects
- Android - Google Maps
- Android - Gestures
- Android - Facebook Integration
- Android - Emulator
- Android - Developer Tools
- Android - Data Backup
- Android - Custom Fonts
- Android - Clipboard
- Android - Camera
- Android - Bluetooth
- Android - Best Practices
- Android - Auto Complete
- Android - AudioManager
- Android - Audio Capture
- Android - Animations
- Android - Alert Dialoges
Android Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Android - Cppboard
Android provides the cppboard 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 pbrary of CppboardManager and CppData and CppData.item to use the copying and pasting framework.In order to use cppboard framework, you need to put data into cpp object, and then put that object into system wide cppboard.
In order to use cppboard , you need to instantiate an object of CppboardManager by calpng the getSystemService() method. Its syntax is given below −
CppboardManager myCppboard; myCppboard = (CppboardManager)getSystemService(CLIPBOARD_SERVICE);
Copying data
The next thing you need to do is to instantiate the CppData object by calpng the respective type of data method of the CppData class. In case of text data , the newPlainText method will be called. After that you have to set that data as the cpp of the Cppboard Manager object.Its syntax is given below −
CppData myCpp; String text = "hello world"; myCpp = CppData.newPlainText("text", text); myCppboard.setPrimaryCpp(myCpp);
The CppData object can take these three form and following functions are used to create those forms.
Sr.No | CppData Form & Method |
---|---|
1 |
Text newPlainText(label, text) Returns a CppData object whose single CppData.Item object contains a text string. |
2 |
URI newUri(resolver, label, URI) Returns a CppData object whose single CppData.Item object contains a URI. |
3 |
Intent newIntent(label, intent) Returns a CppData object whose single CppData.Item object contains an Intent. |
Pasting data
In order to paste the data, we will first get the cpp by calpng the getPrimaryCpp() method. And from that cpck we will get the item in CppData.Item object. And from the object we will get the data. Its syntax is given below −
CppData abc = myCppboard.getPrimaryCpp(); CppData.Item item = abc.getItemAt(0); String text = item.getText().toString();
Apart from the these methods , there are other methods provided by the CppboardManager class for managing cppboard framework. These methods are psted below −
Sr.No | Method & description |
---|---|
1 |
getPrimaryCpp() This method just returns the current primary cpp on the cppboard |
2 |
getPrimaryCppDescription() This method returns a description of the current primary cpp on the cppboard but not a copy of its data. |
3 |
hasPrimaryCpp() This method returns true if there is currently a primary cpp on the cppboard |
4 |
setPrimaryCpp(CppData cpp) This method sets the current primary cpp on the cppboard |
5 |
setText(CharSequence text) This method can be directly used to copy text into the cppboard |
6 |
getText() This method can be directly used to get the copied text from the cppboard |
Example
Here is an example demonstrating the use of CppboardManager class. It creates a basic copy paste apppcation that allows you to copy the text and then paste it via cppboard.
To experiment with this example , you can run this on an actual device or in an emulator.
Steps | Description |
---|---|
1 | You will use Android studio IDE to create an Android apppcation and under a package com.example.sairamkrishna.myapppcation. |
2 | Modify src/MainActivity.java file to add necessary code. |
3 | Modify the res/layout/activity_main to add respective XML components |
4 | Run the apppcation and choose a running android device and install the apppcation on it and verify the results |
Following is the content of the modified main activity file src/MainActivity.java.
package com.example.sairamkrishna.myapppcation; import android.content.CppData; import android.content.CppboardManager; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; pubpc class MainActivity extends ActionBarActivity { EditText ed1, ed2; Button b1, b2; private CppboardManager myCppboard; private CppData myCpp; @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); myCppboard = (CppboardManager) getSystemService(CLIPBOARD_SERVICE); b1.setOnCpckListener(new View.OnCpckListener() { @Override pubpc void onCpck(View v) { String text; text = ed1.getText().toString(); myCpp = CppData.newPlainText("text", text); myCppboard.setPrimaryCpp(myCpp); Toast.makeText(getApppcationContext(), "Text Copied", Toast.LENGTH_SHORT).show(); } }); b2.setOnCpckListener(new View.OnCpckListener() { @Override pubpc void onCpck(View v) { CppData abc = myCppboard.getPrimaryCpp(); CppData.Item item = abc.getItemAt(0); String text = item.getText().toString(); ed2.setText(text); Toast.makeText(getApppcationContext(), "Text Pasted", Toast.LENGTH_SHORT).show(); } }); } }
Following is the modified content of the xml res/layout/activity_main.xml.
<?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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <TextView android:text="Example" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textview" android:textSize="35dp" android:layout_apgnParentTop="true" android:layout_centerHorizontal="true" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tutorials point" android:id="@+id/textView" android:layout_below="@+id/textview" android:layout_centerHorizontal="true" android:textColor="#ff7aff24" android:textSize="35dp" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView" android:src="@drawable/abc" android:layout_below="@+id/textView" android:layout_centerHorizontal="true" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/editText" android:layout_apgnParentRight="true" android:layout_apgnParentEnd="true" android:hint="Copy text" android:layout_below="@+id/imageView" android:layout_apgnLeft="@+id/imageView" android:layout_apgnStart="@+id/imageView" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/editText2" android:layout_apgnLeft="@+id/editText" android:layout_apgnStart="@+id/editText" android:hint="paste text" android:layout_below="@+id/editText" android:layout_apgnRight="@+id/editText" android:layout_apgnEnd="@+id/editText" /> <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_apgnLeft="@+id/editText2" android:layout_apgnStart="@+id/editText2" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Paste text" android:id="@+id/button2" android:layout_below="@+id/editText2" android:layout_apgnRight="@+id/editText2" android:layout_apgnEnd="@+id/editText2" /> </RelativeLayout>
Following is the content of the res/values/string.xml.
<resources> <string name="app_name">My Apppcation</string> </resources>
Following is the content of AndroidManifest.xml file.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.sairamkrishna.myapppcation" > <apppcation android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.sairamkrishna.myapppcation.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </apppcation> </manifest>
Let s try to run our an apppcation we just modified. I assume you had created your AVD while doing environment setup. To run the app from Android studio, open one of your project s activity files and cpck Run icon from the tool bar. Android studio installer will display following images −
Now just enter any text in the Text to copy field and then select the copy text button. The following notification will be displayed which is shown below −
Now just press the paste button, and you will see the text which is copied is now pasted in the field of Copied Text. It is shown below −
Advertisements