TextWatcher ব্যবহার করে রিয়েল-টাইম ক্যারেক্টার কাউন্ট
এই উদাহরণটি দেখায় কীভাবে EditText-এ টাইপ করার সাথে সাথে ক্যারেক্টার সংখ্যা গুনে TextView-এ প্রদর্শন করা যায়।
ধাপ ১: নতুন প্রজেক্ট তৈরি করুন
Android Studio-তে File → New Project থেকে প্রজেক্ট তৈরি করুন।
ধাপ ২: লেআউট XML (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="16dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:text="Character Count: 0"
android:textSize="24sp"
android:textStyle="bold" />
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/textView"
android:layout_centerInParent="true"
android:ems="10"
android:hint="Type here..."
android:inputType="textMultiLine"
android:gravity="top" />
</RelativeLayout>
ধাপ ৩: MainActivity.java বাস্তবায়ন
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView textView;
EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
editText = findViewById(R.id.editText);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// টেক্সট পরিবর্তনের আগে - এখানে কাউন্ট আপডেট করা উচিত নয়
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// টেক্সট পরিবর্তন হয়ে যাওয়ার সময় - রিয়েল-টাইম কাউন্টের জন্য সেরা জায়গা
int length = s.length();
textView.setText("Character Count: " + length);
}
@Override
public void afterTextChanged(Editable s) {
// টেক্সট পরিবর্তন সম্পন্ন হয়ে গেলে
// এখানে ফরম্যাটিং বা ভ্যালিডেশন করা যায়
}
});
}
}
TextWatcher মেথডগুলোকে সঠিকভাবে ব্যবহার করা
| মেথড | কখন কল হয় | ব্যবহার |
|---|---|---|
| beforeTextChanged | পরিবর্তনের আগে | আনডু ফাংশন্যালিটি, পুরানো মান সংরক্ষণ |
| onTextChanged | পরিবর্তনের সময় | রিয়েল-টাইম আপডেট, কাউন্ট, ফিল্টার |
| afterTextChanged | পরিবর্তনের পর | ফরম্যাটিং, ভ্যালিডেশন, ডেটাবেজ সেভ |
সাধারণ বাগ এবং সমাধান
সমস্যা: মূল কোডে `beforeTextChanged`-এ কাউন্ট আপডেট করা হয়েছে, যা ভুল কারণ এখানে নতুন টেক্সট এখনো প্রয়োগ হয়নি।
সমাধান: `onTextChanged`-এ `s.length()` ব্যবহার করুন যা নতুন টেক্সটের দৈর্ঘ্য দেয়।
উন্নত ফিচার: ক্যারেক্টার লিমিট এবং রিমাইন্ডার
final int MAX_LENGTH = 160; // SMS লিমিট
editText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
int length = s.length();
int remaining = MAX_LENGTH - length;
if (remaining >= 0) {
textView.setText(remaining + " characters remaining");
textView.setTextColor(Color.BLACK);
} else {
textView.setText("Limit exceeded by " + (-remaining) + " chars");
textView.setTextColor(Color.RED);
}
}
@Override
public void afterTextChanged(Editable s) {
// লিমিট পার হলে অটোমেটিক কাটা (ঐচ্ছিক)
if (s.length() > MAX_LENGTH) {
int selectionStart = editText.getSelectionStart();
s.delete(MAX_LENGTH, s.length());
// কারসর পজিশন ঠিক রাখুন
if (selectionStart > MAX_LENGTH) {
editText.setSelection(MAX_LENGTH);
}
}
}
});
ধাপ ৪: 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>
অ্যাপ্লিকেশন টেস্ট করুন
অ্যাপ রান করুন এবং EditText-এ টাইপ করুন। উপরের TextView-এ ক্যারেক্টার কাউন্ট রিয়েল-টাইমে আপডেট হবে।