[Android] How to convert a character string to resourceId

important point

--fragment will result in an error unless getActivity (). getPackageName () is used. --If you use Integer.parseInt (), you get a java.lang.NumberFormatException error.

MainActivity.java


//java.lang.I get a NumberFormatException error
String bookImage = "book_image";
int imageId = Integer.parseInt(bookImage);

How to convert a string to resourceId in Activity

MainActivity.java


String bookImage = "book_image";
int imageId = getResources().getIdentifier(
                    bookImage, "drawable", getPackageName());

-----------------------------
//Output result
bookImage : book_image
imageId   : 2131099756 (Some number)

How to convert a string to resourceId in Fragment

MainFragment.java


String bookImage = "book_image";
int imageId = getResources().getIdentifier(
                    bookImage, "drawable", getActivity().getPackageName());

-----------------------------
//Output result
bookImage : book_image
imageId   : 2131099756(Some number)

Recommended Posts

[Android] How to convert a character string to resourceId
[Java] How to convert a character string from String type to byte type
[Java] How to cut out a character string character by character
[Java] How to erase a specific character from a character string
Convert iso-2022-jp character string to utf-8
[Java] How to use substring to cut out a character string
[Convenient to remember !!!] How to convert from LocalDate type to character string and from character string to LocalDate type
Convert to a tag to URL string in Rails
Convert a string to a character-by-character array with swift
How to convert a solidity contract to a Java contract class
[Java] How to use substring to cut out a part of a character string
[Ruby] How to generate a random alphabet string
[Java] How to convert one element of a String type array to an Int type
How to convert A to a and a to A using AND and OR in Java
How to convert a file to a byte array in Java
Convert String type to Timestamp type
Convert to Ruby Leet string
How to leave a comment
How to use String [] args
How to convert Java radix
How to insert a video
How to create a method
How to take a screenshot with the Android Studio emulator
Convert a Java byte array to a string in hexadecimal notation
How to change a string in an array to a number in Ruby
How to store a string from ArrayList to String in Java (Personal)
I tried to convert a string to a LocalDate type in Java
How to add columns to a table
Android Development-Try to display a dialog-
How to "hollow" View on Android
How to make a Java container
How to enclose any character with "~"
How to sign a Minecraft MOD
How to make a JDBC driver
How to convert erb file to haml
I want to convert InputStream to String
[Java] How to create a folder
How to write a ternary operator
How to convert LocalDate and Timestamp
[Swift] How to send a notification
How to make a splash screen
How to make a Jenkins plugin
How to make a Maven project
[Android] Convert Android Java code to Kotlin
How to make a Java array
[Android] How to make Dialog Fragment
[Rails] How to omit the display of the character string of the link_to method
[Java] How to convert from String to Path type and get the path
How to check for the contents of a java fixed-length string
About the method to convert a character string to an integer / decimal number (cast data) in Java
Code to specify the image name as a character string and paste it into ImageView (android)
[Java] Divide a character string by a specified character
How to execute a contract using web3j
How to sort a List using Comparator
[Swift] Copy the character string to the clipboard
How to make a Java calendar Summary
A memorandum on how to use Eclipse
How to redo a deployment on Heroku
[Rails] How to convert from erb to haml
[Android] How to deal with dark themes
[Basic] How to write a Dockerfile Self-learning ②