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 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: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);
}
}



Make TextView selectable, select and copy Make TextView selectable, select and copy Reviewed by Pendik on 22.03 Rating: 5

Tidak ada komentar:

Diberdayakan oleh Blogger.