ওভারভিউ
এই টিউটোরিয়ালে দেখানো হয়েছে কিভাবে setTextAppearance() এবং রিসোর্স ব্যবহার করে রানটাইমে TextView-এর স্টাইল (রঙ, আকার, বোল্ড, ইট্যালিক ইত্যাদি) ডাইনামিক্যালি পরিবর্তন করা যায়।
প্রয়োজনীয় ধাপসমূহ
ধাপ ১: নতুন প্রজেক্ট তৈরি
ধাপ ২: res/values/styles.xml
<resources>
<!-- বোল্ড এবং ইট্যালিক স্টাইল -->
<style name="BoldItalicStyle">
<item name="android:textStyle">bold|italic</item>
<item name="android:textColor">#FFFFFF</item>
<item name="android:textSize">24sp</item>
</style>
<!-- নরমাল স্টাইল -->
<style name="NormalStyle">
<item name="android:textStyle">normal</item>
<item name="android:textColor">#C0C0C0</item>
<item name="android:textSize">18sp</item>
</style>
<!-- হাইলাইট স্টাইল -->
<style name="HighlightStyle">
<item name="android:textStyle">bold</item>
<item name="android:textColor">@color/white</item>
<item name="android:textSize">20sp</item>
<item name="android:background">@color/highlight_bg</item>
</style>
</resources>
ধাপ ৩: res/values/colors.xml
<resources>
<color name="highlightedTextViewColor">#4CAF50</color>
<color name="normalTextViewColor">#F44336</color>
<color name="highlight_bg">#FFEB3B</color>
<color name="white">#FFFFFF</color>
</resources>
ধাপ ৪: activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity">
<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="TextView Style Change Demo"
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:text="Tap to change style!"
android:textColor="@android:color/background_dark"
android:textSize="36sp"
android:textStyle="bold"
android:padding="24dp"
android:background="@drawable/text_bg" />
</RelativeLayout>
ধাপ ৫: res/drawable/text_bg.xml (অপশনাল)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android">
<solid android:color="@android:color/transparent" />
<corners android:radius="12dp" />
<stroke android:width="2dp" android:color="@color/colorPrimary" />
</shape>
ধাপ ৬: MainActivity.kt
import android.os.Build
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
class MainActivity : AppCompatActivity() {
private lateinit var textView: TextView
private var isStyled = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "Runtime Style Change"
textView = findViewById(R.id.textView)
textView.setOnClickListener {
toggleStyle()
}
}
private fun toggleStyle() {
isStyled = !isStyled
if (isStyled) {
// স্টাইল ১: বোল্ড+ইট্যালিক, সাদা রঙ, গ্রিন ব্যাকগ্রাউন্ড
setTextAppearanceCompat(R.style.BoldItalicStyle)
textView.setBackgroundResource(R.color.highlightedTextViewColor)
textView.text = "Styled: Bold + Italic!"
} else {
// স্টাইল ২: নরমাল, গ্রে রঙ, রেড ব্যাকগ্রাউন্ড
setTextAppearanceCompat(R.style.NormalStyle)
textView.setBackgroundResource(R.color.normalTextViewColor)
textView.text = "Normal Style"
}
}
// API লেভেল অনুযায়ী কম্প্যাট মেথড
private fun setTextAppearanceCompat(@StyleRes styleResId: Int) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
textView.setTextAppearance(styleResId)
} else {
// ডিপ্রিকেটেড কিন্তু পুরানো ডিভাইসের জন্য
@Suppress("DEPRECATION")
textView.setTextAppearance(this, styleResId)
}
}
}
প্রোগ্রামেটিক্যালি স্টাইল পরিবর্তন (XML ছাড়া)
styles.xml ছাড়াও সরাসরি কোডে করা যায়:
private fun applyCustomStyle() {
with(textView) {
// টেক্সট স্টাইল
setTypeface(Typeface.DEFAULT_BOLD) // বা Typeface.create("sans-serif-medium", Typeface.NORMAL)
setTextColor(Color.WHITE)
setTextSize(TypedValue.COMPLEX_UNIT_SP, 22f)
// ব্যাকগ্রাউন্ড
setBackgroundColor(ContextCompat.getColor(this@MainActivity, R.color.highlightedTextViewColor))
// বা গ্রেডিয়েন্ট ড্রয়েবল
// setBackgroundResource(R.drawable.gradient_bg)
}
}
// স্প্যান ব্যবহার করে আংশিক স্টাইল
private fun applyPartialStyle() {
val spannable = SpannableString("Partial styling: Bold and Red")
spannable.setSpan(StyleSpan(Typeface.BOLD), 18, 22, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
spannable.setSpan(ForegroundColorSpan(Color.RED), 27, 30, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
textView.text = spannable
}
TextView এর প্রধান স্টাইল অ্যাট্রিবিউটস
| XML অ্যাট্রিবিউট | প্রোগ্রামেটিক মেথড | বিবরণ |
|---|---|---|
android:textStyle | setTypeface() | normal, bold, italic, bold|italic |
android:textColor | setTextColor() | Color int, ColorStateList |
android:textSize | setTextSize(unit, size) | sp 단위 권장 |
android:typeface | setTypeface(Typeface) | normal, sans, serif, monospace |
android:fontFamily | setTypeface(ResourcesCompat.getFont()) | কাস্টম ফন্ট (res/font/) |
android:letterSpacing | setLetterSpacing() | অক্ষর અંતর (API 21+) |
android:lineSpacingExtra | setLineSpacing(add, mult) | লাইন স্পেসিং |
android:background | setBackgroundResource() | পটভূমি ড্রয়েবল/রঙ |
Jetpack Compose-এ স্টাইল পরিবর্তন
var isStyled by remember { mutableStateOf(false) }
Text(
text = if (isStyled) "Styled!" else "Normal",
fontSize = if (isStyled) 24.sp else 18.sp,
fontWeight = if (isStyled) FontWeight.Bold else FontWeight.Normal,
fontStyle = if (isStyled) FontStyle.Italic else FontStyle.Normal,
color = if (isStyled) Color.White else Color.Gray,
modifier = Modifier
.fillMaxWidth()
.padding(16.dp)
.background(if (isStyled) Color.Green else Color.Red)
.padding(16.dp)
.clickable { isStyled = !isStyled }
.wrapContentSize(Alignment.Center)
)