ধারণা ও Jetpack Compose-এর প্রয়োগ
স্ট্রিং রিসোর্স আপনার অ্যাপ্লিকেশনের জন্য ঐচ্ছিক টেক্সট স্টাইলিং ও ফর্ম্যাটিং সহ টেক্সট স্ট্রিং প্রদান করে। তিন ধরনের রিসোর্স আপনার অ্যাপ্লিকেশনকে স্ট্রিং প্রদান করতে পারে:
- স্ট্রিং
- XML রিসোর্স যা একটি স্ট্রিং প্রদান করে।
- স্ট্রিং অ্যারে
- XML রিসোর্স যা স্ট্রিংয়ের একটি অ্যারে প্রদান করে।
- পরিমাণ স্ট্রিং (বহুবচন)
- XML রিসোর্স যা বহুবচনের জন্য বিভিন্ন স্ট্রিং বহন করে।
সব স্ট্রিং কিছু স্টাইলিং মার্কআপ এবং ফর্ম্যাটিং আর্গুমেন্ট প্রয়োগ করতে সক্ষম। স্ট্রিং স্টাইলিং ও ফর্ম্যাটিং সম্পর্কে তথ্যের জন্য, ফর্ম্যাটিং ও স্টাইলিং সংক্রান্ত বিভাগ দেখুন।
স্ট্রিং
একটি স্ট্রিং যা অ্যাপ্লিকেশন বা অন্যান্য রিসোর্স ফাইল (যেমন XML লেআউট) থেকে রেফারেন্স করা যেতে পারে।
- ফাইলের লোকেশন:
res/values/filename.xml
ফাইলের নাম ইচ্ছামতো দেওয়া হয়েছে।<string>এলিমেন্টেরnameরিসোর্স আইডি হিসেবে ব্যবহার করা হয়।- কম্পাইল করা রিসোর্স ডেটাটাইপ:
- রিসোর্স পয়েন্টার থেকে
String। - রিসোর্স রেফারেন্স:
-
Java-তে:
R.string.string_name
XML-এ:@string/string_name - সিনট্যাক্স:
-
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="string_name" >text_string</string> </resources>
- এলিমেন্ট:
- উদাহরণ:
- XML ফাইল
res/values/strings.xml-এ সেভ করা হয়েছে:<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello!</string> </resources>
এই লেআউট XML একটি ভিউতে স্ট্রিং প্রয়োগ করে:
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" />
এই অ্যাপ্লিকেশন কোড একটি স্ট্রিং ফিরিয়ে আনে:
স্ট্রিং ফিরিয়ে আনতে আপনি
getString(int)অথবাgetText(int)ব্যবহার করতে পারেন।getText(int)স্ট্রিংয়ে প্রয়োগ করা যেকোনও রিচ টেক্সট স্টাইলিং রেখে দেয়।
স্ট্রিং অ্যারে
স্ট্রিংয়ের একটি অ্যারে যা অ্যাপ্লিকেশন থেকে রেফারেন্স করা যেতে পারে।
- ফাইলের লোকেশন:
res/values/filename.xml
ফাইলের নাম ইচ্ছামতো দেওয়া হয়েছে।<string-array>এলিমেন্টেরnameরিসোর্স আইডি হিসেবে ব্যবহার করা হয়।- কম্পাইল করা রিসোর্স ডেটাটাইপ:
- রিসোর্স পয়েন্টার
String-এর অ্যারেতে পয়েন্ট করে। - রিসোর্স রেফারেন্স:
-
Java-তে:
R.array.string_array_name
XML-এ:@[package:]array/string_array_name - সিনট্যাক্স:
-
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="string_array_name"> <item >text_string</item> </string-array> </resources>
- এলিমেন্ট:
- উদাহরণ:
- XML ফাইল
res/values/strings.xml-এ সেভ করা হয়েছে: এই অ্যাপ্লিকেশন কোড একটি স্ট্রিং অ্যারে ফিরিয়ে আনে:<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="planets_array"> <item>Mercury</item> <item>Venus</item> <item>Earth</item> <item>Mars</item> </string-array> </resources>
Kotlin
val array: Array<String> =
resources.getStringArray(R.array.planets_array)জাভা
Resources res =
getResources(); String[] planets = res.getStringArray(R.array.planets_array);
পরিমাণ স্ট্রিং (বহুবচন)
পরিমাণের সাথে ব্যাকরণগত মিলের জন্য বিভিন্ন ভাষায় বিভিন্ন নিয়ম
রয়েছে। যেমন, ইংরেজিতে ১ সংখ্যাটি একটি বিশেষ কেস। আমরা লিখি
"১টি বই", কিন্তু অন্য যেকোনও পরিমাণের জন্য আমরা লিখব "nটি বই"। একবচন ও বহুবচনের মধ্যে এই পার্থক্য
খুবই সাধারণ, তবে অন্যান্য ভাষায় আরও সূক্ষ্ম
পার্থক্য করা হয়। Android-এ কাজ করে এমন সম্পূর্ণ সেট হল zero, one, two, few,
many এবং other।
কোনও নির্দিষ্ট ভাষা ও পরিমাণের জন্য কোন কেস ব্যবহার করা হবে তা নির্ধারণ করার নিয়ম
খুব জটিল হতে পারে, তাই Android আপনাকে getQuantityString()-এর মতো পদ্ধতি প্রদান করে
যাতে আপনি উপযুক্ত রিসোর্স বেছে নিতে পারেন।
API 24+ ভার্সনে আপনি এর পরিবর্তে অনেক বেশি শক্তিশালী ICU MessageFormat
ক্লাস ব্যবহার করতে পারবেন।
- ফাইলের লোকেশন:
res/values/filename.xml
ফাইলের নাম ইচ্ছামতো দেওয়া হয়েছে।<plurals>এলিমেন্টেরnameরিসোর্স আইডি হিসেবে ব্যবহার করা হয়।- রিসোর্স রেফারেন্স:
-
Java-তে:
R.plurals.plural_name - সিনট্যাক্স:
-
<?xml version="1.0" encoding="utf-8"?> <resources> <plurals name="plural_name"> <item quantity=["zero" | "one" | "two" | "few" | "many" | "other"] >text_string</item> </plurals> </resources>
- এলিমেন্ট:
- উদাহরণ:
- XML ফাইল
res/values/strings.xml-এ সেভ করা হয়েছে:<?xml version="1.0" encoding="utf-8"?> <resources> <plurals name="numberOfSongsAvailable"> <!-- As a developer, you should always supply "one" and "other" strings. Your translators will know which strings are actually needed for their language. Always include %d in "one" because translators will need to use %d for languages where "one" doesn't mean 1 (as explained above). --> <item quantity="one">%d song found.</item> <item quantity="other">%d songs found.</item> </plurals> </resources>
XML ফাইল
res/values-pl/strings.xml-এ সেভ করা হয়েছে:<?xml version="1.0" encoding="utf-8"?> <resources> <plurals name="numberOfSongsAvailable"> <item quantity="one">Znaleziono %d piosenkę.</item> <item quantity="few">Znaleziono %d piosenki.</item> <item quantity="other">Znaleziono %d piosenek.</item> </plurals> </resources>
ব্যবহার:
Kotlin
val count = getNumberOfSongsAvailable() val songsFound = resources.
getQuantityString(R.plurals.numberOfSongsAvailable, count, count)জাভা
int count = getNumberOfSongsAvailable(); Resources res =
getResources(); String songsFound = res.getQuantityString(R.plurals.numberOfSongsAvailable, count, count);getQuantityString()পদ্ধতি ব্যবহার করার সময়, আপনার স্ট্রিংয়ে স্ট্রিং ফর্ম্যাটিং থাকলে, আপনাকেcountদুবার পাস করতে হবে। যেমন, স্ট্রিং%d songs found-এর জন্য, প্রথমcountপ্যারামিটারটি উপযুক্ত বহুবচন স্ট্রিং বেছে নেয় এবং দ্বিতীয়countপ্যারামিটারটি%dপ্লেসহোল্ডারে যোগ করা হয়। আপনার বহুবচন স্ট্রিংয়ে স্ট্রিং ফর্ম্যাটিং অন্তর্ভুক্ত না থাকলে, আপনাকেgetQuantityString-এ তৃতীয় প্যারামিটার পাস করতে হবে না।
ফর্ম্যাট ও স্টাইল
আপনার স্ট্রিং রিসোর্স কীভাবে সঠিকভাবে ফর্ম্যাট এবং স্টাইল করবেন সে সম্পর্কে এখানে কয়েকটি গুরুত্বপূর্ণ বিষয় রয়েছে যা আপনার জানা উচিত।
স্ট্রিং ফর্ম্যাট করা
আপনার স্ট্রিং ফর্ম্যাট করতে হলে, স্ট্রিং রিসোর্সে ফর্ম্যাট আর্গুমেন্ট যোগ করে তা করতে পারেন, যেমনটি নিম্নলিখিত উদাহরণ রিসোর্সে দেখানো হয়েছে।
<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>
এই উদাহরণে, ফর্ম্যাট স্ট্রিংয়ে দুটি আর্গুমেন্ট আছে: %1$s হল একটি স্ট্রিং এবং
%2$d হল একটি দশমিক সংখ্যা। তারপর, getString(int, Object...) কল করে স্ট্রিং ফর্ম্যাট করুন। যেমন:
Kotlin
var text = getString(R.string.welcome_messages, username, mailCount)
জাভা
String text = getString(R.string.welcome_messages, username, mailCount);
HTML মার্কআপের মাধ্যমে স্টাইল করা
HTML মার্কআপের মাধ্যমে আপনি স্ট্রিংয়ে স্টাইলিং যোগ করতে পারবেন। যেমন:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="welcome">Welcome to <b>Android</b>!</string> </resources>
আপনি ফর্ম্যাটিং প্রয়োগ না করলে, setText(java.lang.CharSequence) কল করে সরাসরি TextView টেক্সট সেট করতে
পারবেন। তবে, কিছু ক্ষেত্রে, আপনি
স্টাইল করা টেক্সট রিসোর্স তৈরি করতে চাইতে পারেন যা ফর্ম্যাট স্ট্রিং হিসেবেও ব্যবহার করা হয়।
সাধারণত, এটি কাজ করে না কারণ format(String, Object...) এবং
getString(int, Object...) পদ্ধতি স্ট্রিং থেকে সব স্টাইল সংক্রান্ত তথ্য
মুছে দেয়। এর সমাধান হল, এস্কেপড এন্টিটি সহ HTML ট্যাগ লেখা, যা পরে
ফর্ম্যাটিং হয়ে যাওয়ার পরে fromHtml(String)-এর মাধ্যমে ফিরিয়ে আনা হয়। যেমন:
- আপনার স্টাইল করা টেক্সট রিসোর্স HTML-এস্কেপড স্ট্রিং হিসেবে স্টোর করুন:
<resources> <string name="welcome_messages">Hello, %1$s! You have <b>%2$d new messages</b>.</string> </resources>
এই ফর্ম্যাট করা স্ট্রিংয়ে, একটি
<b>এলিমেন্ট যোগ করা হয়েছে। লক্ষ্য করুন যে শুরুর ব্র্যাকেটটি HTML-এস্কেপ করা হয়েছে,<নোটেশন ব্যবহার করে। - তারপর, স্ট্রিংটি স্বাভাবিকভাবে ফর্ম্যাট করুন, তবে HTML টেক্সটকে স্টাইল করা টেক্সটে রূপান্তর করতে
fromHtml(String)-কে কল করুন:Kotlin
val text: String = getString(R.string.welcome_messages, username, mailCount) val styledText: Spanned = Html.fromHtml(text, FROM_HTML_MODE_LEGACY)
জাভা
String text = getString(R.string.welcome_messages, username, mailCount); Spanned styledText = Html.fromHtml(text, FROM_HTML_MODE_LEGACY);
যেহেতু fromHtml(String) পদ্ধতি সব HTML এন্টিটি ফর্ম্যাট করে, তাই htmlEncode(String) ব্যবহার করে ফর্ম্যাট করা টেক্সটের সাথে
আপনি যে স্ট্রিং ব্যবহার করেন তাতে সম্ভাব্য যেকোনও HTML অক্ষর এড়িয়ে যেতে ভুলবেন না। যেমন, আপনি যদি এমন কোনও স্ট্রিং ফর্ম্যাট করেন যাতে "<" বা "&" এর মতো অক্ষর থাকে, তাহলে ফর্ম্যাট করার আগে সেগুলিকে এড়িয়ে যেতে হবে, যাতে ফর্ম্যাট করা স্ট্রিংটি fromHtml(String)-এর মাধ্যমে পাস করার সময়, অক্ষরগুলি যেভাবে মূলত লেখা হয়েছিল সেভাবেই বেরিয়ে আসে। যেমন:
Kotlin
val escapedUsername: String = TextUtils.htmlEncode(username)
val text: String = getString(R.string.welcome_messages, escapedUsername, mailCount)
val styledText: Spanned = Html.fromHtml(text, FROM_HTML_MODE_LEGACY)জাভা
String escapedUsername = TextUtils.htmlEncode(username);
String text = getString(R.string.welcome_messages, escapedUsername, mailCount);
Spanned styledText = Html.fromHtml(text);spannables-এর সাহায্যে স্টাইলিং
Spannable হল একটি টেক্সট অবজেক্ট যা আপনি টাইপফেস প্রপার্টি যেমন রঙ এবং ফন্টের ওজন দিয়ে স্টাইল করতে পারেন। আপনি SpannableStringBuilder ব্যবহার করে
টেক্সট তৈরি করেন এবং তারপরে android.text.style
প্যাকেজে উল্লেখ করা স্টাইল টেক্সটে প্রয়োগ করেন।
স্প্যান করা যায় এমন টেক্সট তৈরি করার বেশিরভাগ কাজ সেট-আপ করতে আপনি নিম্নলিখিত সহায়ক পদ্ধতি ব্যবহার করতে পারেন:
Kotlin
/** * Returns a CharSequence that concatenates the specified array of CharSequence * objects and then applies a list of zero or more tags to the entire range. * * @param content an array of character sequences to apply a style to * @param tags the styled span objects to apply to the content * such as android.text.style.StyleSpan */ private fun apply(content: Array<out CharSequence>, vararg tags: Any): CharSequence { return SpannableStringBuilder().apply { openTags(tags) content.forEach { charSequence -> append(charSequence) } closeTags(tags) } } /** * Iterates over an array of tags and applies them to the beginning of the specified * Spannable object so that future text appended to the text will have the styling * applied to it. Do not call this method directly. */ private fun Spannable.openTags(tags: Array<out Any>) { tags.forEach { tag -> setSpan(tag, 0, 0, Spannable.SPAN_MARK_MARK) } } /** * "Closes" the specified tags on a Spannable by updating the spans to be * endpoint-exclusive so that future text appended to the end will not take * on the same styling. Do not call this method directly. */ private fun Spannable.closeTags(tags: Array<out Any>) { tags.forEach { tag -> if (length > 0) { setSpan(tag, 0, length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) } else { removeSpan(tag) } } }
জাভা
/** * Returns a CharSequence that concatenates the specified array of CharSequence * objects and then applies a list of zero or more tags to the entire range. * * @param content an array of character sequences to apply a style to * @param tags the styled span objects to apply to the content * such as android.text.style.StyleSpan * */ private static CharSequence applyStyles(CharSequence[] content, Object[] tags) { SpannableStringBuilder text = new SpannableStringBuilder(); openTags(text, tags); for (CharSequence item : content) { text.append(item); } closeTags(text, tags); return text; } /** * Iterates over an array of tags and applies them to the beginning of the specified * Spannable object so that future text appended to the text will have the styling * applied to it. Do not call this method directly. */ private static void openTags(Spannable text, Object[] tags) { for (Object tag : tags) { text.setSpan(tag, 0, 0, Spannable.SPAN_MARK_MARK); } } /** * "Closes" the specified tags on a Spannable by updating the spans to be * endpoint-exclusive so that future text appended to the end will not take * on the same styling. Do not call this method directly. */ private static void closeTags(Spannable text, Object[] tags) { int len = text.length(); for (Object tag : tags) { if (len > 0) { text.setSpan(tag, 0, len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } else { text.removeSpan(tag); } } }
নিম্নলিখিত bold, italic এবং color পদ্ধতিগুলি উপরের হেল্পার পদ্ধতিগুলিকে র্যাপ করে এবং android.text.style প্যাকেজে সংজ্ঞায়িত স্টাইল প্রয়োগ করার নির্দিষ্ট উদাহরণ প্রদর্শন করে। অন্য ধরনের টেক্সট স্টাইলিংয়ের জন্য আপনি একই ধরনের পদ্ধতি তৈরি করতে পারেন।
Kotlin
/** * Returns a CharSequence that applies boldface to the concatenation * of the specified CharSequence objects. */ fun bold(vararg content: CharSequence): CharSequence = apply(content, StyleSpan(Typeface.BOLD)) /** * Returns a CharSequence that applies italics to the concatenation * of the specified CharSequence objects. */ fun italic(vararg content: CharSequence): CharSequence = apply(content, StyleSpan(Typeface.ITALIC)) /** * Returns a CharSequence that applies a foreground color to the * concatenation of the specified CharSequence objects. */ fun color(color: Int, vararg content: CharSequence): CharSequence = apply(content, ForegroundColorSpan(color))
জাভা
/** * Returns a CharSequence that applies boldface to the concatenation * of the specified CharSequence objects. */ public static CharSequence bold(CharSequence... content) { return apply(content, new StyleSpan(Typeface.BOLD)); } /** * Returns a CharSequence that applies italics to the concatenation * of the specified CharSequence objects. */ public static CharSequence italic(CharSequence... content) { return apply(content, new StyleSpan(Typeface.ITALIC)); } /** * Returns a CharSequence that applies a foreground color to the * concatenation of the specified CharSequence objects. */ public static CharSequence color(int color, CharSequence... content) { return apply(content, new ForegroundColorSpan(color)); }
কোনও বাক্যাংশের মধ্যে থাকা স্বতন্ত্র শব্দে বিভিন্ন স্টাইল প্রয়োগ করতে কীভাবে এইসব পদ্ধতি একসাথে চেইন করতে হয় তার উদাহরণ এখানে দেওয়া হল:
Kotlin
// Create an italic "hello, " a red "world", // and bold the entire sequence. val text: CharSequence = bold(italic(getString(R.string.hello)), color(Color.RED, getString(R.string.world)))
জাভা
// Create an italic "hello, " a red "world", // and bold the entire sequence. CharSequence text = bold(italic(getString(R.string.hello)), color(Color.RED, getString(R.string.world)));
এছাড়াও, core-ktx Kotlin মডিউলে এক্সটেনশন ফাংশন থাকে যা স্প্যান নিয়ে কাজ করা
আরও সহজ করে তোলে। আরও জানতে আপনি GitHub-এ android.text
প্যাকেজ ডকুমেন্টেশন চেক করে দেখতে পারেন।
স্প্যান নিয়ে কাজ করার ব্যাপারে আরও জানতে, নিম্নলিখিত লিঙ্ক দেখুন:
অ্যানোটেশন সহ স্টাইলিং
আপনার strings.xml রিসোর্স ফাইলে <annotation> ট্যাগের সাথে Annotation
ক্লাস ব্যবহার করে আপনি জটিল বা কাস্টম স্টাইলিং প্রয়োগ করতে পারবেন। The
annotation tag allows you to mark parts of the string for custom styling by
defining custom key-value pairs in the XML that the framework then converts
into Annotation spans. আপনি তারপর এইসব অ্যানোটেশন ফিরিয়ে আনতে পারবেন এবং স্টাইলিং প্রয়োগ করতে
কী ও ভ্যালু ব্যবহার করতে পারবেন।
অ্যানোটেশন তৈরি করার সময়, প্রতিটি strings.xml ফাইলের স্ট্রিংয়ের সব অনুবাদে <annotation>
ট্যাগ যোগ করতে ভুলবেন না।

সব ভাষায় "টেক্সট" শব্দে কাস্টম টাইপফেস প্রয়োগ করা হচ্ছে
উদাহরণ - কাস্টম টাইপফেস যোগ করা
-
<annotation>ট্যাগ যোগ করুন এবং কী-ভ্যালু পেয়ার নির্ধারণ করুন। এই ক্ষেত্রে, কী হল font এবং ভ্যালু হল আমরা যে ধরনের ফন্ট ব্যবহার করতে চাই: title_emphasis// values/strings.xml <string name="title">Best practices for <annotation font="title_emphasis">text</annotation> on Android</string> // values-es/strings.xml <string name="title"><annotation font="title_emphasis">Texto</annotation> en Android: mejores prácticas</string>
-
স্ট্রিং রিসোর্স লোড করুন এবং font কী সহ অ্যানোটেশন খুঁজুন। তারপর একটি কাস্টম স্প্যান তৈরি করুন এবং আগে থেকে থাকা স্প্যানটি পরিবর্তন করুন।
Kotlin
// get the text as SpannedString so we can get the spans attached to the text val titleText = getText(R.string.title) as SpannedString // get all the annotation spans from the text val annotations = titleText.getSpans(0, titleText.length, Annotation::class.java) // create a copy of the title text as a SpannableString. // the constructor copies both the text and the spans. so we can add and remove spans val spannableString = SpannableString(titleText) // iterate through all the annotation spans for (annotation in annotations) { // look for the span with the key font if (annotation.key == "font") { val fontName = annotation.value // check the value associated to the annotation key if (fontName == "title_emphasis") { // create the typeface val typeface = getFontCompat(R.font.permanent_marker) // set the span at the same indices as the annotation spannableString.setSpan(CustomTypefaceSpan(typeface), titleText.getSpanStart(annotation), titleText.getSpanEnd(annotation), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) } } } // now, the spannableString contains both the annotation spans and the CustomTypefaceSpan styledText.text = spannableString
জাভা
// get the text as SpannedString so we can get the spans attached to the text SpannedString titleText = (SpannedString) getText(R.string.title); // get all the annotation spans from the text Annotation[] annotations = titleText.getSpans(0, titleText.length(), Annotation.class); // create a copy of the title text as a SpannableString. // the constructor copies both the text and the spans. so we can add and remove spans SpannableString spannableString = new SpannableString(titleText); // iterate through all the annotation spans for (Annotation annotation: annotations) { // look for the span with the key font if (annotation.getKey().equals("font")) { String fontName = annotation.getValue(); // check the value associated to the annotation key if (fontName.equals("title_emphasis")) { // create the typeface Typeface typeface = ResourcesCompat.getFont(this, R.font.roboto_mono); // set the span at the same indices as the annotation spannableString.setSpan(new CustomTypefaceSpan(typeface), titleText.getSpanStart(annotation), titleText.getSpanEnd(annotation), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } } } // now, the spannableString contains both the annotation spans and the CustomTypefaceSpan styledText.text = spannableString;
আপনি একই টেক্সট একাধিকবার ব্যবহার করলে, আপনার একবার SpannableString অবজেক্ট তৈরি করা উচিত এবং সম্ভাব্য পারফর্ম্যান্স ও মেমরি সংক্রান্ত সমস্যা এড়াতে প্রয়োজন অনুযায়ী এটি আবার ব্যবহার করা উচিত।
অ্যানোটেশন ব্যবহারের আরও উদাহরণের জন্য, Android-এ আন্তর্জাতিক টেক্সট স্টাইলিং দেখুন।
অ্যানোটেশন স্প্যান ও টেক্সট পার্সেলিং
যেহেতু Annotation স্প্যানও ParcelableSpans, তাই কী-ভ্যালু পেয়ার
পার্সেল ও আনপার্সেল করা হয়। যতক্ষণ পর্যন্ত পার্সেল প্রাপক জানেন যে কীভাবে
অ্যানোটেশন ব্যাখ্যা করতে হয়, ততক্ষণ পর্যন্ত আপনি পার্সেল করা টেক্সটে কাস্টম
স্টাইলিং প্রয়োগ করতে Annotation স্প্যান ব্যবহার করতে পারবেন।
ইনটেন্ট বান্ডেলে টেক্সট পাস করার সময় আপনার কাস্টম স্টাইলিং বজায় রাখতে, আপনাকে প্রথমে টেক্সটে Annotation স্প্যান যোগ করতে হবে। আপনি এটি XML রিসোর্সে <annotation> ট্যাগের মাধ্যমে করতে পারেন, যেমনটি উপরের উদাহরণে দেখানো হয়েছে, অথবা কোডে একটি নতুন Annotation তৈরি করে এবং এটিকে একটি স্প্যান হিসাবে সেট করে, যেমনটি নিচে দেখানো হয়েছে:
Kotlin
val spannableString = SpannableString("My spantastic text") val annotation = Annotation("font", "title_emphasis") spannableString.setSpan(annotation, 3, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) // start Activity with text with spans val intent = Intent(this, MainActivity::class.java) intent.putExtra(TEXT_EXTRA, spannableString) startActivity(intent)
জাভা
SpannableString spannableString = new SpannableString("My spantastic text"); Annotation annotation = new Annotation("font", "title_emphasis"); spannableString.setSpan(annotation, 3, 7, 33); // start Activity with text with spans Intent intent = new Intent(this, MainActivity.class); intent.putExtra(TEXT_EXTRA, spannableString); this.startActivity(intent);
Bundle থেকে SpannableString হিসেবে টেক্সট ফিরিয়ে আনুন এবং তারপর উপরে দেখানো উদাহরণের মতো
অ্যাটাচ করা অ্যানোটেশন পার্স করুন।
Kotlin
// read text with Spans val intentCharSequence = intent.getCharSequenceExtra(TEXT_EXTRA) as SpannableString
জাভা
// read text with Spans SpannableString intentCharSequence = (SpannableString)intent.getCharSequenceExtra(TEXT_EXTRA);
টেক্সট স্টাইলিং সম্পর্কে আরও তথ্যের জন্য, নিম্নলিখিত লিঙ্কগুলি দেখুন: