পরিচয়
উদাহরণের আগে জানা দরকার Linkify কী। Linkify HTML-এ হাইপারলিংকের মতোই। এর মাধ্যমে কন্টেন্ট ব্রাউজ করা যায়। এখানে Android-এ TextView-এ Linkify ব্যবহার করার সহজ সমাধান দেওয়া হয়েছে।
ধাপ ১: নতুন প্রজেক্ট তৈরি করুন
Android Studio-তে যান: File → New Project এবং প্রয়োজনীয় বিবরণ পূরণ করে একটি নতুন প্রজেক্ট তৈরি করুন।
ধাপ ২: লেয়াউট XML কনফিগার করুন
res/layout/activity_main.xml ফাইলে নিচের কোডটি যোগ করুন:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Result Data"
android:textSize="20sp"
android:padding="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
উপরের XML-এ একটি TextView রয়েছে যাতে টেক্সট এবং ওয়েব URL লিংক থাকে।
ধাপ ৩: MainActivity.java-তে লজিক লিখুন
src/MainActivity.java ফাইলে নিচের কোডটি যোগ করুন:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.util.Linkify;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.result);
textView.setText("TutorialsPoint.com originated from the idea that there exists a class of readers who respond better to online content and prefer to learn new skills at their ow...");
Linkify.addLinks(textView, Linkify.WEB_URLS);
}
}
উপরের কোডে TextView-এ টেক্সট সেট করা হয়েছে। টেক্সটে tutorialspoint.com নামে একটি URL রয়েছে। Android-এ Linkify কল করতে Linkify.addLinks() মেথড কল করতে হয়, যেখানে TextView এবং LinkifyMask পাস করতে হয়।
বিভিন্ন ধরনের LinkifyMask
- Linkify.WEB_URLS: URL-কে ওয়েব URL হিসেবে লিংকিফাই করে। ক্লিক করলে ডিফল্ট ওয়েব ব্রাউজারে পাঠায়।
- Linkify.EMAIL_ADDRESSES: ইমেইল আইডিকে লিংকিফাই করে। ক্লিক করলে ডিফল্ট ইমেইল ক্লায়েন্ট খুলে।
- Linkify.PHONE_NUMBERS: ফোন নম্বরকে লিংকিফাই করে। ক্লিক করলে ডিফল্ট ডায়ালারে পাঠায়।
- Linkify.ALL: WEB_URLS, EMAIL_ADDRESSES এবং PHONE_NUMBERS সবই হ্যান্ডেল করে।
ধাপ ৪: Manifest.xml-এ ইন্টারনেট পারমিশন যোগ করুন
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.andy.myapplication">
<uses-permission android:name="android.permission.INTERNET" />
<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 ডিভাইস কম্পিউটারের সাথে সংযুক্ত করুন। Android Studio-তে প্রজেক্টের একটি Activity ফাইল খুলুন এবং টুলবারের Run আইকনে ক্লিক করুন। আপনার মোবাইল ডিভাইস নির্বাচন করুন। ডিফল্ট স্ক্রিন প্রদর্শিত হবে:

উপরের আউটপুটে লিংকিফাই টেক্সটে ক্লিক করলে ওয়েব ব্রাউজারে ওয়েবসাইট দেখাবে:

লিংকে ক্লিক করলে ডিফল্ট ওয়েব ব্রাউজারে ওয়েবসাইট খুলবে:
