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

কোটলিন ব্যবহার করে অ্যান্ড্রয়েড TextView-এ লাইন ব্রেক যোগ করা

ওভারভিউ

এই টিউটোরিয়ালটি দেখায় কিভাবে অ্যান্ড্রয়েড TextView-এ লাইন ব্রেক (Line Break) যোগ করা যায়। মূলত দুই পদ্ধতি: XML স্ট্রিং রিসোর্সে \n বা <br> ব্যবহার করা, এবং প্রোগ্রাম্যাটিক্যালি টেক্সট সেট করা।

পূর্বশর্ত

  • অ্যান্ড্রয়েড স্টুডিও (কোটলিন)
  • String Resource ও TextView বেসিক

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

File → New Project → Empty Activity → Language: Kotlin।

ধাপ ২: লেআউট (res/layout/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"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:text="Tutorials Point"
        android:textAlignment="center"
        android:textColor="@android:color/holo_green_dark"
        android:textSize="32sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="@string/questions"
        android:textSize="24sp" />

</RelativeLayout>

ধাপ ৩: স্ট্রিং রিসোর্স (res/values/strings.xml)

<resources>
    <string name="app_name">LineBreakDemo</string>
    
    <string name="questions">
1. What is your favourite food?\n
2. Which is your favourite sport?\n
3. What\'s your bucket list?
    </string>
</resources>

নোট: XML-এ \n ব্যবহার করুন। <br> HTML ট্যাগ TextView-এ ডিফল্টভাবে রেন্ডার হয় না (HtmlCompat.fromHtml() দরকার)।

ধাপ ৪: MainActivity.kt

package com.example.linebreak

import android.graphics.Color
import android.os.Bundle
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: TextView = findViewById(R.id.textView)
        textView.setTextColor(Color.RED)
    }
}

ধাপ ৫: AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.example.linebreak">

    <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>

চলান ও ফলাফল

অ্যাপ চালালে TextView-এ তিনটি লাইনে প্রশ্নগুলো দেখাবে:

1. What is your favourite food?
2. Which is your favourite sport?
3. What's your bucket list?

বিভিন্ন পদ্ধতিসমূহ

১. XML-এ \n (সর্বোত্তম/সरल)

<string name="multi_line">Line 1\nLine 2\nLine 3</string>

২. প্রোগ্রাম্যাটিক্যালি (Kotlin)

textView.text = "Line 1\nLine 2\nLine 3"
// অথবা
textView.text = buildString {
    append("Line 1")
    append("\n")
    append("Line 2")
}

৩. HTML <br> ব্যবহার (HtmlCompat দরকার)

import androidx.core.text.HtmlCompat

val htmlText = "Line 1
Line 2
Line 3" textView.text = HtmlCompat.fromHtml(htmlText, HtmlCompat.FROM_HTML_MODE_LEGACY)

৪. String Resource-এ <br> (কাজ নাও করতে পারে)

Android XML স্ট্রিং রিসোর্সে <br> সরাসরি সাপোর্ট করে না। এসকেপ করতে হয়: &lt;br&gt; কিন্তু TextView সেটি রেন্ডার করে না।

সেরা অনুশীলন

  • স্ট্যাটিক টেক্সট: strings.xml-এ \n ব্যবহার করুন (লোকালাইজেশন সহজ)।
  • ডাইনামিক টেক্সট: Kotlin-এ \n বা buildString ব্যবহার করুন।
  • রিচ টেক্সট (বোল্ড, কালার, লিংক): HtmlCompat.fromHtml() বা SpannableString ব্যবহার করুন।
  • XML-এ android:maxLines: লাইন সংখ্যা সীমাবদ্ধ করতে।
  1. অ্যান্ড্রয়েডে ডাইনামিক্যালি LinearLayout-এ TextView যোগ করা

  2. এন্ড্রয়েডে টেক্সটে শ্যাডো যোগ করার পদ্ধতি

  3. অ্যান্ড্রয়েড TextView-এ লাইন ব্রেক যোগ করার পদ্ধতি

  4. অ্যান্ড্রয়েড এমুলেটরের IP ঠিকানা কোটলিন ব্যবহার করে পাওয়ার উপায়