Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Android

एंड्रॉइड डिवाइस की स्क्रीन ऊंचाई और चौड़ाई कैसे प्राप्त करें?

एंड्रॉइड में स्क्रीन आयाम प्राप्त करने का पूरा गाइड

यह उदाहरण दर्शाता है कि एंड्रॉइड डिवाइस की स्क्रीन ऊंचाई, चौड़ाई और इंच में आकार को प्रोग्रामेटिक रूप से कैसे प्राप्त किया जाए।

चरण 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 आइकन पर क्लिक करें और अपना डिवाइस चुनें। ऐप स्क्रीन के आयाम प्रदर्शित करेगा।

एंड्रॉइड डिवाइस की स्क्रीन ऊंचाई और चौड़ाई कैसे प्राप्त करें?

  1. एंड्रॉइड में ImageView की ऊंचाई और चौड़ाई कैसे प्राप्त करें? एंड्रॉइड में ImageView की ऊंचाई और चौड़ाई कैसे प्राप्त करें?

    रनटाइम पर ImageView के आयाम प्राप्त करना यह उदाहरण दर्शाता है कि बटन क्लिक पर ImageView की वास्तविक ऊंचाई और चौड़ाई कैसे प्राप्त करें। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनें। चरण 2: लेआउट में ImageView, बटन और TextView जोड़ें res/layout/activity_main.xml: <?xml v

  1. अपनी Android स्क्रीन को TV पर कैसे कास्ट करें अपनी Android स्क्रीन को TV पर कैसे कास्ट करें

    यदि आप अपने Android डिवाइस से वीडियो और छवियों को बड़ी स्क्रीन पर देखना पसंद करते हैं, तो अपनी Android स्क्रीन को अपने TV या PC पर कास्ट करें। जबकि इनमें से कुछ विधियों के लिए आपके Android डिवाइस का Miracast के साथ संगत होना आवश्यक है, ये निश्चित रूप से एकमात्र विकल्प नहीं हैं। कास्टिंग शुरू करें और

  1. एंड्रॉइड पर ऑन-स्क्रीन वॉल्यूम कंट्रोल कैसे दिखाएं एंड्रॉइड पर ऑन-स्क्रीन वॉल्यूम कंट्रोल कैसे दिखाएं

    एंड्रॉइड फोन में डिवाइस की वॉल्यूम को कंट्रोल करने के लिए साइड में बटन होते हैं। आप गाने, पॉडकास्ट सुनते समय या वीडियो देखते समय इन बटनों का उपयोग करके आसानी से वॉल्यूम कंट्रोल कर सकते हैं। कभी-कभी, ये कुंजियां आपके फोन की वॉल्यूम को कंट्रोल करने का एकमात्र तरीका होती हैं। और अगर ये फिजिकल कीज़ टूट