Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Android

एंड्रॉइड में EditText पर इमेज कैसे जोड़ें?

एंड्रॉइड में 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 फील्ड्स में बाएं/दाएं आइकन दिखाई देंगे।

  1. एंड्रॉइड में EditText खाली है या नहीं कैसे जांचें? एंड्रॉइड में EditText खाली है या नहीं कैसे जांचें?

    एंड्रॉइड में EditText खाली/वैलिड है या नहीं जांचना — बेस्ट प्रैक्टिसेज यूज़र इनपुट वैलिडेट करना हर ऐप की बुनियादी जरूरत है। TextUtils.isEmpty() सबसे सरल तरीका है, लेकिन **सिर्फ खाली स्ट्रिंग ही नहीं, व्हाइटस्पेस-ओनली स्ट्रिंग भी अमान्य माननी चाहिए**। यह ट्यूटोरियल सभी सामान्य पैटर्न कवर करता है। चर

  1. एंड्रॉइड में गोल कोनों वाला EditText कैसे बनाएं? एंड्रॉइड में गोल कोनों वाला EditText कैसे बनाएं?

    ड्रॉएबल शेप के साथ कस्टम EditText स्टाइलिंग यह उदाहरण दर्शाता है कि XML ड्रॉएबल शेप का उपयोग करके EditText के लिए गोल कोने, बॉर्डर और पृष्ठभूमि कैसे बनाएं। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनें। चरण 2: लेआउट में EditText जोड़ें res/layout/activity_main.xml: <?xm

  1. ट्किंटर में इमेज कैसे जोड़ें? ट्किंटर में इमेज कैसे जोड़ें?

    इमेज किसी भी एप्लिकेशन में बहुत उपयोगी ऑब्जेक्ट हैं। हम पायथन में Pillow या PIL पैकेज का उपयोग करके ट्किंटर एप्लिकेशन में इमेज प्रोसेस कर सकते हैं। इमेज लोड करने, निकालने, इमेज पेन कॉन्फ़िगर करने आदि जैसे कई बिल्ट-इन फंक्शन्स हैं। उदाहरण इस उदाहरण में, हम यूज़र से डायलॉग बॉक्स से इमेज चुनने के लिए