Android에서 TextView에 HTML링크를 넣는 방법 2가지.


1) 레이아웃에 직접 써주는 방법


- TextView의 autoLink 속성을 사용하여 URL을 넣어준다.

1
2
3
4
5
6
7
8
9
10
11
<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:autoLink="web"
        android:text="레이아웃에서 설정한 URL https://www.google.com"/>
 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:autoLink="web"
        android:text="레이아웃에서 설정한 URL https://www.naver.com"/>
cs


2) 클래스에서 메소드를 이용하여 넣는 방법.


- setAutoLinkMask 메소드를 이용하여 URL을 넣어준다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        TextView textView1 = (TextView) findViewById(R.id.textView1);
        TextView textView2 = (TextView) findViewById(R.id.textView2);
 
        textView1.setAutoLinkMask(Linkify.WEB_URLS);
        textView2.setAutoLinkMask(Linkify.WEB_URLS);
 
        textView1.setText("프로그램에서 설정한 URL https://www.google.com");
        textView2.setText("프로그램에서 설정한 URL https://www.naver.com");
    }
cs


'Android' 카테고리의 다른 글

[Android] ProgressBar Customize  (0) 2019.03.04
[Android] ProgressBar 표시하기  (0) 2019.03.02
[Android] gravity 속성  (0) 2019.03.01
[Android] style 속성 사용  (0) 2019.02.28
[Android] TextView에 긴 문자열 생략해서 표시하기  (0) 2019.02.27

+ Recent posts