The ID of the dynamic get control under Android

Source: Internet
Author: User

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 .

The following information is available from the Web:
Mainly consists of two methods, the individual suggests the second.  
1. Instead of placing the picture 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/image.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 , fill in the package name: com.test.image 
int resID = Getresources (). Getidentifier ("Goto_radar", " Drawable "," com.test.image ");  
drawable image = Getresources (). getdrawable (ResID);

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.