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

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

অ্যান্ড্রয়েডে GridLayout-কে স্ক্রিন আকার অনুযায়ী ফিট করা

এই উদাহরণটি দেখায় কিভাবে প্রোগ্রামেটিক্যালি GridLayout-এর কলাম ও রো সংখ্যা নির্ধারণ করে এবং ডাইনামিক্যালি ভিউ (ImageView) যোগ করে স্ক্রিনে ফিট করা যায়।

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

Android Studio-তে File → New Project এ যান এবং একটি নতুন প্রজেক্ট তৈরি করুন।

ধাপ ২: লেআউট XML (activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<GridLayout
    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: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.java

package com.app.sample;

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

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        GridLayout gridLayout = findViewById(R.id.tableGrid);
        gridLayout.removeAllViews(); // XML-এ থাকা বাটনগুলো মুছে দিন

        int total = 12;      // মোট আইটেম
        int column = 5;      // কলাম সংখ্যা
        int row = total / column; // রো সংখ্যা (বাকি থাকলে +1 হবে)

        gridLayout.setColumnCount(column);
        gridLayout.setRowCount(row + 1);

        for (int i = 0, c = 0, r = 0; i < total; i++, c++) {
            if (c == column) {
                c = 0;
                r++;
            }

            ImageView imageView = new ImageView(this);
            imageView.setImageResource(R.drawable.ic_launcher_background);

            GridLayout.LayoutParams param = new GridLayout.LayoutParams();
            param.height = TableLayout.LayoutParams.WRAP_CONTENT;
            param.width = TableLayout.LayoutParams.WRAP_CONTENT;
            param.rightMargin = 5;
            param.topMargin = 5;
            param.setGravity(Gravity.CENTER);
            param.columnSpec = GridLayout.spec(c);
            param.rowSpec = GridLayout.spec(r);

            imageView.setLayoutParams(param);
            gridLayout.addView(imageView);
        }
    }
}

ধাপ ৪: AndroidManifest.xml

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

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

অ্যাপটি চালান

ডিভাইস সংযুক্ত করে Run ক্লিক করুন। GridLayout ৫ কলামে ১২টি ইমেজ দেখাবে, যা স্ক্রিনের মধ্যে ফিট থাকবে।

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

  1. অ্যান্ড্রয়েডে ফোন কল করার পদ্ধতি

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

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

  4. Android-এ ছবি থেকে ওয়ালপেপার তৈরি করুন (এবং ফিট করান)