Configuration এবং DisplayMetrics ব্যবহার করে স্ক্রিন সাইজ ও ডেনসিটি নির্ধারণ
এই উদাহরণটি দেখাবে কীভাবে রানটাইমে ডিভাইসের স্ক্রিন সাইজ ক্যাটাগরি (Small, Normal, Large, XLarge) এবং ডেনসিটি (LDPI, MDPI, HDPI, XHDPI, XXHDPI, XXXHDPI) প্রোগ্রামেটিক্যালি জানা যায়।
ধাপ ১: প্রজেক্ট তৈরি করুন
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"
android:padding="16dp"
tools:context=".MainActivity">
<Button
android:id="@+id/btnGetScreenSize"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:onClick="GetScreenSize"
android:text="Get ScreenSize" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/btnGetScreenSize"
android:layout_centerInParent="true"
android:layout_marginTop="10dp"
android:onClick="GetScreenDensity"
android:text="Get Screen Density" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="Android device screen size category (small, normal, large, xlarge)"
android:textAlignment="center"
android:textSize="24sp"
android:textStyle="bold|italic" />
</RelativeLayout>
ধাপ ৩: মেইন এক্টিভিটি (src/MainActivity.java)
import androidx.appcompat.app.AppCompatActivity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void GetScreenSize(View view) {
int screenLayout = getResources().getConfiguration().screenLayout;
int sizeCategory = screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK;
String sizeName;
if (sizeCategory == Configuration.SCREENLAYOUT_SIZE_SMALL) {
sizeName = "Small";
} else if (sizeCategory == Configuration.SCREENLAYOUT_SIZE_NORMAL) {
sizeName = "Normal";
} else if (sizeCategory == Configuration.SCREENLAYOUT_SIZE_LARGE) {
sizeName = "Large";
} else if (sizeCategory == Configuration.SCREENLAYOUT_SIZE_XLARGE) {
sizeName = "XLarge";
} else {
sizeName = "Unknown";
}
Toast.makeText(this, "Screen Size Category: " + sizeName, Toast.LENGTH_LONG).show();
}
public void GetScreenDensity(View view) {
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int density = metrics.densityDpi;
String densityName;
switch (density) {
case DisplayMetrics.DENSITY_LOW:
densityName = "LDPI (Low)"; break;
case DisplayMetrics.DENSITY_MEDIUM:
densityName = "MDPI (Medium)"; break;
case DisplayMetrics.DENSITY_HIGH:
densityName = "HDPI (High)"; break;
case DisplayMetrics.DENSITY_XHIGH:
densityName = "XHDPI (Extra High)"; break;
case DisplayMetrics.DENSITY_XXHIGH:
densityName = "XXHDPI (Extra Extra High)"; break;
case DisplayMetrics.DENSITY_XXXHIGH:
densityName = "XXXHDPI (Extra Extra Extra High)"; break;
case DisplayMetrics.DENSITY_TV:
densityName = "TV"; break;
default:
densityName = "Custom (" + density + " dpi)";
}
Toast.makeText(this, "Screen Density: " + densityName + " (" + density + " dpi)", Toast.LENGTH_LONG).show();
}
}
ধাপ ৪: মেনিফেস্ট (androidManifest.xml)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.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>
ব্যাখ্যা
- Screen Size Category:
Configuration.SCREENLAYOUT_SIZE_MASKবিটমাস্ক দিয়ে সাইজ ক্যাটাগরি বের করা হয়। - Screen Density:
DisplayMetrics.densityDpiদিয়ে dpi পাওয়া যায়। স্ট্যান্ডার্ড বাকেট: 120 (LDPI), 160 (MDPI), 240 (HDPI), 320 (XHDPI), 480 (XXHDPI), 640 (XXXHDPI)। - এই তথ্য ব্যবহার করে রেসপন্সিভ লেআউট, ড্রয়েবল সিলেকশন, বা এনালিটিক্স করা যায়।
ফলাফল
"Get ScreenSize" বাটনে ক্লিক করলে স্ক্রিন ক্যাটাগরি (Small/Normal/Large/XLarge) এবং "Get Screen Density" ক্লিক করলে ডেনসিটি (MDPI, HDPI ইত্যাদি) টোস্টে দেখাবে।
