কম্পিউটার টিউটোরিয়াল

Kotlin ব্যবহার করে Android-এ পেজিনেশন টেক্সট তৈরি করা

ওভারভিউ

এই উদাহরণটি দেখাবে কিভাবে বড় টেক্সট কন্টেন্টকে পেজে ভাগ করে (pagination) TextView-এ দেখানো যায়, এবং "Back"/"Forward" বাটন দিয়ে পেজ নেভিগেশন করা যায়।

প্রয়োজনীয় ধাপসমূহ

ধাপ ১: নতুন প্রজেক্ট তৈরি

ধাপ ২: activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal">
        
        <Button
            android:id="@+id/buttonBack"
            style="?android:attr/buttonBarButtonStyle"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:text="◀ Back" />
        
        <Button
            android:id="@+id/buttonForward"
            style="?android:attr/buttonBarButtonStyle"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:text="Forward ▶" />
    </LinearLayout>
    
    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/buttonBack" />
</RelativeLayout>

ধাপ ৩: Pagination.kt (কাস্টম পেজিনেশন লজিক)

import android.graphics.Paint
import android.text.Layout
import android.text.TextPaint

class Pagination(
    private val text: CharSequence,
    private val width: Int,
    private val height: Int,
    private val paint: TextPaint,
    private val lineSpacingMultiplier: Float,
    private val lineSpacingExtra: Float,
    private val includeFontPadding: Boolean
) {
    private val pages = mutableListOf<CharSequence>()
    private val layout = Layout.Builder.obtain(text, 0, text.length, paint, width)
        .setLineSpacing(lineSpacingExtra, lineSpacingMultiplier)
        .setIncludePad(includeFontPadding)
        .setBreakStrategy(Layout.BREAK_STRATEGY_HIGH_QUALITY)
        .setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NORMAL)
        .build()
    
    init {
        var lineIndex = 0
        val linesPerPage = height / layout.getLineBounds(0, android.graphics.Rect()).height()
        
        while (lineIndex < layout.lineCount) {
            val endLine = min(lineIndex + linesPerPage, layout.lineCount)
            val start = layout.getLineStart(lineIndex)
            val end = layout.getLineEnd(endLine - 1)
            pages.add(text.subSequence(start, end))
            lineIndex = endLine
        }
    }
    
    operator fun get(index: Int): CharSequence? = pages.getOrNull(index)
    fun size() = pages.size
}

ধাপ ৪: MainActivity.kt

import android.graphics.Color
import android.graphics.Typeface
import android.os.Bundle
import android.text.Html
import android.text.Spannable
import android.text.SpannableString
import android.text.TextUtils
import android.text.style.ForegroundColorSpan
import android.text.style.RelativeSizeSpan
import android.text.style.StyleSpan
import android.view.ViewTreeObserver
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    private lateinit var buttonBack: Button
    private lateinit var buttonForward: Button
    private lateinit var textView: TextView
    private var pagination: Pagination? = null
    private lateinit var charSequence: CharSequence
    private var currentIndex = 0
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "Pagination Demo"
        
        buttonBack = findViewById(R.id.buttonBack)
        buttonForward = findViewById(R.id.buttonForward)
        textView = findViewById(R.id.tv)
        
        // স্ট্রিং রিসোর্স থেকে বা হার্ডকোডেড
        val longText = "আপনার বড় টেক্সট কন্টেন্ট এখানে... ".repeat(50)
        val spanString = SpannableString(longText)
        
        // স্টাইলিং উদাহরণ
        spanString.setSpan(ForegroundColorSpan(Color.BLUE), 0, 24, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        spanString.setSpan(RelativeSizeSpan(2f), 0, 24, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        spanString.setSpan(StyleSpan(Typeface.BOLD), 0, 24, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        
        charSequence = spanString
        
        // লেআউট রেডি হলে পেজিনেশন শুরু
        textView.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
            override fun onGlobalLayout() {
                textView.viewTreeObserver.removeOnGlobalLayoutListener(this)
                pagination = Pagination(
                    charSequence, textView.width, textView.height,
                    textView.paint, textView.lineSpacingMultiplier,
                    textView.lineSpacingExtra, textView.includeFontPadding
                )
                update()
            }
        })
        
        buttonBack.setOnClickListener {
            currentIndex = if (currentIndex > 0) currentIndex - 1 else 0
            update()
        }
        
        buttonForward.setOnClickListener {
            currentIndex = if (currentIndex < pagination!!.size() - 1) currentIndex + 1 else pagination!!.size() - 1
            update()
        }
    }
    
    private fun update() {
        pagination?.get(currentIndex)?.let { textView.text = it }
    }
}

ধাপ ৫: strings.xml (অপশনাল)

<resources>
    <string name="long_string">
        আপনার খুব বড় টেক্সট কন্টেন্ট এখানে লিখুন...
    </string>
</resources>

ধাপ ৬: AndroidManifest.xml

স্ট্যান্ডার্ড কনফিগারেশন।

আধুনিক বিকল্প

  • Jetpack Compose: Text(text, maxLines, overflow) + rememberPagerState() (Accompanist Pager)
  • ViewPager2 + Fragment: প্রতিটি পেজের জন্য আলাদা ফ্র্যাগমেন্ট
  • RecyclerView + Paging 3: ডেটাবেজ/নেটওয়ার্ক থেকে লোডিং

দ্রষ্টব্য

উপরোক্ত Pagination ক্লাসটি একটি বেসিক ইমপ্লিমেন্টেশন। প্রোডাকশন-রেডি করতে হলে হাইফিনেশন, RTL সাপোর্ট, ইমেজ স্প্যান হ্যান্ডলিং, এবং পারফরম্যান্স অপ্টিমাইজেশন যোগ করতে হবে।

  1. অ্যান্ড্রয়েডে পেজিনেটিং টেক্সট তৈরির পদ্ধতি

  2. Kotlin ব্যবহার করে Android TextView-এ একাধিক স্টাইল প্রয়োগ করা

  3. Kotlin ব্যবহার করে Android সার্ভিস থেকে নোটিফিকেশন পাঠানো

  4. অ্যান্ড্রয়েডে কোটলিন ব্যবহার করে Swipe Refresh Layout তৈরি করার উপায়