
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do I get the resource id of an image if I know its name in Android?
This example demonstrate about How do I get the resource id of an image if I know its name in Android
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.
Step 2 − Add the following code to res/layout/activity_main.java
<? xml version= "1.0" encoding= "utf-8" ?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://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>
Step 3 − Add the following code to 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 mDrawableName = "ic_launcher_background" ; int resID = getResources().getIdentifier(mDrawableName , "drawable" , getPackageName()) ; TextView tvResourceId = findViewById(R.id. tvResourceId ) ; tvResourceId.setText(String. valueOf (resID)) ; } }
Step 4 − Add the following code to androidManifest.xml
<? xml version= "1.0" encoding= "utf-8" ?> <manifest xmlns: android = "http://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>
Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click Run icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen –
- Related Questions & Answers
- How do I get the resource id of an image if I know its name in Android using Kotlin?
- How to get Resource name using Resource id in Android?
- How to get Resource Name using Resource id in Android using Kotlin?
- How do I get the src of an image in Selenium?
- How do I know if Python has pandas installed?
- How can I get the ID of an DOM element using jQuery?
- How can I know when an EditText loses focus in Android?
- How do I get the index of an item in Tkinter.Listbox?
- How do I get the id after INSERT into MySQL database in Python?
- How can I select an element by its ID attribute using jQuery?
- How can I add an image on EditText in Android?
- How do I display an alert dialog on Android?
- How do I restart an Android Activity using Kotlin?
- How do I get the height and width of the Android Navigation Bar programmatically?
- How can I select an element by its class name using jQuery?
Advertisements