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

Kotlin का उपयोग करके Android नेविगेशन बार की ऊंचाई और चौड़ाई प्रोग्रामेटिक रूप से कैसे प्राप्त करें?

यह उदाहरण दर्शाता है कि Kotlin में सिस्टम रिसोर्स पहचानकर्ता का उपयोग करके नेविगेशन बार के आयाम कैसे प्राप्त करें।

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

Android Studio: File → New Project।

चरण 2 - 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"
    tools:context=".MainActivity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="70dp"
        android:background="#008080"
        android:padding="5dp"
        android:text="TutorialsPoint"
        android:textColor="#fff"
        android:textSize="24sp"
        android:textStyle="bold" />
    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Android नेविगेशन बार ऊंचाई और चौड़ाई"
        android:textColor="@android:color/holo_purple"
        android:textSize="16sp"
        android:textStyle="bold" />
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text"
        android:layout_centerInParent="true"
        android:layout_marginTop="10dp"
        android:textColor="@android:color/background_dark"
        android:textSize="24sp"
        android:textStyle="bold" />
</RelativeLayout>

चरण 3 - MainActivity.kt में आयाम प्राप्त करें

import android.content.res.Resources
import android.os.Bundle
import android.util.DisplayMetrics
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "KotlinApp"

        val textView = findViewById<TextView>(R.id.textView)
        val resources: Resources = resources
        val displayMetrics = DisplayMetrics()
        windowManager.defaultDisplay.getRealMetrics(displayMetrics)

        // नेविगेशन बार ऊंचाई के लिए रिसोर्स ID प्राप्त करें
        val navBarHeightId = resources.getIdentifier("navigation_bar_height", "dimen", "android")
        val navBarWidthId = resources.getIdentifier("navigation_bar_width", "dimen", "android")

        val navBarHeight = if (navBarHeightId > 0) resources.getDimensionPixelSize(navBarHeightId) else 0
        val navBarWidth = if (navBarWidthId > 0) resources.getDimensionPixelSize(navBarWidthId) else 0

        val screenWidth = displayMetrics.widthPixels
        val screenHeight = displayMetrics.heightPixels

        textView.text = """
            स्क्रीन: ${screenWidth} x ${screenHeight} px
            नेविगेशन बार ऊंचाई: ${navBarHeight} px
            नेविगेशन बार चौड़ाई: ${navBarWidth} px
        """.trimIndent()
    }
}

चरण 4 - AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.example.navbardimensions">
    <application ...>
        <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>

ऐप चलाएं

ऐप चलाएं। स्क्रीन पर नेविगेशन बार की ऊंचाई और चौड़ाई पिक्सेल में प्रदर्शित होगी।

महत्वपूर्ण: यह विधि केवल तभी काम करती है जब डिवाइस में सॉफ्ट नेविगेशन बार हो (हार्डवेयर बटन नहीं)। जेस्चर नेविगेशन मोड (Android 10+) में नेविगेशन बार ऊंचाई 0 हो सकती है।
आधुनिक विकल्प (API 30+): WindowInsets API का उपयोग करें:
ViewCompat.setOnApplyWindowInsetsListener(view) { v, insets ->
    val navBarInsets = insets.getInsets(WindowInsetsCompat.Type.navigationBars())
    val height = navBarInsets.bottom
    // ...
}
  1. Android में ImageView की चौड़ाई और ऊंचाई प्रोग्रामेटिकली कैसे सेट करें? Android में ImageView की चौड़ाई और ऊंचाई प्रोग्रामेटिकली कैसे सेट करें?

    यह उदाहरण दर्शाता है कि Android में ImageView को Java कोड से बनाकर उसकी चौड़ाई-ऊंचाई सेट करके RelativeLayout में कैसे जोड़ा जाए। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में नया प्रोजेक्ट बनाएं। चरण 2: लेआउट XML res/layout/activity_main.xml - खाली RelativeLayout: <?xml version=1.0 encoding=utf

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

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

  1. एंड्रॉइड में एक्शन बार की ऊंचाई कैसे प्राप्त करें? एंड्रॉइड में एक्शन बार की ऊंचाई कैसे प्राप्त करें?

    Toolbar.getHeight() के साथ एक्शन बार ऊंचाई मापें यह उदाहरण दर्शाता है कि Toolbar/ActionBar की रनटाइम ऊंचाई पिक्सेल में कैसे प्राप्त करें। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनें। चरण 2: लेआउट में Toolbar और बटन res/layout/activity_main.xml: <?xml version=1.0 encod