एंड्रॉइड में EditText पर ड्रॉएबल (इमेज/आइकन) जोड़ना
यह उदाहरण दिखाता है कि `android:drawableLeft/Right/Start/End` और `android:drawablePadding` एट्रिब्यूट का उपयोग करके EditText के अंदर आइकन कैसे जोड़ा जाता है।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project चुनकर नया प्रोजेक्ट बनाएं।
चरण 2: लेआउट XML - बिल्ट-इन आइकन के साथ
res/layout/activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
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:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:drawableEnd="@android:drawable/ic_menu_search"
android:drawablePadding="10dp"
android:hint="खोजें..." />
<!-- बाएं तरफ यूजर आइकन, दाएं तरफ क्लियर आइकन -->
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp"
android:layout_marginTop="20dp"
android:padding="10dp"
android:drawableStart="@android:drawable/ic_menu_myplaces"
android:drawableEnd="@android:drawable/ic_menu_close_clear_cancel"
android:drawablePadding="10dp"
android:hint="उपयोगकर्ता नाम" />
<!-- कस्टम ड्रॉएबल (res/drawable में) -->
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp"
android:layout_marginTop="20dp"
android:padding="10dp"
android:drawableStart="@drawable/ic_email"
android:drawablePadding="12dp"
android:hint="ईमेल"
android:inputType="textEmailAddress"
android:background="@drawable/edittext_background" />
</LinearLayout>
चरण 3: कस्टम बैकग्राउंड ड्रॉएबल
res/drawable/edittext_background.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
MainActivity.java:
package com.example.sample;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
चरण 5: प्रोग्रामेटिकली ड्रॉएबल बदलना
EditText editText = findViewById(R.id.editText);
// कंपाउंड ड्रॉएबल सेट करें: left, top, right, bottom
editText.setCompoundDrawablesWithIntrinsicBounds(
R.drawable.ic_email, // left/start
0, // top
R.drawable.ic_clear, // right/end
0 // bottom
);
// ड्रॉएबल और टेक्स्ट के बीच पैडिंग
editText.setCompoundDrawablePadding(16);
// RTL सपोर्ट के लिए (API 17+):
editText.setCompoundDrawablesRelativeWithIntrinsicBounds(
R.drawable.ic_email, // start
0, // top
R.drawable.ic_clear, // end
0 // bottom
);
चरण 6: Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.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" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
📋 ड्रॉएबल पोजीशन एट्रिब्यूट्स
| एट्रिब्यूट | पोजीशन | RTL में व्यवहार |
|---|---|---|
| `drawableLeft` | बाएं | फिक्स्ड (बाएं ही रहता है) |
| `drawableRight` | दाएं | फिक्स्ड (दाएं ही रहता है) |
| `drawableStart` | शुरुआत | RTL में दाएं, LTR में बाएं |
| `drawableEnd` | अंत | RTL में बाएं, LTR में दाएं |
| `drawableTop` | ऊपर | - |
| `drawableBottom` | नीचे | - |
💡 प्रो टिप: क्लिक करने योग्य आइकन
दाएं आइकन पर क्लिक लिसनर के लिए:
editText.setOnTouchListener((v, event) -> {
if (event.getAction() == MotionEvent.ACTION_UP) {
Drawable[] drawables = editText.getCompoundDrawables();
Drawable endDrawable = drawables[2]; // दाएं ड्रॉएबल
if (endDrawable != null) {
int drawableWidth = endDrawable.getIntrinsicWidth();
int padding = editText.getCompoundDrawablePadding();
int textWidth = editText.getWidth() - editText.getPaddingRight() - padding - drawableWidth;
if (event.getRawX() >= textWidth) {
// आइकन पर क्लिक हुआ
editText.setText("");
return true;
}
}
}
return false;
});
ऐप चलाएं
डिवाइस कनेक्ट करें, Run पर क्लिक करें। EditText फील्ड्स में बाएं/दाएं आइकन दिखाई देंगे।