ওভারভিউ
এই উদাহরণটি দেখায় কিভাবে কোটলিন ব্যবহার করে একটি অ্যান্ড্রয়েড অ্যাপ থেকে ফেসবুক পেজ খোলা যায়। এতে ফেসবুক অ্যাপ ইনস্টল থাকলে নেটিভ অ্যাপে, না থাকলে ব্রাউজারে পেজ খুলবে।
পূর্বশর্ত
- অ্যান্ড্রয়েড স্টুডিও (কোটলিন)
- ইন্টারনেট পারমিশন
ধাপ ১: প্রজেক্ট তৈরি করুন
File → New Project → Empty Activity → Language: Kotlin।
ধাপ ২: লেআউট (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/rlMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp"
android:onClick="fbClick"
android:text="Open Facebook" />
</LinearLayout>
ধাপ ৩: MainActivity.kt
package com.example.facebookopen
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun fbClick(view: View) {
startActivity(getOpenFacebookIntent())
}
private fun getOpenFacebookIntent(): Intent? {
return try {
// ফেসবুক অ্যাপ ইনস্টল আছে কিনা চেক করুন
packageManager.getPackageInfo("com.facebook.katana", 0)
// নেটিভ ফেসবুক অ্যাপে পেজ খোলা (fb:// URI স্কিম)
Intent(Intent.ACTION_VIEW, Uri.parse("fb://page/YOUR_PAGE_ID"))
} catch (e: Exception) {
// ফেসবুক অ্যাপ নেই → ব্রাউজারে খোলুন
Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/YOUR_PAGE_USERNAME"))
}
}
}
গুরুত্বপূর্ণ: YOUR_PAGE_ID এবং YOUR_PAGE_USERNAME আপনার ফেসবুক পেজের আসল ID এবং Username দিয়ে প্রতিস্থাপন করুন। পেজ ID পেতে: https://findmyfbid.com/ ব্যবহার করুন।
ধাপ ৪: AndroidManifest.xml (ইন্টারনেট পারমিশন সহ)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.facebookopen">
<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>
চলান ও পরীক্ষা
- অ্যাপ চালান এবং "Open Facebook" বাটনে ট্যাপ করুন।
- ফেসবুক অ্যাপ ইনস্টল থাকলে পেজ নেটিভ অ্যাপে খুলবে।
- নাই থাকলে ব্রাউজারে (Chrome/ব্রাউজার) পেজ লোড হবে।
উন্নত টিপস
- প্রোফাইল/গ্রুপ:
fb://profile/IDবাfb://group/IDব্যবহার করুন। - শেয়ার ইন্টেন্ট: শুধু পেজ খোলা নয়, কন্টেন্ট শেয়ার করতে
ShareCompatবাACTION_SENDব্যবহার করুন। - Package Visibility (Android 11+): ম্যানিফেস্টে
<queries>যোগ করুন:<queries> <package android:name="com.facebook.katana" /> <package android:name="com.facebook.lite" /> </queries> - ত্রুটি হ্যান্ডলিং: নেটওয়ার্ক অনুপস্থিতি বা অবৈধ URL-এর জন্য try-catch ব্লকে Toast দেখান।