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

কোটলিন ব্যবহার করে অ্যান্ড্রয়েডে এক্টিভিটিগুলোর মধ্যে স্যুইচ কীভাবে করবেন?

এই উদাহরণটি প্রদর্শন করে কীভাবে অ্যান্ড্রয়েডে কোটলিন ব্যবহার করে বিভিন্ন এক্টিভিটির মধ্যে স্যুইচ করা যায়।

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

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="40dp" 
        android:textColor="@android:color/holo_purple" 
        android:text="MainActivity" 
        android:textSize="24sp" 
        android:textStyle="bold" /> 
    <Button 
        android:id="@+id/btnOpenAct2" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_centerInParent="true" 
        android:text="Open Second Activity" /> 
</RelativeLayout>

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

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "KotlinApp"

        val button = findViewById<Button>(R.id.btnOpenAct2)
        button.setOnClickListener {
            val intent = Intent(this@MainActivity, NewActivity::class.java)
            startActivity(intent)
        }
    }
}

পদক্ষেপ ৪: নতুন এক্টিভিটি তৈরি করুন

NewActivity.kt

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity

class NewActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_new)
        title = "KotlinApp"

        val button = findViewById<Button>(R.id.btnOpenMain)
        button.setOnClickListener {
            val intent = Intent(this@NewActivity, MainActivity::class.java)
            startActivity(intent)
        }
    }
}

activity_new.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=".NewActivity"> 
    <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_centerHorizontal="true" 
        android:layout_marginTop="40dp" 
        android:text="New Activity" 
        android:textColor="@android:color/holo_green_dark" 
        android:textSize="24sp" 
        android:textStyle="bold" /> 
    <Button 
        android:id="@+id/btnOpenMain" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_centerInParent="true" 
        android:text="Go back to MainActivity" /> 
</RelativeLayout>

পদক্ষেপ ৫: 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> 
        <activity android:name=".NewActivity" /> 
    </application> 
</manifest>

অ্যাপ্লিকেশন চালান

আপনার আসল অ্যান্ড্রয়েড ডিভাইস কম্পিউটারের সাথে সংযুক্ত আছে বলে ধরে। Android Studio-তে আপনার প্রজেক্টের এক্টিভিটি ফাইল খুলুন এবং টুলবারের রান আইকনে ক্লিক করুন। আপনার মোবাইল ডিভাইস সিলেক্ট করুন এবং ডিভাইসে ডিফল্ট স্ক্রিন দেখুন।

  1. অ্যান্ড্রয়েডে এক্টিভিটির মাঝপথে SharedPreferences ব্যবহার

  2. এন্ড্রয়েডে একসাথে সব অ্যাক্টিভিটি বন্ধ করার পদ্ধতি

  3. অ্যান্ড্রয়েডে এক্টিভিটির মাঝপথে Drawable পাস করা

  4. Android-এ বিভিন্ন অ্যাক্টিভিটির মধ্যে স্যুইচ করার পদ্ধতি