Android में TextWatcher से रियल-टाइम कैरेक्टर काउंट
यह उदाहरण दर्शाता है कि TextWatcher का उपयोग करके EditText में टाइप करते समय कैरेक्टर काउंट को रियल-टाइम में कैसे प्रदर्शित किया जाए।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project चुनें।
चरण 2: लेआउट फ़ाइल (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="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="16dp"
android:text="कैरेक्टर काउंट: 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_centerHorizontal="true"
android:ems="10"
android:hint="यहां टाइप करें..."
android:inputType="textMultiLine"
android:gravity="top"
android:minHeight="200dp"
android:padding="12dp"
android:background="@android:drawable/edit_text"/>
</RelativeLayout>
चरण 3: MainActivity (src/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 {
private TextView countTextView;
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
countTextView = findViewById(R.id.textView);
editText = findViewById(R.id.editText);
// TextWatcher जोड़ें
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) {
// टेक्स्ट बदलते समय - रियल-टाइम काउंट के लिए बेस्ट
updateCharacterCount(s.length());
}
@Override
public void afterTextChanged(Editable s) {
// टेक्स्ट बदलने के बाद - Editable मॉडिफाई करने के लिए
}
});
}
private void updateCharacterCount(int count) {
countTextView.setText("कैरेक्टर काउंट: " + count);
// वैकल्पिक: लिमिट पर रंग बदलें
if (count > 140) {
countTextView.setTextColor(0xFFFF0000); // लाल
} else if (count > 120) {
countTextView.setTextColor(0xFFFFA500); // नारंगी
} else {
countTextView.setTextColor(0xFF000000); // काला
}
}
}
सुधार: मूल कोड में बग था
beforeTextChanged में editText.length() उपयोग करता था, जो पुराना लेंथ लौटाता है (क्योंकि टेक्स्ट अभी बदला नहीं है)। सही तरीका onTextChanged में s.length() या afterTextChanged में s.length() उपयोग करना है।
TextWatcher कॉलबैक तुलना
| कॉलबैक | सही लेंथ | उपयोग |
|---|---|---|
beforeTextChanged |
पुराना (बदलाव से पहले) | पिछला मान सेव करना |
onTextChanged |
नया (s.length()) | रियल-टाइम UI अपडेट ✓ |
afterTextChanged |
नया (s.length()) | Editable मॉडिफाई करना |
XML में maxLength का उपयोग
<EditText
...
android:maxLength="160"
android:hint="अधिकतम 160 कैरेक्टर"/>
चरण 4: 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>
</intent-filter>
</activity>
</application>
</manifest>
ऐप चलाएं
ऐप लॉन्च करें, EditText में टाइप करें, और ऊपर कैरेक्टर काउंट रियल-टाइम अपडेट होते देखें।

