Search code examples
androidhyperlinktextview

Link in an Android textView not clickable


I would like to embed a link into a textView. However, the links are not underlined and not clickable.

I have the following string resource

    <string name="info_efficiency_link">
    <![CDATA[
    &#8226; Heat pumps are by far the most efficient heating technology.<br/><br/>
    &#8226; They convert 1 kWh of electricity into 3-4 kWh of heat.<br/><br/>
    &#8226; Ground-source heat pumps are more efficient than air-source heat pumps but also more expensive.<br/><br/>
    &#8226; By the way: Fridges and freezers use a process similar to a heat pump, moving heat from inside the appliance to the outside.<br/><br/>
    &#8226; Sources: <a href="https://www.entega.de/blog/wirkungsgrad-waermepumpe/">Prof. Dipl. –Ing. W. Schenk, Hochschule München, 2020</a> (accessed on 19.10.2024).<br/><br/>
    <a href="https://www.techem.de/blog/wirkungsgrad-waermepumpe/">Techem Energy Services GmbH, 2022</a> (accessed on 19.10.2024).<br/><br/>
    ]]>
</string>

THis is the xml layout file

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".FR_Options">

    <!-- HorizontalScrollView for scrolling horizontally -->
    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"> <!-- Ensures it uses full width even if content is small -->

        <!-- LinearLayout inside to arrange items horizontally -->
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="horizontal"> <!-- Set orientation to horizontal for scrolling -->

            <!-- First item: Caption, ImageView and TextView stacked vertically -->
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="vertical"
                android:padding="10dp">

                <!-- Caption TextView -->
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="@string/cap_efficiency"
                    android:textSize="13sp"
                    android:textStyle="bold" />

                <ImageView
                    android:layout_width="270dp"
                    android:layout_height="180dp"
                    android:layout_marginTop="10dp"
                    android:src="@drawable/facts_efficiency_english" />

                <TextView
                    android:id="@+id/tv_facts_efficiency"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textSize="9dp"
                    android:autoLink="web"
                    android:text="@string/info_efficiency_link"
                    android:linksClickable="true"
                    android:textColor="@android:color/black" />
            </LinearLayout>


        </LinearLayout>
    </HorizontalScrollView>

</FrameLayout>

with the relevant textView being android:id="@+id/tv_facts_efficiency". And here is the java file of the fragment

package com.example.game;

import androidx.fragment.app.Fragment;
import android.content.pm.ActivityInfo;
import android.os.Bundle;

import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.core.text.HtmlCompat;

import com.example.game.databinding.FragmentInterestingFactsBinding;

public class FR_InterestingFacts extends Fragment {

    private FragmentInterestingFactsBinding binding;

    public FR_InterestingFacts() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Set the screen orientation to landscape
        getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = FragmentInterestingFactsBinding.inflate(inflater, container, false);


        String formattedText = getString(R.string.info_efficiency_link);
        binding.tvFactsEfficiency.setText(HtmlCompat.fromHtml(formattedText, HtmlCompat.FROM_HTML_MODE_LEGACY));


        binding.tvFactsEfficiency.setMovementMethod(LinkMovementMethod.getInstance());

        return binding.getRoot();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}

Do you know how to solve this issue? The links are not displayed and nothing happens when you click on them. I tried it with two different emulators.


Solution

  • To fix this issue you have to remove the property android:autoLink="web" from the TextView as you call the setMovementMethod() to activate the click event. Also the attribute android:linksClickable="true" is not needed as by default the TextView has this value enabled to True.

    So the xml should look like the below:

    <TextView
        android:id="@+id/tv_facts_efficiency"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="9dp"
        android:text="@string/info_efficiency_link"
        android:textColor="@android:color/black" /> 
    

    And the Java code you already have is correct:

    String formattedText = getString(R.string.info_efficiency_link);
    tvFactsEfficiency.setText(HtmlCompat.fromHtml(formattedText, HtmlCompat.FROM_HTML_MODE_LEGACY));
    tvFactsEfficiency.setMovementMethod(LinkMovementMethod.getInstance());