반응형

텍스트에 하이퍼링크 적용 방법


① strings.xml에 URL을 포함 한 경우

 

- strings.xml

<resources>
    <string name="hyper_link_with_url">http://junn97.com</string>
</resources>

 

- activity_main.xml

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hyper_link_with_url"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:autoLink="web"/>

 

android:text = "@string/hyper_link_with_url"

android:autoLink="web"


② 원하는 텍스트로 설정 후 연결 할 경우

 

- strings.xml

 

<resources>
    <string name="hyper_link_with_text"><a href="http://junn97.com">내 블로그</a></string>
</resources>

 

- actvity_main.xml

    <TextView
        android:id="@+id/tvHyperLink"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hyper_link_with_text"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

 

- MainActivity.java

public class MainActivity extends AppCompatActivity {

    private TextView mtvHyperLink;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mtvHyperLink = findViewById(R.id.tvHyperLink);
        mtvHyperLink.setMovementMethod(LinkMovementMethod.getInstance());
    }
}

 

XML 레이아웃에 android:autoLink="web"이 설정되어 있다면, 이는 프로그래밍 방식으로 설정한 동작을 무시하고 autoLink에서 인식된 링크만이 클릭 가능하게 만듭니다. 

 

즉, XML 레이아웃에서 자동으로 감지되지 않는 링크는 프로그래밍 방식으로 설정한 이동 방법에도 불구하고 클릭할 수 없게 됩니다.

 

참조

https://tutorial.eyehunts.com/android/how-to-make-links-in-textview-clickable-hyperlink-android-kotlin-java/

 

How to make links in a TextView clickable HyperLink Android Kotlin/Java

To make a TextView clickable(hypertext link) in Android you can do it in value Strings file or calling setMovementMethod().

tutorial.eyehunts.com

 

반응형

+ Recent posts