0
0

More than 1 year has passed since last update.

Android TextViewリンク多言語対応するときの色々

Last updated at Posted at 2021-11-19

はじめに

androidのTextViewリンク化(多言語)について、自分が下記のものを調べたけど、
うまく行けなかったので、色々を試して、簡単に共有します。

問題点

TextView内の多言語リンクうまく表示できないこと

resourcesファイル

<resources xmlns:tools="http://schemas.android.com/tools">
   <string name="link_text">Here is <a href="http://www.google.com">google</a> page </string>
</resources>

xmlファイル

<TextView
                    android:id="@+id/link_text"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:autoLink="web"
                    android:linksClickable="true"
                    android:gravity="center"
                    android:textSize="18sp" />

fragmentファイル

val linkText = binding.linkText
        linkText.isClickable = true
        linkText.movementMethod = LinkMovementMethod.getInstance()
        val infoText =
            rootContext.getString(R.string.link_text)
        linkText.text =
            Html.fromHtml(infoText, HtmlCompat.FROM_HTML_MODE_COMPACT)

結果下記の図

スクリーンショット 2021-11-19 11.13.40.png

googleのリンク化されない

解決方法

xmlファイルの
android:autoLink="web"
android:linksClickable="true"

この二つが要らない。

注意点として:
HTML文字列は、バックエンドまたはリソースファイルから取得できます。
テキストをリソース文字列として配置する場合は、必ずCDATAタグを追加してください。

例:
<string name="your_text">normal text & <![CDATA[<a href="http://www.google.com">link text</a>]]>.....</string>`

<TextView
                    android:id="@+id/link_text"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:autoLink="web"
                    android:linksClickable="true"
                    android:gravity="center"
                    android:textSize="18sp" />

今回の例

  <string name="link_text">Here is <![CDATA[<a href="http://www.google.com">google</a>]]> page </string>

スクリーンショット 2021-11-19 11.20.55.png

HtmlCompatすごく使いやすいものと思いますので、引き続きこの記事で更新する予定です。

参考文献

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0