Skip to content

Instantly share code, notes, and snippets.

@ParkSangGwon
Created August 10, 2016 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ParkSangGwon/c16913b4b786e57d3ae6cc0e247c2c39 to your computer and use it in GitHub Desktop.
Save ParkSangGwon/c16913b4b786e57d3ae6cc0e247c2c39 to your computer and use it in GitHub Desktop.
Linkify로 TextView의 특정단어 클릭시 URL이동시키기
<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"
android:padding="@dimen/activity_vertical_margin"
>
<TextView
android:id="@+id/tvLinkify"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tvLinkify = (TextView) findViewById(R.id.tvLinkify);
String text = "안녕하세요. 셀폰서비스를 운영중인 박상권입니다.저는 박상권의 삽질블로그를 운영하고 있습니다.";
tvLinkify.setText(text);
Linkify.TransformFilter mTransform = new Linkify.TransformFilter() {
@Override
public String transformUrl(Matcher match, String url) {
return "";
}
};
Pattern pattern1 = Pattern.compile("셀폰");
Pattern pattern2 = Pattern.compile("박상권의 삽질블로그");
Linkify.addLinks(tvLinkify, pattern1, "http://selphone.co.kr",null,mTransform);
Linkify.addLinks(tvLinkify, pattern2, "http://gun0912.tistory.com/",null,mTransform);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment