KotlinでAndroidのTextViewにクリック可能なリンクを作成する方法を解説
この記事では、Kotlinを使ってAndroidアプリのTextView内にクリック可能なリンクを作成する方法を、サンプルコードとともにわかりやすく解説します。
TextViewにHTMLのアンカータグ(<a>タグ)を含む文字列を設定し、LinkMovementMethodを適用することで、テキスト中のリンク部分をタップしてブラウザを開けるようになります。それでは、手順を見ていきましょう。
手順1:新規プロジェクトを作成する
Android Studioを起動し、「File」→「New Project」を選択して、必要な情報を入力して新しいプロジェクトを作成します。
手順2:レイアウトファイル(activity_main.xml)を編集する
res/layout/activity_main.xml に以下のコードを追加します。
コード例
<?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:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="50dp" android:text="Tutorials Point" android:textAlignment="center" android:textColor="@android:color/holo_green_dark" android:textSize="32sp" android:textStyle="bold" /> <TextView android:id="@+id/textViewLink" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:gravity="center" android:text="@string/messageWithLink" android:textSize="24sp" android:textStyle="bold" /> </RelativeLayout>
このレイアウトには、画面上部に表示されるタイトル用のTextViewと、画面中央に配置されたリンク付きテキストを表示するTextView(textViewLink)の2つが含まれています。
手順3:文字列リソース(strings.xml)にリンクを定義する
res/values/strings.xml を開き、以下のコードを追加します。
<resources> <string name="app_name">Your app Name</string> <string name="messageWithLink"><a href="https://www.google.com/">Tap here to open Google</a></string> </resources>
ポイントは、文字列リソースの中に直接 <a href=...> タグを記述している点です。これにより、TextViewがこの部分をHTMLのリンクとして認識するようになります。
手順4:MainActivity.ktにコードを追加する
src/MainActivity.kt に以下のコードを記述します。
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.method.LinkMovementMethod
import android.widget.TextView
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
val textView: TextView = findViewById(R.id.textViewLink)
textView.movementMethod = LinkMovementMethod.getInstance()
}
}
LinkMovementMethod.getInstance() をTextViewのmovementMethodに設定することが重要です。この設定を忘れると、リンクが表示されていてもタップしても反応しないため注意してください。
手順5:AndroidManifest.xmlを確認する
androidManifest.xml は以下の通りです。今回の実装に特別なパーミッションは不要ですが、ランチャーアクティビティの設定が正しく行われているか確認しておきましょう。
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="https://schemas.android.com/apk/res/android" package="com.example.q11"> <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端末をパソコンに接続しているものとします。Android Studioからアプリを実行するには、プロジェクト内のいずれかのアクティビティファイルを開き、ツールバーの実行アイコン
をクリックします。オプションとして接続済みのモバイルデバイスを選択すると、実機の画面にアプリが表示されます。
アプリを起動すると、画面中央に「Tap here to open Google」というテキストが表示され、そのリンクをタップするとブラウザでGoogleのページが開きます。


-
Androidでスレッドを作成する方法を徹底解説!初心者向けサンプルコード付き
Androidにおけるスレッドとは?スレッドとは、軽量なサブプロセスのことであり、UI(ユーザーインターフェース)を中断させることなくバックグラウンド処理を実行するために使用されます。Androidアプリ開発では、時間のかかる処理をメインスレッドから切り離すことで、アプリの応答性や操作性を維持することができます。本記事では、実際のサンプルコードを交えながら、Androidでスレッドを作成する方法をステップごとに分かりやすく解説します。手順1:Android Studioで新規プロジェクトを作成するまず、Android Studioを起動し、メニューから「File」→「New Project」を
-
AndroidのTextViewでクリック可能なリンクを作成する方法
このチュートリアルでは、AndroidアプリのTextViewに表示したテキストの一部を、タップできるリンク(クリック可能なリンク)にする方法を解説します。文字列リソースにHTMLの<a>タグを記述し、LinkMovementMethodを設定するだけで、簡単に実装できます。 完成イメージ 画面の中央に太字のメッセージが表示され、「Tap here to open Link」というリンク部分をタップすると、デフォルトのブラウザが起動してGoogleのページが開きます。 手順1:新規プロジェクトを作成する Android Studioで新しいプロジェクトを作成します。メニューから