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

Kotlin-এ Android-এ একটিivity শুরু করার আগে প্রগ্রেস ডায়ালগ দেখানোর পদ্ধতি

পূর্ব প্রস্তুতি

এই উদাহরণটি দেখায় কিভাবে একটি নতুন অ্যাক্টিভিটি লোড হওয়ার আগে প্রগ্রেস ডায়ালগ প্রদর্শন করা যায়।

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

Android Studio-তে File → New Project নির্বাচন করুন।

ধাপ ২: 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:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="70dp"
        android:background="#008080"
        android:padding="5dp"
        android:text="TutorialsPoint"
        android:textColor="#fff"
        android:textSize="24sp"
        android:textStyle="bold" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Showing Progress dialog before starting the activity in Android.."
        android:textAlignment="center"
        android:textColor="@android:color/holo_blue_bright"
        android:textSize="24sp"
        android:textStyle="bold" />

</RelativeLayout>

ধাপ ৩: MainActivity.kt-এ AsyncTask দিয়ে প্রগ্রেস ডায়ালগ

import android.app.ProgressDialog
import android.os.AsyncTask
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity

@Suppress("DEPRECATION")
class MainActivity : AppCompatActivity() {
    private lateinit var progressDialog: ProgressDialog

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

        progressDialog = ProgressDialog(this)
        progressDialog.setTitle("Content Loader")
        progressDialog.progress = 10
        progressDialog.max = 100
        progressDialog.setMessage("Loading...")

        MyTask(this).execute()
    }

    companion object {
        inner class MyTask(private val mainActivity: MainActivity) : AsyncTask<Void, Void, Void>() {
            override fun doInBackground(vararg params: Void?): Void? {
                // Background task simulation
                Thread.sleep(3000)
                return null
            }

            override fun onPreExecute() {
                mainActivity.progressDialog.show()
            }

            override fun onPostExecute(result: Void?) {
                mainActivity.progressDialog.dismiss()
                // Start new activity here if needed
            }
        }
    }
}

ধাপ ৪: AndroidManifest.xml

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

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

নোট

AsyncTask API ডিপ্রিকেটেড। আধুনিক Android ডেভেলপমেন্টে Coroutines, Flow বা WorkManager ব্যবহার করা ভালো।

  1. Android-এ Alert Dialog প্রদর্শনের পদ্ধতি

  2. অ্যান্ড্রয়েড এলার্ট ডায়ালগে ListView প্রদর্শনের পদ্ধতি

  3. Progress Dialog in Android?

  4. অ্যান্ড্রয়েডে একটি অ্যাক্টিভিটি শুরু করার আগে প্রোগ্রেস ডায়ালগ কীভাবে দেখাবেন?