রানটাইমে View-এর প্রস্থ ও উচ্চতা পাওয়ার উদাহরণ
এই উদাহরণটি দেখায় কিভাবে `ViewTreeObserver.OnGlobalLayoutListener` ব্যবহার করে লেআউট রেন্ডার হওয়ার পর View-এর আকার নির্ণয় করা যায়।
ধাপ ১: নতুন প্রজেক্ট তৈরি করুন
Android Studio-তে File → New Project এ যান এবং প্রয়োজনীয় বিবরণ পূরণ করে একটি নতুন প্রজেক্ট তৈরি করুন।
ধাপ ২: লেআউট 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: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>
ধাপ ৩: ড্রয়েবল ফোল্ডারে ছবি রাখুন
res/drawable ফোল্ডারে একটি ছবি (.png/.jpg/.jpeg) কপি-পেস্ট করুন এবং নাম দিন `image`।
ধাপ ৪: MainActivity.java-এ লজিক লিখুন
src/MainActivity.java ফাইলে নিচের কোডটি যোগ করুন:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.TextView;
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);
String width = Integer.toString(v.getWidth());
String height = Integer.toString(v.getHeight());
((TextView) findViewById(R.id.textView)).setText(
String.format("প্রস্থ: %s, উচ্চতা: %s", 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);
}
}
}
}
ধাপ ৫: AndroidManifest.xml কনফিগার করুন
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>
অ্যাপ্লিকেশন চালানোর পদ্ধতি
আপনার Android ডিভাইস কম্পিউটারের সাথে সংযুক্ত করুন। Android Studio-তে প্রজেক্টের activity ফাইল খুলে টুলবারের Run আইকনে ক্লিক করুন। আপনার মোবাইল ডিভাইস সিলেক্ট করুন এবং আউটপুট পরীক্ষা করুন।
