Giới thiệu
Bài viết minh họa cách lấy Resource ID (R.drawable.xxx) của hình ảnh khi chỉ biết tên file (String) qua phương thức getResources().getIdentifier().
Các bước thực hiện
Bước 1: Tạo dự án mới
Khởi tạo project Android Studio. Đảm bảo có file ảnh trong res/drawable/ (ví dụ: ic_launcher_background.png).
Bước 2: Giao diện hiển thị Resource ID (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:id="@+id/ivLauncer"
android:layout_width="match_parent"
android:layout_height="450dp"
android:contentDescription="@string/app_name"
android:src="@drawable/ic_launcher_background" />
<TextView
android:id="@+id/tvResourceId"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Bước 3: Logic lấy Resource ID (src/MainActivity.java)
package app.tutorialspoint.com.sample;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String drawableName = "ic_launcher_background"; // Tên file không đuôi mở rộng
int resID = getResources().getIdentifier(drawableName, "drawable", getPackageName());
TextView tvResourceId = findViewById(R.id.tvResourceId);
tvResourceId.setText("Resource ID: " + resID); // Ví dụ: 2131230845
Log.d("ResourceID", "ID của " + drawableName + " = " + resID);
}
}
Bước 4: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.tutorialspoint.com.sample">
<uses-permission android:name="android.permission.VIBRATE" />
<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. TextView sẽ hiển thị Resource ID số nguyên của drawable.

Giải thích phương thức getIdentifier()
int getIdentifier(String name, String defType, String defPackage)
- name: Tên resource (không đuôi .png, .xml, v.v.)
- defType: Loại resource: "drawable", "layout", "string", "id", "color", "dimen", "raw", "anim", v.v.
- defPackage: Package name của ứng dụng (
getPackageName()) - Trả về: Resource ID (int), trả về 0 nếu không tìm thấy
Các trường hợp sử dụng phổ biến
- Load ảnh động dựa trên tên:
"icon_" + categoryName - Theme động: thay đổi drawable theo cấu hình người dùng
- Localization: chọn resource theo ngôn ngữ/khu vực
- Plugin/Module: truy cập resource của module khác qua package name
⚠️ Cảnh báo hiệu suất
getIdentifier() chậm hơn truy cập trực tiếp R.drawable.xxx vì dùng reflection. Không dùng trong vòng lặp hoặc onDraw(). Cache kết quả nếu gọi nhiều lần.
Kotlin Extension (tiện lợi)
fun Context.getDrawableId(name: String): Int = resources.getIdentifier(name, "drawable", packageName)
// Dùng: val id = getDrawableId("ic_launcher_background")