imageview resource as string imagename from drawable folder

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

imageview resource as string imagename from drawable folder

Postby faisalloe » Tue Dec 01, 2009 5:35 am

hello,

I am having lots of images in drawable folder.

I want to set imageview's imageView.setimage* by providing image name as a argument instead of providing reference of R.drawable.imageName
faisalloe
Developer
Developer
 
Posts: 29
Joined: Wed Nov 04, 2009 5:01 am

Top

Postby XCaffeinated » Wed Dec 02, 2009 2:39 am

Hi Faisalloe,

There's no way to pass a string directly to .setImage() , but if you want you can load all your strings into a hashmap like this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. HashMap<String, Integer> m = new HashMap<String, Integer>();
  2.  
  3. m.put("my_pic1.png", R.drawable.my_pic1);
  4.  
  5. m.put("my_pic2.png", R.drawable.my_pic2);
  6.  
  7. //...
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


then retrieve the appropriate resource id when you need it, for example:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. int resid = m.get("my_pic2.png");
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


and use it as .setImageResource(resid).

This will work in a pinch, but it won't be very fast and you will clutter up your code with a lot of string data. What is it you are trying to accomplish?

XCaf
XCaffeinated
Developer
Developer
 
Posts: 25
Joined: Sun Nov 29, 2009 10:16 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 8 guests