Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

วิธีเพิ่มรูปภาพใน EditText (Drawable) ใน Android

ตัวอย่างนี้สาธิตวิธีเพิ่มไอคอน/รูปภาพเข้าไปใน EditText โดยใช้ Attribute drawableLeft, drawableRight, drawableStart, drawableEnd, drawableTop, drawableBottom

ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่

Android Studio → File → New Project

ขั้นตอนที่ 2: Layout (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_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:paddingStart="5dp"
        android:paddingLeft="5dp"
        android:drawableRight="@android:drawable/ic_menu_search"
        android:hint="ค้นหา..." />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="16dp"
        android:paddingStart="5dp"
        android:drawableLeft="@android:drawable/ic_menu_myplaces"
        android:hint="สถานที่" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="16dp"
        android:paddingStart="5dp"
        android:drawableStart="@android:drawable/ic_menu_call"
        android:drawableEnd="@android:drawable/ic_menu_send"
        android:hint="โทร / ส่งข้อความ" />

</LinearLayout>

ขั้นตอนที่ 3: MainActivity.java

package com.example.sample;

import android.support.v7.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);
    }
}

Attribute สำหรับ Drawable ใน EditText/TextView

Attribute ตำแหน่ง API Level
drawableLeft ซ้าย (Left) 1+
drawableRight ขวา (Right) 1+
drawableTop บน (Top) 1+
drawableBottom ล่าง (Bottom) 1+
drawableStart เริ่มต้น (RTL-aware) 17+
drawableEnd สิ้นสุด (RTL-aware) 17+

ควบคุมระยะห่าง (Padding/Margin)

EditText editText = findViewById(R.id.editText);

// เซ็ต Drawable ทั้ง 4 ด้าน (Left, Top, Right, Bottom)
// null = ไม่มี Drawable
editText.setCompoundDrawablesWithIntrinsicBounds(
    R.drawable.ic_left,   // Left
    0,                    // Top
    R.drawable.ic_right,  // Right
    0                     // Bottom
);

// หรือเซ็ตแค่ด้านเดียว
editText.setCompoundDrawablesRelativeWithIntrinsicBounds(
    R.drawable.ic_start,  // Start (RTL-aware)
    0,
    0,
    0
);

ใช้รูปภาพของตัวเอง (Custom Drawable)

  1. วางไฟล์รูป (PNG/WebP) ใน res/drawable/
  2. อ้างอิง: android:drawableRight="@drawable/my_icon"
  3. แนะนำ: ใช้ VectorDrawable (.xml) เพื่อขนาดเล็กและ Scale ได้ไม่เสียคุณภาพ

ขั้นตอนที่ 4: Drawable Background (Optional - res/drawable/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>

ใช้กับ EditText: android:background="@drawable/background"

ขั้นตอนที่ 5: AndroidManifest.xml

<?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">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

การทดสอบ

รันแอป → จะเห็น EditText 3 แบบ: มีไอคอนค้นหาด้านขวา, ไอคอนสถานที่ด้านซ้าย, ไอคอนโทร/ส่งทั้งสองข้าง