Make TextView selectable select and copy


Example to make TextView selectable, you can select and copy text from TextView and paste to other field, such as EditText.


Add android_textIsSelectable="true" in layout XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout

android_layout_width="match_parent"
android_layout_height="match_parent"
android_padding="16dp"
android_orientation="vertical"
tools_context="com.blogspot.android_er.androidselectabletext.MainActivity">

<TextView
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_gravity="center_horizontal"
android_autoLink="web"
android_text="http://android-er.blogspot.com/"
android_textStyle="bold" />

<TextView
android_id="@+id/textview1"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_textSize="28sp"
android_text="Default TextView, un-selectable"/>

<TextView
android_id="@+id/textview2"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android:textIsSelectable="true"
android_textSize="28sp"
android_text="Selectable TextView, by XML"/>

<TextView
android_id="@+id/textview3"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_textSize="28sp"
android_text="Selectable TextView, by Java code"/>

<EditText
android_id="@+id/edittext"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_textSize="28sp"/>
</LinearLayout>


or call setTextIsSelectable(true) method of the TextView object in Java code:
package com.blogspot.android_er.androidselectabletext;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

TextView textView1, textView2, textView3;
EditText editText;

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

textView1 = (TextView)findViewById(R.id.textview1);
textView2 = (TextView)findViewById(R.id.textview2);
textView3 = (TextView)findViewById(R.id.textview3);
textView3.setTextIsSelectable(true);
editText = (EditText)findViewById(R.id.edittext);
}
}



Related Posts by Categories

0 comments:

Post a Comment

Blog Archive

Powered by Blogger.