Configuration और DisplayMetrics का उपयोग करके स्क्रीन आकार श्रेणी और घनत्व पहचानना
एंड्रॉइड डिवाइस को चार सामान्यीकृत स्क्रीन आकार श्रेणियों में वर्गीकृत करता है: small, normal, large, xlarge। यह वर्गीकरण Configuration.screenLayout के माध्यम से उपलब्ध है। इसके अलावा स्क्रीन घनत्व (ldpi, mdpi, hdpi, xhdpi, xxhdpi, xxxhdpi) DisplayMetrics से प्राप्त होता है।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में प्रोजेक्ट बनाएं।
चरण 2: लेआउट फ़ाइल
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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerInParent="true"
android:gravity="center">
<Button
android:id="@+id/btnScreenSize"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:onClick="showScreenSize"
android:text="स्क्रीन आकार श्रेणी दिखाएं" />
<Button
android:id="@+id/btnScreenDensity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="showScreenDensity"
android:text="स्क्रीन घनत्व दिखाएं" />
<TextView
android:id="@+id/tvDetails"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:gravity="center"
android:textSize="16sp"
android:lineSpacingExtra="4dp"
tools:text="परिणाम यहाँ दिखेगा" />
</LinearLayout>
</RelativeLayout>
चरण 3: MainActivity में डिटेक्शन लॉजिक
package com.app.sample;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private TextView tvDetails;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvDetails = findViewById(R.id.tvDetails);
// प्रारंभ में दोनों दिखाएं
showScreenSize(null);
showScreenDensity(null);
}
// स्क्रीन आकार श्रेणी: small, normal, large, xlarge
public void showScreenSize(View view) {
Configuration config = getResources().getConfiguration();
int screenLayout = config.screenLayout;
int sizeCategory = screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK;
String sizeName;
String description;
switch (sizeCategory) {
case Configuration.SCREENLAYOUT_SIZE_SMALL:
sizeName = "SMALL (छोटा)";
description = "छोटे स्क्रीन वाले डिवाइस (पुराने फोन, ~3 इंच से कम)";
break;
case Configuration.SCREENLAYOUT_SIZE_NORMAL:
sizeName = "NORMAL (सामान्य)";
description = "अधिकांश फोन (~3-5 इंच)";
break;
case Configuration.SCREENLAYOUT_SIZE_LARGE:
sizeName = "LARGE (बड़ा)";
description = "बड़े फोन / छोटे टैबलेट (~5-7 इंच)";
break;
case Configuration.SCREENLAYOUT_SIZE_XLARGE:
sizeName = "XLARGE (अतिरिक्त बड़ा)";
description = "टैबलेट (~7-10 इंच+)";
break;
default:
sizeName = "UNKNOWN (" + sizeCategory + ")";
description = "अज्ञात श्रेणी";
}
// अतिरिक्त: लॉन्ग स्क्रीन जांच
boolean isLongScreen = (screenLayout & Configuration.SCREENLAYOUT_LONG_MASK) == Configuration.SCREENLAYOUT_LONG_YES;
String result = "📱 स्क्रीन आकार श्रेणी: " + sizeName + "\n" +
"विवरण: " + description + "\n" +
"लॉन्ग स्क्रीन: " + (isLongScreen ? "हाँ" : "नहीं") + "\n" +
"स्क्रीन लेआउट रॉ: " + screenLayout;
tvDetails.setText(result);
if (view != null) Toast.makeText(this, sizeName, Toast.LENGTH_SHORT).show();
}
// स्क्रीन घनत्व: ldpi, mdpi, hdpi, xhdpi, xxhdpi, xxxhdpi
public void showScreenDensity(View view) {
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int densityDpi = metrics.densityDpi;
float density = metrics.density; // dp से px अनुपात
int widthPx = metrics.widthPixels;
int heightPx = metrics.heightPixels;
float xdpi = metrics.xdpi;
float ydpi = metrics.ydpi;
String densityName;
String densityDesc;
switch (densityDpi) {
case DisplayMetrics.DENSITY_LOW:
densityName = "LDPI (कम ~120 dpi)";
densityDesc = "पुराने/कम रिज़ॉल्यूशन डिवाइस";
break;
case DisplayMetrics.DENSITY_MEDIUM:
densityName = "MDPI (मध्यम ~160 dpi) - बेसलाइन";
densityDesc = "मूल एंड्रॉइड बेसलाइन घनत्व";
break;
case DisplayMetrics.DENSITY_HIGH:
densityName = "HDPI (उच्च ~240 dpi)";
densityDesc = "प्रारंभिक हाई-रेज फोन";
break;
case DisplayMetrics.DENSITY_XHIGH:
densityName = "XHDPI (अतिरिक्त उच्च ~320 dpi)";
densityDesc = "आधुनिक मिड-रेंज फोन";
break;
case DisplayMetrics.DENSITY_XXHIGH:
densityName = "XXHDPI (अतिरिक्त अतिरिक्त उच्च ~480 dpi)";
densityDesc = "हाई-एंड फोन (फ्लैगशिप)";
break;
case DisplayMetrics.DENSITY_XXXHIGH:
densityName = "XXXHDPI (अतिरिक्त³ उच्च ~640 dpi)";
densityDesc = "अल्ट्रा-हाई-रेज डिस्प्ले";
break;
case DisplayMetrics.DENSITY_TV:
densityName = "TV (~213 dpi)";
densityDesc = "टेलीविजन डिवाइस";
break;
default:
densityName = "कस्टम (" + densityDpi + " dpi)";
densityDesc = "गैर-मानक घनत्व";
}
// भौतिक आकार इंच में
float widthInches = widthPx / xdpi;
float heightInches = heightPx / ydpi;
double diagonalInches = Math.sqrt(widthInches * widthInches + heightInches * heightInches);
String result = "📊 स्क्रीन घनत्व: " + densityName + "\n" +
"विवरण: " + densityDesc + "\n" +
"घनत्व अनुपात (density): " + density + " (1dp = " + density + "px)\n" +
"रिज़ॉल्यूशन: " + widthPx + " x " + heightPx + " px\n" +
"भौतिक आकार: " + String.format("%.1f x %.1f इंच", widthInches, heightInches) + "\n" +
"विकर्ण: " + String.format("%.1f इंच", diagonalInches);
tvDetails.setText(result);
if (view != null) Toast.makeText(this, densityName, Toast.LENGTH_SHORT).show();
}
}
स्क्रीन आकार श्रेणी और घनत्व बकेट
| श्रेणी | लगभग विकर्ण | उदाहरण डिवाइस |
|---|---|---|
| Small | < 3 इंच | पुराने फोन (HTC Wildfire, आदि) |
| Normal | 3 - 5 इंच | अधिकांश फोन (Galaxy S सीरीज, Pixel, आदि) |
| Large | 5 - 7 इंच | बड़े फोन, छोटे टैबलेट (Note सीरीज, iPad Mini) |
| XLarge | > 7 इंच | टैबलेट (iPad, Galaxy Tab, आदि) |
| घनत्व बकेट | DPI रेंज | स्केल फैक्टर | उपयोग |
|---|---|---|---|
| ldpi | ~120 | 0.75x | विरले ही अब |
| mdpi | ~160 | 1.0x (बेसलाइन) | रेफरेंस |
| hdpi | ~240 | 1.5x | पुराने हाई-एंड |
| xhdpi | ~320 | 2.0x | मिड-रेंज |
| xxhdpi | ~480 | 3.0x | फ्लैगशिप |
| xxxhdpi | ~640 | 4.0x | प्रीमियम/VR |
रिसोर्स क्वालिफायर्स (रिस्पॉन्सिव डिज़ाइन के लिए)
अलग-अलग स्क्रीन के लिए अलग रिसोर्सेज प्रदान करें:
चरण 4: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="https://schemas.android.com/apk/res/android" package="com.app.sample"> <!-- स्क्रीन सपोर्ट घोषणा (वैकल्पिक लेकिन अनुशंसित) --> <supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" android:requiresSmallestWidthDp="320" android:compatibleWidthLimitDp="720" android:largestWidthLimitDp="960" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/Theme.AppCompat.Light"> <activity android:name=".MainActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>आधुनिक दृष्टिकोण: WindowMetrics (API 30+)
// एंड्रॉइड 11+ (API 30) - विंडो मेट्रिक्स (मल्टी-विंडो, फोल्डेबल्स के लिए बेहतर) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { WindowMetrics windowMetrics = getWindowManager().getCurrentWindowMetrics(); WindowInsets windowInsets = windowMetrics.getWindowInsets(); Insets insets = windowInsets.getInsetsIgnoringVisibility(WindowInsets.Type.systemBars()); Rect bounds = windowMetrics.getBounds(); int width = bounds.width() - insets.left - insets.right; int height = bounds.height() - insets.top - insets.bottom; // ये ऐप के लिए उपलब्ध वास्तविक ड्रॉइंग क्षेत्र हैं }Jetpack Compose में
@Composable fun ScreenInfo() { val density = LocalDensity.current val configuration = LocalConfiguration.current val screenWidthDp = with(density) { configuration.screenWidthDp.roundToInt() } val screenHeightDp = with(density) { configuration.screenHeightDp.roundToInt() } val smallestWidthDp = configuration.smallestScreenWidthDp val sizeClass = when { smallestWidthDp >= 600 -> "Tablet" smallestWidthDp >= 400 -> "Large Phone" else -> "Phone" } Text("आकार वर्ग: $sizeClass, चौड़ाई: ${screenWidthDp}dp, ऊंचाई: ${screenHeightDp}dp") }नोट:Configuration.screenLayoutडिवाइस की सामान्यीकृत श्रेणी देता है, वास्तविक भौतिक आकार नहीं। रिस्पॉन्सिव लेआउट के लिएsmallestWidth(swXXXdp क्वालिफायर) अधिक विश्वसनीय है क्योंकि यह ओरिएंटेशन-स्वतंत्र है।