android, Linkify 를 이용하여 TextView 에 link 주기 |
HashTag 예제
<기본>
// Hash Tag 패턴
Pattern tagMatcher = Pattern.compile("[#]+[A-Za-z0-9-_]+\\b");
// TextView 의 Link 를 눌렀을 때 연결할 uri 혹은 url base 주소
String uri = "content://com.cklee.hashtags/";
// textview 에서 tagMatcher 에 matching 되는 녀석을 link 로 만들고,
// 이녀석을 클릭하면 ACTION_VIEW 를 호출하면서 MIME type 을 uri + tag 로 생성된 scheme 으로부터 받아온다.
Linkify.addLinks(textView, tagMatcher, uri ); // textView has hashTag string
< Manifest >
...
<provider
android:name=".HashTagProvider"
android:authorities="com.cklee.hashtags" />
<activity android:name=".HashTagDetailActivity">
<intent-filter >
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="vnd.android.cursor.item/vnd.cc.hashtag" />
</intent-filter>
</activity>
...
<provider>
// In HashTagProvider which extends ContentProvider
@Override
public String getType(Uri arg0) {
//MIME type of the data represented by URI
return "vnd.android.cursor.item/vnd.cc.hashtag";
}
전화번호, 이메일 주소, 웹 URL 연결
<기본>
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
spannableStringBuilder.append( bodyString );
Linkfy.addLinks( spannableStringBuilder , Pattens.PHONE, "myphone:" );
Linkfy.addLinks( spannableStringBuilder , Pattens.EMAIL_ADDRESS, "myemail:" );
Linkfy.addLinks( spannableStringBuilder , Pattens.WEB_URL, "myweburl:" );
mTextView.setText(spannableStringBuilder);
mTextView.setMovementMethod( LinkMovementMethod.getInstance() );
<Manifest>
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.BROWSABLE"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:scheme="myphone"/>
<data android:scheme="myemail"/>
<data android:scheme="myweburl"/>
</intent-filter>
<Activity>
if ( getIntent().getScheme().equals( "myphone" ) ){
// do sth...
}
댓글