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

Android ऐप में स्क्रीन डायमेंशन पिक्सल में कैसे प्राप्त करें?

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

चरण 1: नया प्रोजेक्ट बनाएं

Android Studio में नया प्रोजेक्ट बनाएं।

चरण 2: लेआउट XML

res/layout/activity_main.xml - चार TextViews:

<?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"
    tools:context="MainActivity">
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="176dp"
        android:text="Width in Pixels: "
        android:textAppearance="?android:attr/textAppearanceLarge" />
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="16dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/textView1"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="24dp"
        android:text="Height in Pixels: "
        android:textAppearance="?android:attr/textAppearanceLarge" />
    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView3"
        android:layout_marginTop="16dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>

चरण 3: MainActivity.java में DisplayMetrics का उपयोग

src/MainActivity.java:

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.WindowManager;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    TextView one, two;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        one = findViewById(R.id.textView2);
        two = findViewById(R.id.textView4);

        DisplayMetrics displayMetrics = new DisplayMetrics();
        WindowManager windowManager = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
        windowManager.getDefaultDisplay().getMetrics(displayMetrics);

        int deviceWidth = displayMetrics.widthPixels;
        int deviceHeight = displayMetrics.heightPixels;

        one.setText(String.valueOf(deviceWidth));
        two.setText(String.valueOf(deviceHeight));
    }
}

चरण 4: 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>

ऐप चलाएं

स्क्रीन की चौड़ाई और ऊंचाई पिक्सल में TextViews में दिखाई देगी।

Android ऐप में स्क्रीन डायमेंशन पिक्सल में कैसे प्राप्त करें?

  1. Android में किसी ऐप की स्क्रीन को पिन कैसे करें Android में किसी ऐप की स्क्रीन को पिन कैसे करें

    यदि आपका कोई मित्र आपका फोन उधार लेना चाहता है ताकि वह ऐप्स में से एक का उपयोग कर सके, तो संभावना है कि आप नहीं चाहेंगे कि वह फोन के अन्य हिस्सों का पता लगाए। Android स्मार्टफोन आपको ऐप की स्क्रीन को पिन करने की अनुमति देकर इसके खिलाफ सुरक्षा प्रदान करते हैं ताकि आपका मित्र केवल वही ऐप उपयोग कर सके

  1. हटाए गए Android ऐप आइकन को जल्दी और सुरक्षित रूप से पुनर्स्थापित करें हटाए गए Android ऐप आइकन को जल्दी और सुरक्षित रूप से पुनर्स्थापित करें

    हम उन विभिन्न ऐप्स के शॉर्टकट आइकन होम स्क्रीन पर रखना पसंद करते हैं जिनका हम अक्सर उपयोग करते हैं। इससे डिवाइस अनलॉक करके सीधे ऐप आइकन पर टैप करना आसान हो जाता है। ऐप ड्रावर खोलने, कई ऐप्स को स्क्रॉल करने और फिर अंत में आवश्यक ऐप पर पहुँचने की आवश्यकता नहीं होती। Android आपको अपनी होम स्क्रीन को क

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

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