artist
artist

Reputation: 6371

paste option for edittext

I have an edittext and I would like to paste some text in it. I can copy the text from some web page but I am not able to paste the text in my edittext control.How can I enable my edittext to paste some text.Here is my main.xml for edittext ;

enter code here

<EditText 
   android:id="@+id/enter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight= "2"
android:scrollbars="vertical"
android:textColor="@color/black"
/>

Thanks

Upvotes: 7

Views: 15710

Answers (4)

Tarit Ray
Tarit Ray

Reputation: 994

To enable the standard copy/paste for TextView, U can choose one of the following: Change in layout file: Either add below property to your TextView

android:textIsSelectable="true"

and In your Java class write this line to set it programmatically.

myTextView.setTextIsSelectable(true);

if fragment try with

mContext.myTextView.setTextIsSelectable(true);

And long press on the TextView you can see copy/paste action bar.

Upvotes: 3

dineth
dineth

Reputation: 9942

Try setting the inputType="text" for the EditText field

Upvotes: 0

Kerem
Kerem

Reputation: 2897

This is on Android 4.4.2 Samsung S4;

Documentation for TextView says that:

To allow users to copy some or all of the TextView's value and paste it somewhere else, set the XML attribute android:textIsSelectable to "true" or call setTextIsSelectable(true). The textIsSelectable flag allows users to make selection gestures in the TextView, which in turn triggers the system's built-in copy/paste controls.

There is also another Textview attribure called android:cursorVisible which determines if the system should be invoked about the copy/paste callbacks.

By default I believe both of these are true and selection/copy/paste mechanics are already enabled. I could not change that behaviour by using android:textIsSelectable="false" but if I set android:cursorVisible="false" initially you can't paste anything inside the EditText. Only after you type something in, cursor and selection behaviour becomes enabled again. Maybe this should be handled inside the code rather than in the layout xmls, or it might be related to android:inputType which also did not make a difference for me.

So try setting android:cursorVisible="true" in your EditText's layout xml if paste is not enabled by default.

Upvotes: 8

Pir Fahim Shah
Pir Fahim Shah

Reputation: 10623

According to your problem if you copied some data any where in your system and you want to paste it in some specific variable, like Edit TextBox, Textview etc, then this code will surely help you.

 ClipboardManager clipMan = (ClipboardManager)getSystemService(v.getContext().CLIPBOARD_SERVICE);
 myEdtTxt.setText(clipMan.getText());

Note:- here the clipMan object will store the data whenever copied process take place and we will return that data from that object and will set it,

Upvotes: 3

Related Questions