एंड्रॉइड में स्क्रीन आयाम प्राप्त करने का पूरा गाइड
यह उदाहरण दर्शाता है कि एंड्रॉइड डिवाइस की स्क्रीन ऊंचाई, चौड़ाई और इंच में आकार को प्रोग्रामेटिक रूप से कैसे प्राप्त किया जाए।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project पर जाएं और आवश्यक विवरण भरकर एक नया प्रोजेक्ट बनाएं।
चरण 2: लेआउट XML में UI जोड़ें
res/layout/activity_main.xml में निम्न कोड जोड़ें:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:gravity="center_horizontal"
android:layout_marginTop="30dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/getScreenHeight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20sp"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/getScreenWidth"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/getScreenInches"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
चरण 3: MainActivity.java में लॉजिक जोड़ें
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView height, width, inches;
DisplayMetrics displayMetrics;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
height = findViewById(R.id.getScreenHeight);
width = findViewById(R.id.getScreenWidth);
inches = findViewById(R.id.getScreenInches);
displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int screenHeight = displayMetrics.heightPixels;
int screenWidth = displayMetrics.widthPixels;
// स्क्रीन आकार इंच में कैलकुलेट करें
double y = Math.pow(screenHeight / displayMetrics.xdpi, 2);
double x = Math.pow(screenWidth / displayMetrics.xdpi, 2);
double screenInches = Math.sqrt(x + y);
screenInches = (double) Math.round(screenInches * 10) / 10;
height.setText("स्क्रीन ऊंचाई: " + screenHeight);
width.setText("स्क्रीन चौड़ाई: " + screenWidth);
inches.setText("स्क्रीन इंच: " + screenInches);
}
}
चरण 4: AndroidManifest.xml कॉन्फ़िगर करें
सुनिश्चित करें कि आपके मैनिफेस्ट में मुख्य एक्टिविटी सही ढंग से घोषित है।
ऐप चलाएं
अपने एंड्रॉइड डिवाइस को कंप्यूटर से कनेक्ट करें, Android Studio में Run आइकन पर क्लिक करें और अपना डिवाइस चुनें। ऐप स्क्रीन के आयाम प्रदर्शित करेगा।