ButterKnife ব্যবহার করে ভিউ বাইন্ডিং সহজ করা
ButterKnife হল Jake Wharton-এর তৈরি একটি লাইব্রেরি যা অ্যানোটেশন প্রসেসিং ব্যবহার করে `findViewById()` কলগুলোকে অটোমেট করে দেয়। দ্রষ্টব্য: বর্তমানে এটি মেইনটেইনেন্স মোডে আছে, নতুন প্রজেক্টে View Binding বা Data Binding ব্যবহার করা উচিত।
ধাপ ১: নতুন প্রজেক্ট তৈরি করুন
Android Studio-তে File → New Project থেকে প্রজেক্ট তৈরি করুন।
ধাপ ২: build.gradle (Module: app) এ ডিপেন্ডেন্সি যোগ করুন
dependencies {
implementation 'com.jakewharton:butterknife:10.2.3'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.3'
// Kotlin-এর জন্য kapt ব্যবহার করুন:
// kapt 'com.jakewharton:butterknife-compiler:10.2.3'
}
ধাপ ৩: লেআউট XML (activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
android:padding="16dp">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="..........."
android:textSize="18sp"
android:textStyle="bold"
android:textAllCaps="true"
android:layout_marginBottom="16dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="..........."
android:textSize="18sp"
android:textStyle="bold"
android:textAllCaps="true"
android:layout_marginBottom="16dp" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="..........."
android:textSize="18sp"
android:textStyle="bold"
android:textAllCaps="true"
android:layout_marginBottom="24dp" />
<Button
android:id="@+id/btnLoadText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Load Text"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
ধাপ ৪: MainActivity.java বাস্তবায়ন (ButterKnife সহ)
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class MainActivity extends AppCompatActivity {
// @BindView অ্যানোটেশন দিয়ে ভিউ বাইন্ড করুন
@BindView(R.id.textView) TextView textView;
@BindView(R.id.textView2) TextView textView2;
@BindView(R.id.textView3) TextView textView3;
@BindView(R.id.btnLoadText) Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// ButterKnife বাইন্ড করুন (onCreate-এ setContentView-এর পর)
ButterKnife.bind(this);
// এখন সরাসরি ভিউ এক্সেস করা যাবে findViewById ছাড়া
// textView.setText(...) কাজ করবে
}
// @OnClick অ্যানোটেশন দিয়ে ক্লিক লিসেনার
@OnClick(R.id.btnLoadText)
public void onLoadTextClick(View view) {
textView.setText("Hi, How are you?");
textView2.setText("Have a nice day!");
textView3.setText("You are so good.");
Toast.makeText(this, "Text loaded via ButterKnife!", Toast.LENGTH_SHORT).show();
}
// অন্য উদাহরণ: একাধিক ভিউয়ের ক্লিক হ্যান্ডলার
@OnClick({R.id.textView, R.id.textView2, R.id.textView3})
public void onTextViewClick(View view) {
switch (view.getId()) {
case R.id.textView:
Toast.makeText(this, "First text clicked", Toast.LENGTH_SHORT).show();
break;
case R.id.textView2:
Toast.makeText(this, "Second text clicked", Toast.LENGTH_SHORT).show();
break;
case R.id.textView3:
Toast.makeText(this, "Third text clicked", Toast.LENGTH_SHORT).show();
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
// ফ্র্যাগমেন্টে Unbinder.unbind() কল করুন, অ্যাক্টিভিটিতে অটোমেটিক হ্যান্ডল হয়
}
}
ButterKnife-এর মূল অ্যানোটেশনগুলো
| অ্যানোটেশন | ব্যবহার |
|---|---|
| @BindView | View বাইন্ড করতে (findViewById রিপ্লেস করে) |
| @BindViews | একাধিক View লিস্টে বাইন্ড করতে |
| @OnClick | Click listener সেট করতে |
| @OnLongClick | Long click listener |
| @OnTextChanged | TextWatcher সেট করতে |
| @OnItemClick | AdapterView item click |
| @BindString | String রিসোর্স বাইন্ড করতে |
| @BindColor | Color রিসোর্স বাইন্ড করতে |
| @BindDimen | Dimension রিসোর্স বাইন্ড করতে |
আধুনিক বিকল্প: View Binding (প্রস্তাবিত)
Android Gradle Plugin 3.6+ এ বিল্ট-ইন View Binding উপলব্ধ, কোনো এক্সট্রা লাইব্রেরি দরকার নেই:
build.gradle-এ সক্ষম করুন:
android {
buildFeatures {
viewBinding true
}
}
Activity-এ ব্যবহার:
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
// সরাসরি অ্যাক্সেস: binding.textView, binding.btnLoadText
binding.btnLoadText.setOnClickListener(v -> {
binding.textView.setText("Hello View Binding!");
});
}
@Override
protected void onDestroy() {
super.onDestroy();
binding = null; // মেমরি লিক প্রতিরোধ
}
}
View Binding-এর সুবিধা ButterKnife-এর চেয়ে
- কম্পাইল-টাইম টাইপ সেফটি (নাল সেফটি)
- অ্যাপারেন্সি প্রসেসিং ছাড়া (বিল্ড দ্রুত)
- অটো-জেনারেটেড বাইন্ডিং ক্লাস
- নাল সেফটি এবং IDE অটোকমপ্লিট
- অফিসিয়ালি সাপোর্টেড এবং মেইনটেইনড
ধাপ ৫: 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>
অ্যাপ্লিকেশন টেস্ট করুন
অ্যাপ রান করুন, "Load Text" বাটনে ক্লিক করলে তিনটি TextView-এ টেক্সট আপডেট হবে। ButterKnife ছাড়া findViewById লিখতে হয় না।