IT Share you

Android에서 하이퍼 링크 텍스트보기 만들기

shareyou 2020. 11. 12. 20:37
반응형

Android에서 하이퍼 링크 텍스트보기 만들기


Google 과 같은 textview 텍스트에 대한 링크를 만들고 싶습니다 . 어쨌든 이와 같은 링크를 만들 수 있습니까? (ie) Google이라는 단어를 클릭하면 적절한 링크가 열립니다. 어떤 아이디어라도 환영합니다.


이것을 시도하고 무슨 일이 일어나는지 알려주십시오 ..

자바 코드 사용 :

TextView textView =(TextView)findViewById(R.id.textView);
textView.setClickable(true);
textView.setMovementMethod(LinkMovementMethod.getInstance());
String text = "<a href='http://www.google.com'> Google </a>";
textView.setText(Html.fromHtml(text));

API 레벨> = 24부터는 Html.fromHtml(String source)더 이상 사용되지 않습니다. 대신 fromHtml(String, int),

textView.setText(Html.fromHtml(text, Html.FROM_HTML_MODE_COMPACT));

또는 레이아웃 xml 파일에서 TextView 위젯 속성 내부

android:autoLink="web"
android:linksClickable="true"

android:autoLink="web"TextView의 xml에서 사용 하십시오. 클릭 가능한 URL을 자동으로 변환해야합니다 (텍스트에있는 경우).


모든 테스트 및 작동 100 %
솔루션 : android:autoLink="web"
아래는 전체 예제

샘플 레이아웃 Xml입니다.

    <TextView
        android:id="@+id/txtLostpassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:autoLink="email"
        android:gravity="center"
        android:padding="20px"
        android:text="@string/lostpassword"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/txtLostpassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:autoLink="web"
        android:gravity="center"
        android:padding="20px"
        android:text="@string/defaultpassword"
        android:textAppearance="?android:attr/textAppearanceSmall" />

string.xml의 문자열

<string name="lostpassword">If you lost your password please contact <a href="mailto:support@cleverfinger.com.au?Subject=Lost%20Password" target="_top">support@cleverfinger.com.au</a></string>

<string name="defaultpassword">User Guide <a href="http://www.cleverfinger.com.au/user-guide/">http://www.cleverfinger.com.au/user-guide/</a></string>

Textview 의 기본 속성을 사용하여 수행 할 수도 있습니다.

android:autoLink="email"

Note :- Html.fromHtml is deprecated in Android N

You need to do check and support Android N and higher versions of Android

                  //Set clickable true
                 tagHeading.setClickable(true);

                  //Handlle click event
                  tagHeading.setMovementMethod(LinkMovementMethod.getInstance());

                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
                    tagHeading.setText(Html.fromHtml("<a href='https://github.com/hiteshsahu'>https://github.com/hiteshsahu</a>", Html.FROM_HTML_MODE_LEGACY));
                } else {
                    tagHeading.setText(Html.fromHtml("<a href='https://github.com/hiteshsahu'>https://github.com/hiteshsahu</a>"));
                }

Alternatively

You can don't want to id programmatically add autoLink flag on TextView.

android:autoLink="web"

android:linksClickable="true"

This way You don't need to add <a href='somelink'> tags.

Which is a disadvantage, if you want to add hyperlink on a text you can't do it this way. eg you can't do something like this:- [hiteshsahu][1]

           <TextView
                android:id="@+id/tag_info"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/tag_ll"
                android:layout_gravity="left"
                android:layout_margin="10dp"
                android:autoLink="web"
                android:linksClickable="true"
                android:text="https://github.com/hiteshsahu"
                android:textColor="@color/secondary_text" />

The result from both approach:-

https://github.com/hiteshsahu


For Latest version of SDK fromHtml is deprecated Use below line

String yourtext = "<a style='text-decoration:underline' href='http://www.sample.com'> Sample Website </a>";
    if (Build.VERSION.SDK_INT >= 24) {
        textView.setText(Html.fromHtml(yourtext, Html.FROM_HTML_MODE_LEGACY));
    } else {
        textView.setText(Html.fromHtml(yourtext));
    }

참고URL : https://stackoverflow.com/questions/9290651/make-a-hyperlink-textview-in-android

반응형