यह उदाहरण दर्शाता है कि GridLayout को प्रोग्रामेटिकली डायनामिक कॉलम/रो काउंट के साथ स्क्रीन साइज़ में फिट कैसे किया जाता है।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project।
चरण 2: लेआउट XML
res/layout/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="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:columnCount="4"
android:orientation="horizontal"
android:padding="8dp"
tools:context=".MainActivity">
<!-- प्रोग्रामेटिकली भरा जाएगा -->
</GridLayout>
चरण 3: 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;
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();
int totalItems = 12;
int columnCount = 5;
int rowCount = (int) Math.ceil((double) totalItems / columnCount);
gridLayout.setColumnCount(columnCount);
gridLayout.setRowCount(rowCount);
// स्क्रीन चौड़ाई के आधार पर सेल साइज़ कैलकुलेट करें
int screenWidth = getResources().getDisplayMetrics().widthPixels;
int cellSize = (screenWidth - (columnCount + 1) * 16) / columnCount; // पैडिंग/मार्जिन घटाकर
for (int i = 0, c = 0, r = 0; i < totalItems; i++, c++) {
if (c == columnCount) {
c = 0;
r++;
}
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.ic_launcher_background);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
GridLayout.LayoutParams params = new GridLayout.LayoutParams();
params.width = cellSize;
params.height = cellSize;
params.setGravity(Gravity.CENTER);
params.columnSpec = GridLayout.spec(c);
params.rowSpec = GridLayout.spec(r);
params.setMargins(8, 8, 8, 8);
imageView.setLayoutParams(params);
gridLayout.addView(imageView);
}
}
}
महत्वपूर्ण सुधार
- मूल कोड में
layout_width="wrap_content"था जिससे ग्रिड स्क्रीन फिट नहीं होता match_parentऔर डायनामिक सेल साइज़ कैलकुलेशन उपयोग करेंGridLayout.spec()से कॉलम/रो स्पेसिफाई करें
बेहतर विकल्प: RecyclerView + GridLayoutManager
GridLayout के बजाय RecyclerView उपयोग करना अधिक फ्लेक्सिबल और परफॉरमेंट-फ्रेंडली है:
// XML में
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
// एक्टिविटी में
RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new GridLayoutManager(this, 5));
recyclerView.setAdapter(new MyGridAdapter(items));
चरण 4: AndroidManifest.xml
मानक मैनिफेस्ट।
ऐप चलाएं
डिवाइस कनेक्ट करें, Run करें। ग्रिड स्क्रीन चौड़ाई में फिट होकर दिखेगा।
