Hướng dẫn này trình bày cách kiểm tra EditText có rỗng (empty) hay không trong Android. Cách tốt nhất là dùng TextUtils.isEmpty() - xử lý cả null và chuỗi rỗng, an toàn và ngắn gọn.
Các bước thực hiện
Bước 1: Tạo dự án mới
Mở Android Studio, chọn File → New Project và điền thông tin cần thiết.
Bước 2: Layout (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textSize="16sp"
android:layout_marginTop="45dp"
android:hint="Nhập văn bản ở đây"
android:padding="16dp" />
<Button
android:id="@+id/buttonCheck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kiểm tra"
android:layout_centerInParent="true"
android:layout_marginTop="25sp"
android:layout_below="@id/editText" />
</RelativeLayout>
Bước 3: Code MainActivity (src/MainActivity.java)
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText editText;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.editText);
button = findViewById(R.id.buttonCheck);
button.setOnClickListener(v -> {
String input = editText.getText().toString();
// Cách 1: TextUtils.isEmpty() - KHUYẾN NGHỊ
// Trả về true nếu null HOẶC length() == 0
if (TextUtils.isEmpty(input)) {
Toast.makeText(MainActivity.this, "Trường không được để trống!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Hợp lệ: " + input, Toast.LENGTH_SHORT).show();
}
// Cách 2: Kiểm tra thủ công (không khuyến khích)
// if (input == null || input.length() == 0) { ... }
// Cách 3: Trim rồi kiểm tra (bỏ qua khoảng trắng đầu/cuối)
// if (TextUtils.isEmpty(input.trim())) { ... }
});
}
}
Bước 4: Cấu hình AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.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>
Chạy ứng dụng
Kết nối thiết bị, nhấn Run. Để trống EditText → nhấn "Kiểm tra" → Toast "Trường không được để trống!". Nhập text → nhấn → Toast "Hợp lệ: ...".

Các phương pháp kiểm tra rỗng - So sánh
| Phương pháp | Code | Xử lý null | Xử lý " " | Đánh giá |
|---|---|---|---|---|
| TextUtils.isEmpty() | TextUtils.isEmpty(str) |
Có | Không (coi " " không rỗng) | ⭐ Chuẩn, an toàn, ngắn gọn |
| TextUtils.isEmpty(trim()) | TextUtils.isEmpty(str.trim()) |
Có | Có (coi " " là rỗng) | ⭐ Khuyến nghị cho input user |
| str == null || str.isEmpty() | Hand-written | Có | Không | Dài, dễ quên check null |
| str != null && !str.isEmpty() | Kiểm tra KHÔNG rỗng | Có | Không | Dùng cho điều kiện ngược |
| str.trim().isEmpty() | Kotlin: str.isNullOrBlank() |
Nếu str=null → NPE | Có | Rủi ro NPE, dùng TextUtils |
Kotlin Extension (Rất tiện)
// Kotlin stdlib
fun EditText.isEmpty(): Boolean = TextUtils.isEmpty(text)
fun EditText.isBlank(): Boolean = TextUtils.isEmpty(text?.trim())
// Sử dụng:
if (editText.isBlank()) {
showError("Không được để trống")
}
Validation nâng cao (Form validation)
Đối với form phức tạp, hãy dùng thư viện hoặc pattern:
- Google Material TextInputLayout: Built-in error hiển thị dưới EditText
- Android Saripaar, Validator: Annotation-based validation
- Kotlin Result/Sealed class: Xử lý validation functional style
Ví dụ với TextInputLayout (Material Design)
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/tilEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/etEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email"
android:inputType="textEmailAddress" />
</com.google.android.material.textfield.TextInputLayout>
TextInputLayout tilEmail = findViewById(R.id.tilEmail);
TextInputEditText etEmail = findViewById(R.id.etEmail);
String email = etEmail.getText().toString().trim();
if (TextUtils.isEmpty(email)) {
tilEmail.setError("Email không được để trống");
tilEmail.setErrorEnabled(true);
} else if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
tilEmail.setError("Email không hợp lệ");
} else {
tilEmail.setError(null);
tilEmail.setErrorEnabled(false);
// Hợp lệ
}
Tóm tắt
- Luôn dùng
TextUtils.isEmpty()cho an toàn null - Dùng
.trim()nếu muốn coi khoảng trắng là rỗng - Kết hợp
TextInputLayoutđể UX tốt hơn (hiển thị error inline) - Kotlin: dùng
.isNullOrBlank()hoặc.isNullOrEmpty()