0

i have a problem with displaying an image on imageButton...

imgWeatherIcon = (ImageButton) findViewById(R.id.mndpImage);

Intent i = getIntent();
            this.iconfile = i.getStringExtra("icon");

            String uri = "drawable/"+ "d" + iconfile;
            System.out.println("* "+ uri);
            int imageBtnResource = getResources().getIdentifier(uri, null, getPackageName());
            Drawable dimgbutton = getResources().getDrawable(imageBtnResource);

            imgWeatherIcon.setImageDrawable(dimgbutton);

some System.out.println outputs:

        uri = drawable/dsnowing
        imageBtnResource  = 2130837512
        dimgbutton = android.graphics.drawable.BitmapDrawable@414733c8

the image exist in the drawable folder name of the imageButton element (mndpImage) in layout is correct

any idea how to solve this problem ?

LitBe
  • 183
  • 2
  • 11
  • I'm confused why you're using the path to the drawable folder and not R.drawable.imageName – Scott Helme Aug 12 '13 at 12:17
  • I'm new at android programing... i found this piece of code and now i'm trying to test it... so if you know the right way to do this ... please :) – LitBe Aug 12 '13 at 12:31
  • If you have items in the drawable folder, instead of using the path to the image you can just use R.drawable.imageName so for an image called dsnowing it would be R.drawable.dsnowing If you have multiple sizes/densities of the images in ldpi/mdpi etc.. it will auto select the correct one based on the device hardware too. – Scott Helme Aug 12 '13 at 12:34
  • imgWeatherIcon.setImageDrawable(R.drawable.dsnowing); – Scott Helme Aug 12 '13 at 12:35

1 Answers1

0

Does this work?

imgWeatherIcon = (ImageButton) findViewById(R.id.mndpImage);
imgWeatherIcon.setBackgroundResource(R.drawable.dsnowing);
Scott Helme
  • 4,786
  • 2
  • 23
  • 35
  • The method setImageDrawable(Drawable) in the type ImageView is not applicable for the arguments (int) – LitBe Aug 12 '13 at 12:42
  • Apologies, try setBackgroundResource(R.drawable.imageName); – Scott Helme Aug 12 '13 at 12:44
  • better... but still not good... the image apears behind the "sample image" that is used for the imageviews without resource ... if you understand what i mean ... here is the result http://shrani.si/f/M/gV/3ny24MGb/problem.png – LitBe Aug 12 '13 at 12:57
  • That image must be assigned somewhere, either in code or xml. Find the reference to the image name and remove it. – Scott Helme Aug 12 '13 at 12:58
  • GREAT JOB! it was inside the XML file .... now i have to figure out how to insert the variable instead of the "dsnowing"... couse i call the images from XML file... – LitBe Aug 12 '13 at 13:06
  • String test= iconfile; imgWeatherIcon.setBackgroundResource(R.drawable.test); doesn't work ... any idea ? – LitBe Aug 12 '13 at 13:17
  • Unless there is a specific reason you need to get the ID of the drawable from a string in the XML file I would avoid it and just use R.drawable.imageName instead. – Scott Helme Aug 12 '13 at 13:25
  • this activity is an extension of the listview ... when i click on a element of the listview the data of that element are send in this activity ... so get the title and the description + the image ... this is why i need to call it trough the variable .... the iconfile is "dsnowing" so i just need to save it as a variable and run it in this setBackgroundResource .... – LitBe Aug 12 '13 at 13:31
  • int resID = getResources().getIdentifier(iconfile, "drawable", getPackageName()); imgWeatherIcon.setBackgroundResource(resID); – Scott Helme Aug 12 '13 at 13:36