728x90

Android RecyclerView TextView 에 보여줄 내용에 자동으로 Link를 거는 방법을 찾아봤다.


간단하게

<TextView
    android:id="@+id/tv_chatMsg"
    android:text="Hello"
    android:textSize="14dp"
    android:autoLink="phone|web|email|map"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentTop="true"
    android:background="@drawable/chat_from"
    />


한줄을 추가해주면 되더라.


하이퍼링크 색상은 지정하지 않으면 빨간색인데 별도 색으로 지정하고 싶으면 android:textColorLink="@color/blue" 를 해주면 된다.


텍스트를 클립보드로 복사하는 기능을 추가하고 싶다면

<TextView
    android:id="@+id/tv_chatMsg"
    android:text="Hello"
    android:textSize="14dp"
    android:textColorLink="@color/blue"
    android:textIsSelectable="true"
    android:autoLink="all"
    android:layout_marginTop="3dp"
    android:layout_below="@id/tv_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />

이 한줄을 추가해 주면 된다. 이 속성은 API Level 11부터 지원된다.

자바 코드에서는 msg.setTextIsSelectable(true); 로 설정해주면 된다.
xml 에서 android:textIsSelectable="true" 를 추가하거나 자바 코드에서 추가하거나 둘 중 하나면 하면 된다.


특정 단어를 눌렀을 때 URL 이 동작되도록 하고 싶은 경우에는 이 방법으로는 해결이 안된다.

특정 단어를 눌렀을 때는 http://gun0912.tistory.com/66 박상권님 블로그 참조.


전화번호를 눌렀을 때 바로 전화를 거는 게 아니라 문자를 보낼 수도 있고 복사를 하고 싶은 경우도 있다면....

https://blog.uncommon.is/a-better-way-to-handle-links-in-textview-27bb70b2d31c 참조


msg = (TextView) itemView.findViewById(R.id.tv_chatMsg);
msg.setMovementMethod(BetterLinkMovementMethod.getInstance());
Linkify.addLinks(msg,Linkify.ALL);

로 해봤지만 단순 autoLink 와 변화된 것은 없다.

블로그 이미지

Link2Me

,