परिचय
यह उदाहरण दर्शाता है कि Kotlin का उपयोग करके रनटाइम पर एंड्रॉइड व्यू की चौड़ाई और ऊंचाई कैसे प्राप्त की जाए। ViewTreeObserver.OnGlobalLayoutListener का उपयोग करें क्योंकि onCreate में व्यू के डायमेंशन 0 होते हैं।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio खोलें, File → New Project पर जाएं और नया प्रोजेक्ट बनाने के लिए सभी आवश्यक विवरण भरें।
चरण 2: लेआउट XML कॉन्फ़िगर करें
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:orientation="vertical"
android:padding="8dp"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="500dp"
android:layout_gravity="center"
android:src="@drawable/image" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/imageView"
android:layout_centerInParent="true"
android:layout_marginTop="20sp"
android:textColor="@android:color/background_dark"
android:textSize="16sp"
android:textStyle="bold" />
</RelativeLayout>
चरण 3: इमेज जोड़ें
res/drawable फोल्डर में एक इमेज फाइल (.png/.jpg/.jpeg) कॉपी करें और उसका नाम image रखें।
चरण 4: Kotlin कोड लागू करें
src/MainActivity.kt में निम्नलिखित कोड जोड़ें:
import android.os.Bundle
import android.view.View
import android.view.ViewTreeObserver.OnGlobalLayoutListener
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private lateinit var imageView: ImageView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
imageView = findViewById(R.id.imageView)
// ग्लोबल लेआउट लिस्टनर जोड़ें - लेआउट पूरा होने पर कॉल होता है
imageView.viewTreeObserver.addOnGlobalLayoutListener(GetViewSize())
}
internal inner class GetViewSize : OnGlobalLayoutListener {
override fun onGlobalLayout() {
// लिस्टनर हटाएं ताकि बार-बार कॉल न हो (API 16+)
imageView.viewTreeObserver.removeOnGlobalLayoutListener(this)
val view = findViewById<View>(R.id.imageView)
val width = view.width
val height = view.height
(findViewById<View>(R.id.textView) as TextView).text =
String.format("चौड़ाई: %d, ऊंचाई: %d", width, height)
}
}
}
चरण 5: मैनिफेस्ट कॉन्फ़िगर करें
androidManifest.xml में निम्नलिखित कोड जोड़ें:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.q11">
<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>
ऐप चलाएं
अपने एंड्रॉइड डिवाइस को कंप्यूटर से कनेक्ट करें। Android Studio में प्रोजेक्ट की एक्टिविटी फाइल खोलें और टूलबार से Run आइकन पर क्लिक करें। अपना मोबाइल डिवाइस चुनें और ऐप का डिफॉल्ट स्क्रीन देखें।
विकल्प: doOnLayout एक्सटेंशन (Kotlin)
Kotlin एक्सटेंशन फंक्शन का उपयोग करके अधिक संक्षिप्त कोड:
fun View.doOnLayout(action: (View) -> Unit) {
viewTreeObserver.addOnGlobalLayoutListener(object : OnGlobalLayoutListener {
override fun onGlobalLayout() {
viewTreeObserver.removeOnGlobalLayoutListener(this)
action(this@doOnLayout)
}
})
}
// उपयोग:
imageView.doOnLayout { view ->
textView.text = "चौड़ाई: ${view.width}, ऊंचाई: ${view.height}"
}