Hướng dẫn này minh họa các cách thêm icon (drawable) vào EditText: trái, phải, trên, dưới bằng XML attribute drawableStart/End/Top/Bottom.
Bước 1: Tạo dự án mới
Mở Android Studio, chọn File → New Project.
Bước 2: Giao diện cơ bản (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">
<!-- Icon phải (search) -->
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp"
android:paddingStart="12dp"
android:paddingEnd="12dp"
android:drawableEnd="@android:drawable/ic_menu_search"
android:hint="Tìm kiếm..." />
<!-- Icon trái (user) -->
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp"
android:layout_marginTop="16dp"
android:paddingStart="12dp"
android:paddingEnd="12dp"
android:drawableStart="@android:drawable/ic_menu_myplaces"
android:hint="Tên người dùng" />
<!-- Icon trái + phải (password) -->
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp"
android:layout_marginTop="16dp"
android:paddingStart="12dp"
android:paddingEnd="12dp"
android:drawableStart="@android:drawable/ic_lock_idle_lock"
android:drawableEnd="@android:drawable/ic_menu_close_clear_cancel"
android:hint="Mật khẩu"
android:inputType="textPassword" />
</LinearLayout>
Bước 3: Activity (src/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);
}
}
Bước 4: Drawable nền tùy chỉnh (res/drawable/edittext_background.xml) — Tùy chọn
<?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>
Áp dụng: android:background="@drawable/edittext_background"
Bước 5: Manifest (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>
💡 Nâng cao: Xử lý click icon (drawableEnd)
editText.setOnTouchListener((v, event) -> {
if (event.getAction() == MotionEvent.ACTION_UP) {
Drawable drawableEnd = editText.getCompoundDrawables()[2]; // index 2 = end/right
if (drawableEnd != null) {
int iconWidth = drawableEnd.getBounds().width();
int clickX = (int) event.getX();
int editTextWidth = editText.getWidth();
int paddingEnd = editText.getPaddingEnd();
// Kiểm tra click vào vùng icon
if (clickX > editTextWidth - paddingEnd - iconWidth) {
// Xử lý click icon (ví dụ: xóa text, toggle password visibility)
return true;
}
}
}
return false;
});
Chạy ứng dụng
Kết nối thiết bị, nhấn Run. Các EditText hiển thị icon hệ thống ở vị trí khác nhau.