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

কোটলিন ব্যবহার করে অ্যান্ড্রয়েডে TextView-এ ক্লিকযোগ্য লিংক কীভাবে তৈরি করবেন?

এই উদাহরণটি প্রদর্শন করে কীভাবে অ্যান্ড্রয়েডে কোটলিন ব্যবহার করে TextView-এ ক্লিকযোগ্য লিংক তৈরি করা যায়।

পদক্ষেপ ১: নতুন প্রজেক্ট তৈরি করুন

Android Studio-তে যান File → New Project এবং প্রজেক্ট তৈরি করার জন্য প্রয়োজনীয় সব বিবরণ পূরণ করুন।

পদক্ষেপ ২: 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/textViewLink" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_centerInParent="true" 
        android:gravity="center" 
        android:text="@string/messageWithLink" 
        android:textSize="24sp" 
        android:textStyle="bold" /> 
</RelativeLayout>

পদক্ষেপ ৩: res/values/strings.xml-এ কোড যোগ করুন

<resources> 
    <string name="app_name">Your App Name</string> 
    <string name="messageWithLink"><a href="https://www.google.com/">Tap here to open Google</a></string> 
</resources>

পদক্ষেপ ৪: src/MainActivity.kt-এ কোড যোগ করুন

import android.os.Bundle
import android.text.method.LinkMovementMethod
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.textViewLink)
        textView.movementMethod = LinkMovementMethod.getInstance()
    }
}
প্রোগ্রামেটিক্যালি লিংক যোগ করা:
import android.text.SpannableString
import android.text.style.ClickableSpan
import android.text.method.LinkMovementMethod
import android.view.View

val text = "Visit our website for more info"
val spannable = SpannableString(text)
val clickableSpan = object : ClickableSpan() {
    override fun onClick(widget: View) {
        // লিংক ক্লিক হলে কি হবে
    }
}
spannable.setSpan(clickableSpan, 6, 14, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
textView.text = spannable
textView.movementMethod = LinkMovementMethod.getInstance()

পদক্ষেপ ৫: 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-তে আপনার প্রজেক্টের এক্টিভিটি ফাইল খুলুন এবং টুলবারের রান আইকনে ক্লিক করুন। আপনার মোবাইল ডিভাইস সিলেক্ট করুন এবং ডিভাইসে ডিফল্ট স্ক্রিন দেখুন।

  1. অ্যান্ড্রয়েডে থ্রেড (Thread) তৈরি করার পদ্ধতি

  2. অ্যান্ড্রয়েড TextView-এ ক্লিকযোগ্য লিংক তৈরি করার পদ্ধতি

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

  4. Kotlin ব্যবহার করে Android-এ রানটাইমে TextView স্টাইল পরিবর্তন করা