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

Kotlin-এ Android GridLayout-কে স্ক্রিন সাইজ অনুযায়ী ফিট করার পদ্ধতি

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

এই উদাহরণটি দেখায় কিভাবে প্রোগ্রামেটিক্যালি GridLayout-এ কলাম এবং রো সেট করে বাটন/ইমেজ গ্রিডকে স্ক্রিন অনুযায়ী সাজানো যায়।

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

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

ধাপ ২: activity_main.xml লেআউট (XML-এ প্রাথমিক GridLayout)

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:id="@+id/tableGrid"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:columnCount="4"
    android:orientation="horizontal"
    tools:context=".MainActivity">

    <Button android:text="1" />
    <Button android:text="2" />
    <Button android:text="3" />
    <Button android:text="4" />
    <Button android:text="5" />
    <Button android:text="6" />
    <Button android:text="7" />
    <Button android:text="8" />
    <Button android:text="9" />
    <Button android:text="10" />
    <Button android:text="11" />
    <Button android:text="12" />
    <Button android:text="13" />
    <Button android:text="14" />
    <Button android:text="15" />
    <Button android:text="16" />

</GridLayout>

ধাপ ৩: MainActivity.kt-এ ডাইনামিক গ্রিড তৈরি করুন

import android.os.Bundle
import android.view.Gravity
import android.widget.GridLayout
import android.widget.ImageView
import android.widget.TableLayout
import androidx.appcompat.app.AppCompatActivity

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

        val gridLayout = findViewById<GridLayout>(R.id.tableGrid)
        gridLayout.removeAllViews() // XML-এ থাকা চাইল্ড হটাও

        val total = 12 // মোট আইটেম
        val column = 5 // কলাম সংখ্যা
        val row = total / column

        gridLayout.columnCount = column
        gridLayout.rowCount = row + 1

        var i = 0
        var c = 0
        var r = 0

        while (i < total) {
            if (c == column) {
                c = 0
                r++
            }

            val imageView = ImageView(this)
            imageView.setImageResource(R.drawable.ic_launcher_background)

            val param = GridLayout.LayoutParams().apply {
                height = TableLayout.LayoutParams.WRAP_CONTENT
                width = TableLayout.LayoutParams.WRAP_CONTENT
                rightMargin = 5
                topMargin = 5
                setGravity(Gravity.CENTER)
                columnSpec = GridLayout.spec(c)
                rowSpec = GridLayout.spec(r)
            }

            imageView.layoutParams = param
            gridLayout.addView(imageView)

            i++
            c++
        }
    }
}

ধাপ ৪: AndroidManifest.xml

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

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

পরীক্ষা করুন

অ্যাপ চালান। ৫ কলামের গ্রিডে ১২টি ইমেজ স্ক্রিনের মাঝপথে সুন্দরভাবে সাজানো থাকবে।

উন্নত বিকল্প

`RecyclerView` + `GridLayoutManager` ব্যবহার করলে আরও পারফরম্যান্স এবং রিসাইক্লিং সুবিধা পাওয়া যায়।

  1. Android-এ Intent ব্যবহার করে ফোন কল করার পদ্ধতি

  2. অ্যান্ড্রয়েডে অ্যালার্ট ডায়ালগকে স্ক্রিনের ৫০% আকারে সেট করা

  3. Android-এ বিভিন্ন স্ক্রিন সাইজ সাপোর্ট করার পদ্ধতি

  4. Android-এ GridLayout স্ক্রিন সাইজে ফিট করা