이 예제는 안드로이드 앱에서 TextView 내부의 텍스트를 클릭 가능한 하이퍼링크로 만드는 방법을 단계별로 소개합니다. 핵심은 strings.xml에 <a> 태그로 링크를 정의하고, 자바 코드에서 LinkMovementMethod를 설정하는 것입니다.
1단계 — 새 프로젝트 생성
Android Studio에서 File ⇒ New Project를 선택해 새 프로젝트를 만들고, 프로젝트 생성에 필요한 모든 세부 정보를 입력합니다.
2단계 — 레이아웃 작성 (res/layout/activity_main.xml)
activity_main.xml 파일에 아래 코드를 추가합니다. 화면 중앙에 링크가 포함된 TextView를 배치하는 구조입니다.
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/textViewLink" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:textSize="24sp" android:textStyle="bold" android:gravity="center" android:text="@string/messageWithLink" /> </RelativeLayout>
3단계 — 문자열 리소스에 링크 추가 (res/values/strings.xml)
strings.xml 파일을 열고 다음과 같이 <a href> 태그가 포함된 문자열을 추가합니다. 이 태그가 TextView에 표시될 클릭 가능한 링크를 정의하는 역할을 합니다.
<resources> <string name="app_name">Sample</string> <string name="messageWithLink"><a href="https://www.google.com/">Tap here to open Link</a></string> </resources>
4단계 — MainActivity에 LinkMovementMethod 설정 (src/MainActivity.java)
MainActivity.java에 아래 코드를 추가합니다. setMovementMethod(LinkMovementMethod.getInstance()) 호출이 링크 클릭 이벤트를 처리할 수 있게 해주는 핵심 부분입니다. 이 설정이 없으면 <a> 태그가 있어도 링크가 동작하지 않습니다.
package app.com.sample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.method.LinkMovementMethod;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textViewLink);
textView.setMovementMethod(LinkMovementMethod.getInstance());
}
}5단계 — AndroidManifest.xml 확인
androidManifest.xml에 아래 코드를 추가하거나, 프로젝트 생성 시 자동으로 만들어진 내용이 올바른지 확인합니다.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="https://schemas.android.com/apk/res/android" package="app.com.sample"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
앱 실행 및 결과 확인
이제 애플리케이션을 실행해 보겠습니다. 실제 안드로이드 모바일 기기가 컴퓨터에 연결되어 있다고 가정합니다. Android Studio에서 프로젝트의 액티비티 파일 중 하나를 연 뒤 툴바의 실행(Run) 아이콘을 클릭하고, 목록에서 본인의 모바일 기기를 선택합니다. 그러면 기기 화면에 아래와 같이 클릭 가능한 링크가 포함된 기본 화면이 표시됩니다.

참고: autoLink 속성으로 더 간단하게 처리하기
텍스트에 포함된 URL을 자동으로 인식하게 하려면 XML에서 android:autoLink="web" 속성을 사용하는 방법도 있습니다. 다만 특정 문구에만 링크를 걸거나 링크 텍스트를 직접 제어하고 싶다면, 위 예제처럼 <a> 태그와 LinkMovementMethod를 조합하는 방식이 더 적합합니다.