Android Tutorial , Programming Tutorial, Php Tutorial, Learn Android, HTML Tutorial, Coding , Java Tutorial, GTU Programs, Learning Programming

Tuesday, 11 October 2016

Copying and Pasting Text in an Edit Text Control in Android

In this example, We will learn that how to copy data from editText and paste data.

For copying and pasting different types of data , Android provides clipboard framework. In this example, we use ClipboardManager class which is used to allow you to copy and paste text via clipboard.

First of all that, You input some data in editText, then click on Copy Button to copy text from edit text. After for paste this copied , You click on Paste button. You will display copied text in text view after clicked on Paste Button.

Let's see the Example :



File Name : MainActivity.java



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.example.copypastedemo;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity {

 EditText ed1;
 TextView t1;
 Button b1,b2;
 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.editText1);
        t1 = (TextView)findViewById(R.id.textView1);
        
        b1=(Button)findViewById(R.id.button1);
        b2=(Button)findViewById(R.id.button2);
                
        myClipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
        
        b1.setOnClickListener(new View.OnClickListener() {
   
   @Override
   public void onClick(View arg0) {
    // TODO Auto-generated method stub
   
    String text;
             text = ed1.getText().toString();
             myClip = ClipData.newPlainText("text", text);
             myClipboard.setPrimaryClip(myClip);
             
             Toast.makeText(getApplicationContext(), "Text Copied",Toast.LENGTH_SHORT).show();
    
    

   }
  });
        
        b2.setOnClickListener(new View.OnClickListener() {
   
   @Override
   public void onClick(View arg0) {
    // TODO Auto-generated method stub
   
    ClipData abc = myClipboard.getPrimaryClip();
             ClipData.Item item = abc.getItemAt(0);
             
             String text = item.getText().toString();
             t1.setText(text);
             
             Toast.makeText(getApplicationContext(), "Text Pasted",Toast.LENGTH_SHORT).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.activity_main, menu);
        return true;
    }
    
}

File Name : activity_main.xml



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<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"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="48dp"
        android:layout_marginTop="76dp"
        android:ems="10"
        android:hint="" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_centerVertical="true"
        android:text="@string/b1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button1"
        android:layout_alignBottom="@+id/button1"
        android:layout_centerHorizontal="true"
        android:text="@string/b2" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="20dp"
        android:text="Paste Text Here" />

</RelativeLayout>

Output :

Copy and Paste Text in Edit Text in Android     Copy and Paste Text in Edit Text in Android2

0 comments:

Post a Comment

Like us on Facebook

Site Visitor

Powered by Blogger.