ตัวอย่างนี้สาธิตวิธีใช้ getResources().getResourceName(int resId) เพื่อดึงชื่อ Resource (String) จาก Resource ID (int)
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่
Android Studio → File → New Project
ขั้นตอนที่ 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">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="16sp"
android:textStyle="bold" />
</RelativeLayout>
ขั้นตอนที่ 3: MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
// 1. ดึง Resource ID ของ TextView เอง
int resId = textView.getId(); // คืนค่า int เช่น 2131234567
// 2. แปลง Resource ID เป็นชื่อ Resource String
// รูปแบบ: "package:type/name" เช่น "com.example.sample:id/textView"
String resourceName = getResources().getResourceName(resId);
// 3. แสดงผล
textView.setText("Resource ID: " + resourceName);
}
}
ขั้นตอนที่ 4: 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>
รูปแบบผลลัพธ์
ผลลัพธ์จะเป็น String ในรูปแบบ: package_name:type/entry_name
| Type | ตัวอย่าง |
|---|---|
| id | com.example.sample:id/textView |
| layout | com.example.sample:layout/activity_main |
| drawable | com.example.sample:drawable/ic_launcher |
| string | com.example.sample:string/app_name |
| color | com.example.sample:color/colorPrimary |
Method ที่เกี่ยวข้องอื่นๆ
// ดึง Resource ID จากชื่อ (กลับกัน)
int resId = getResources().getIdentifier("textView", "id", getPackageName());
// ดึงชื่อ Type และ Entry แยกกัน
Resources res = getResources();
String type = res.getResourceTypeName(resId); // "id"
String entry = res.getResourceEntryName(resId); // "textView"
// ดึง Package Name
String packageName = res.getResourcePackageName(resId); // "com.example.sample"
กรณีใช้งาน (Use Cases)
- Debug/Logging: แสดงชื่อ Resource ใน Log แทน ID ตัวเลข
- Dynamic Resource Loading: โหลด Resource จากชื่อ String ที่ได้จาก Server/Config
- Testing: ตรวจสอบ Resource ID ถูกต้อง
- Theme/Style Inspection: ดูว่า View ใช้ Style/Attribute อะไร
การทดสอบ
รันแอป → TextView จะแสดงข้อความ: Resource ID: com.example.sample:id/textView (Package name อาจต่างไปตามโปรเจกต์)