स्ट्रिंग नाम से ड्रॉएबल रिसोर्स ID प्राप्त करना
यह उदाहरण getResources().getIdentifier() का उपयोग करके ड्रॉएबल नाम (स्ट्रिंग) से उसका रिसोर्स ID (इंट) प्राप्त करने का तरीका दिखाता है।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project चुनें।
चरण 2: लेआउट XML (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>
चरण 3: MainActivity.java
package app.tutorialspoint.com.sample;
import android.os.Bundle;
import androidx.appcompat.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";
// रिसोर्स ID प्राप्त करें: नाम, टाइप ("drawable"), पैकेज नाम
int resId = getResources().getIdentifier(drawableName, "drawable", getPackageName());
TextView tvResourceId = findViewById(R.id.tvResourceId);
tvResourceId.setText("रिसोर्स ID: " + resId);
Log.d("ResourceID", "Drawable ID: " + resId);
}
}
चरण 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>
getIdentifier() पैरामीटर्स
int getIdentifier(String name, String defType, String defPackage)
name: रिसोर्स का नाम (बिना एक्सटेंशन के, जैसे "ic_launcher")defType: रिसोर्स टाइप - "drawable", "string", "layout", "id", "color", "dimen", "raw", "anim", आदिdefPackage: पैकेज नाम (आमतौर परgetPackageName())
⚠️ चेतावनी
- यदि रिसोर्स नहीं मिलता, तो **0** लौटाता है (मान्य रिसोर्स ID कभी 0 नहीं होता)।
- रिफ्लेक्शन-आधारित होने के कारण **धीमा** है; लूप या onDraw() में उपयोग न करें।
- ProGuard/R8 रिलीज बिल्ड में अनुपयोगी रिसोर्सेज को हटा सकता है —
@Keepयाres/raw/keep.xmlका उपयोग करें।
आउटपुट
TextView में ड्रॉएबल का न्यूमेरिक रिसोर्स ID (जैसे 2131230845) दिखाई देगा।
