AndroidのLinkifyとは?TextViewにリンク機能を追加する方法をわかりやすく解説
Linkify(リンキファイ)とは
実装例に入る前に、まず「Linkify」について簡単に説明します。Linkifyとは、HTMLにおけるハイパーリンクのようなもので、テキスト内の特定の文字列をクリック可能なリンクへと自動変換できるAndroidの機能です。この機能を利用すれば、TextViewに表示したURLやメールアドレス、電話番号などを、ユーザーがタップできるリンクにすることができます。
ここでは、AndroidのTextViewでLinkifyを使うシンプルな方法を紹介します。
実装手順
ステップ1:新規プロジェクトの作成
Android Studioで新しいプロジェクトを作成します。メニューから「File」→「New Project」を選択し、必要な項目を入力してプロジェクトを作成してください。
ステップ2:レイアウトファイルの編集
res/layout/activity_main.xml に以下のコードを追加します。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Result Data"
android:textSize="20sp"
android:padding="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
上記のXMLでは、テキストを表示するためのTextViewを1つ定義しています。このTextViewに後ほどコード側からURLを含むテキストを設定し、リンク化していきます。
ステップ3:MainActivity.java の編集
src/MainActivity.java に以下のコードを追加します。
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.util.Linkify;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.result);
textView.setText("TutorialsPoint.com originated from the idea that there exists a class of readers who respond better to online content and prefer to learn new skills at their ow...");
Linkify.addLinks(textView, Linkify.WEB_URLS);
}
}
上記のコードでは、TextViewに「tutorialspoint.com」というURLを含むテキストを設定しています。AndroidでLinkifyを呼び出すには Linkify.addLinks() メソッドを使用し、第1引数にTextView、第2引数にLinkifyMaskを渡します。
LinkifyMaskの種類
LinkifyMaskにはいくつかの種類があり、用途に応じて使い分けることができます。
Linkify.WEB_URLS:URLをWebリンクとして認識します。ユーザーがタップすると、そのURLがデフォルトのWebブラウザに渡されます。
Linkify.EMAIL_ADDRESSES:メールアドレスをリンクとして認識します。タップすると、端末のデフォルトのメールアプリが起動します。
Linkify.PHONE_NUMBERS:電話番号をリンクとして認識します。タップすると、電話番号がデフォルトのダイヤラーに渡されます。
Linkify.ALL:WEB_URLS、EMAIL_ADDRESSES、PHONE_NUMBERS のすべてをまとめて有効にします。
ステップ4:マニフェストファイルの編集
manifest.xml に以下のコードを追加します。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.andy.myapplication">
<uses-permission android:name="android.permission.INTERNET"/>
<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からアプリを実行するには、プロジェクト内のアクティビティファイルを開き、ツールバーの「Run」アイコンをクリックします。接続したモバイルデバイスを選択すると、端末の画面にアプリの初期画面が表示されます。

上の画面が表示されたら、リンク化されたテキストをタップしてみてください。以下のように、Webサイトがブラウザで開きます。

リンクをタップすると、次の画像のようにデフォルトのWebブラウザで該当のWebサイトが表示されます。

-
ストックAndroidとは?「バニラAndroid」の特徴とメリットを徹底解説
ストックAndroidは「バニラAndroid」とも呼ばれ、Android OSの中で最もシンプルな基本バージョンを指します。Googleが設計・開発したAndroidのコアをそのまま搭載しており、最大の特徴は、キャリア(通信事業者)が独自にプリインストールするアプリが含まれていない点です。例えば、Verizonの回線で使うストックAndroid端末には、Verizon製のアプリは入っていません。多くのユーザーにとって、こうした不要なアプリ(ブロートウェア)が存在しないことは大きな魅力であり、端末を自分の思い通りにコントロールする自由を取り戻せるのです。 また、ストックAndroidは、端末
-
Android System WebView(ASW)とは?役割と更新方法を徹底解説
Android端末のアプリ一覧や、Google Playストアのアップデート通知で「Android System WebView(AndroidシステムWebView)」という名前を目にしたことがあるかもしれません。この重要なシステムアプリは、ユーザーが直接インストールしたり操作したりするものではありませんが、Android OSにとって欠かせない存在です。 お使いのAndroidのバージョンによって、Android System WebViewはさまざまな形で提供されています。どの端末でも、ASW(またはその派生版)を有効かつ最新の状態に保つ必要があります。本記事では、その理由と、このシス