개요
이 글에서는 Kotlin을 사용해 안드로이드 TextView에서 특정 텍스트 부분만 굵게(bold) 표시하는 방법을 단계별로 소개합니다. 전체 문자열이 아니라 원하는 구간에만 스타일을 적용하려면 SpannableString과 StyleSpan을 활용하면 됩니다.
1단계 — 새 프로젝트 생성
Android Studio에서 File → New Project로 이동한 후, 새 프로젝트 생성에 필요한 모든 항목을 입력해 프로젝트를 만듭니다.
2단계 — 레이아웃 작성 (res/layout/activity_main.xml)
레이아웃 파일에 다음 코드를 추가합니다. 화면 상단에는 배경색이 있는 제목용 TextView가, 화면 중앙에는 본문 텍스트가 표시될 TextView가 배치됩니다.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:background="#008080"
android:padding="5dp"
android:text="TutorialsPoint"
android:textColor="#fff"
android:textSize="24sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:textAlignment="center"
android:textColor="@android:color/holo_green_dark"
android:textSize="36sp" />
</RelativeLayout>
3단계 — MainActivity.kt 작성
src/MainActivity.kt에 다음 코드를 추가합니다.
import android.graphics.Typeface
import android.os.Bundle
import android.text.SpannableString
import android.text.Spanned
import android.text.style.StyleSpan
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
val textView = findViewById<TextView>(R.id.textView)
val text = "My favourite food is Biriyani"
val spannableString = SpannableString(text)
val boldSpan = StyleSpan(Typeface.BOLD)
spannableString.setSpan(boldSpan, 21, 29, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
textView!!.text = spannableString
}
}
코드 설명: SpannableString은 문자열의 일부 구간에만 서식을 적용할 수 있게 해주는 클래스입니다. 위 예제에서는 문장 'My favourite food is Biriyani'에서 인덱스 21부터 29까지, 즉 Biriyani라는 단어에만 StyleSpan(Typeface.BOLD)을 적용해 해당 부분만 굵게 표시됩니다. 마지막 인자인 SPAN_EXCLUSIVE_EXCLUSIVE는 이후 삽입되는 텍스트로 스타일 범위가 확장되지 않도록 지정하는 플래그입니다.
4단계 — AndroidManifest.xml 설정
매니페스트 파일에 다음 코드를 추가합니다.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.q11">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
앱 실행 및 결과 확인
실제 안드로이드 기기가 컴퓨터에 연결되어 있다고 가정하고 앱을 실행해 보겠습니다. Android Studio에서 프로젝트의 액티비티 파일 중 하나를 연 뒤, 툴바의 Run 아이콘을 클릭합니다. 기기 목록에서 본인의 모바일 기기를 선택하면, 기기 화면에 'Biriyani' 부분만 굵게 강조된 결과를 확인할 수 있습니다.
