android-using Getidentifier () to get the resource ID

Source: Internet
Author: User

Use Getidentifier () to get the resource ID

int i=  getresources (). Getidentifier ("icon", "drawable", Getpackagename ()); if (i>0)        {log.i ("AA", "AA");} Else         {log.i ("VBV", "AA");}

Or

int ResID = getresources (). Getidentifier ("org.loveandroid.androidtest:drawable/gallery_photo_1",null ,nullintnullnull//int ResID = Getresources (). Getidentifier ("Bug", "drawable", "Org.anddev.android.testproject"// The first parameter: full_package:type/filename_without_ending is this format and the other can be null

int NULL NULL );      // or      int idflag = getresources (). Getidentifier ("Flag", "drawable", Getpackagename ());    

New Drawable[10];           for (int i = 1; I <=; i++) {       int id = getresources (). Getidentifier ("flag" + I,  "Drawa Ble ", Getpackagename ());       Dw[i-1] = getresources (). getdrawable (ID);}  

//All resources can be obtained by reflection method.Private void_dumpallresourceids (Class<?>ClassType)throwsillegalargumentexception {field[] fIDs=Classtype.getfields (); Try {          for(inti = 0; i < fids.length; i++) {Field fld=Fids[i]; intNID = Fld.getint (NULL); LOG.D ("DBG", Classtype.getsimplename ()+ " " +I+ ": " +Fld.getname ()+ "=" +NID); }       } Catch(Exception e) {Throw Newillegalargumentexception (); }     }   

Import Java.lang.reflect.Field;     ...       _dumpallresourceids (r.layout. class );       _dumpallresourceids (r.drawable. class);   

// Results R$layout 0:main=2130903040    1:small_spinner_dropdown_item=2130903041    0:icon= 2130837504  

Sometimes we need to dynamically get the ID of a control, and then do the operation, after searching the web, find a way

Getresources (). Getidentifier ("textView01", "id", "cn.xxx.xxx");

The first parameter is the ID name, the second is the resource attribute is the ID or the drawable, and the third is the package name.

A problem encountered during the project, read the image name from the database, and then call the picture. Directly with R.drawable.? cannot be called. Check a lot of places finally found a way to share to everyone, hope to help.
Mainly by two methods, individuals suggest the second kind.
1. Instead of placing the image under Res/drawable, it is stored in a SRC package (for example: Com.drawable.resource), in which case the call method is:
String Path = "Com/drawable/resource/imagename.png";
InputStream is = getClassLoader (). getResourceAsStream (path);
Drawable.createfromstream (IS, "src");

2. If you still want to use the image in res/drawable directly, you need to go through the following method:
Assuming that the project was created, the completed package name is: Com.test.image
int resID = Getresources (). Getidentifier ("ImageName", "drawable", "com.test.image");
drawable image = Getresources (). getdrawable (ResID);

This article transferred from: http://wen66.iteye.com/blog/1224201

android-using Getidentifier () to get the resource ID

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.