एंड्रॉइड में टाइपिंग के दौरान EditText में डायनामिक इमेज दिखाना
यह ट्यूटोरियल दिखाता है कि TextWatcher का उपयोग करके जब उपयोगकर्ता 3+ अक्षर टाइप करे तो EditText में कंपाउंड ड्रॉएबल (इमेज) कैसे दिखाएं।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project चुनें।
चरण 2: लेआउट (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:gravity="center"
android:layout_marginTop="30dp"
tools:context=".MainActivity">
<EditText
android:id="@+id/edit_query"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:paddingStart="5dp"
android:background="@drawable/rounded_edittext"
android:drawableStart="@android:drawable/ic_menu_search"
android:paddingLeft="5dp" />
</LinearLayout>
चरण 3: बैकग्राउंड ड्रॉएबल (drawable/rounded_edittext.xml)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF" />
<stroke android:width="1dp" android:color="#2f6699" />
<corners android:radius="10dp" />
</shape>
चरण 4: MainActivity कोड (java/MainActivity.java)
package com.example.myapplication;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText editText = findViewById(R.id.edit_query);
// ड्रॉएबल संसाधन (res/drawable/sir.xml या .png होना चाहिए)
final Drawable image = getResources().getDrawable(R.drawable.sir, getTheme());
image.setBounds(0, 0, 100, 100); // आकार सेट करें
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// टाइपिंग से पहले - इमेज हटाएं
editText.setCompoundDrawables(null, null, null, null);
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// टाइपिंग के दौरान - 3+ अक्षर पर इमेज दिखाएं
if (s.length() >= 3) {
// दाईं ओर इमेज सेट करें (null, null, image, null)
editText.setCompoundDrawables(null, null, image, null);
} else {
editText.setCompoundDrawables(null, null, null, null);
}
}
@Override
public void afterTextChanged(Editable s) {
// टाइपिंग के बाद - कुछ नहीं
}
});
}
}
मुख्य बिंदु
TextWatcherके तीन विधि:beforeTextChanged,onTextChanged,afterTextChangedsetCompoundDrawables(left, top, right, bottom)से प्रोग्रामेटिकली ड्रॉएबल सेट करेंsetBounds()ड्रॉएबल आकार सेट करने के लिए आवश्यक है- 3 अक्षर की शर्त:
s.length() >= 3
सुधार और वैकल्पिक
// Kotlin में सरल
editText.doAfterTextChanged { text ->
val drawable = if (text.length >= 3) ContextCompat.getDrawable(this, R.drawable.sir) else null
if (drawable != null) drawable.setBounds(0, 0, 100, 100)
editText.setCompoundDrawablesRelativeWithIntrinsicBounds(null, null, drawable, null)
}
// डिबाउंस के साथ (तेज टाइपिंग के लिए)
editText.addTextChangedListener(new TextWatcher() {
private Runnable runnable;
@Override public void onTextChanged(CharSequence s, int start, int before, int count) {
if (runnable != null) handler.removeCallbacks(runnable);
runnable = () -> updateDrawable(s.length());
handler.postDelayed(runnable, 300);
}
});
ऐप टेस्ट करें
चलाएं, 3+ अक्षर टाइप करें - दाईं ओर कस्टम इमेज दिखाई देगी। 3 से कम पर गायब हो जाएगी।