रनटाइम पर व्यू का आकार प्राप्त करना
यह उदाहरण दर्शाता है कि ViewTreeObserver.OnGlobalLayoutListener का उपयोग करके रनटाइम पर किसी भी व्यू की चौड़ाई और ऊंचाई कैसे प्राप्त करें।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project से प्रोजेक्ट बनाएं।
चरण 2: लेआउट में ImageView और TextView जोड़ें (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:padding="8dp"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_gravity="center"
android:layout_margin="20sp"
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:textSize="16sp"
android:textStyle="bold" />
</RelativeLayout>
चरण 3: ड्रॉएबल में इमेज जोड़ें
res/drawable फोल्डर में कोई भी इमेज (.png/.jpg/.jpeg) कॉपी करके पेस्ट करें।
चरण 4: MainActivity.java में ViewTreeObserver लागू करें
import android.os.Bundle;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.imageView);
imageView.getViewTreeObserver().addOnGlobalLayoutListener(new GetViewSize());
}
class GetViewSize implements ViewTreeObserver.OnGlobalLayoutListener {
@Override
public void onGlobalLayout() {
View v = findViewById(R.id.imageView);
int width = v.getWidth();
int height = v.getHeight();
((TextView) findViewById(R.id.textView)).setText(String.format("चौड़ाई: %d, ऊंचाई: %d", width, height));
// लिस्टनर हटाएं ताकि बार-बार कॉल न हो
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
v.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
v.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
}
}
}
चरण 5: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.viewsize">
<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>
ऐप चलाएं
डिवाइस कनेक्ट करें और Run दबाएं। स्क्रीन पर ImageView की वास्तविक चौड़ाई और ऊंचाई पिक्सल में दिखाई देगी।
