0

Note I'm referencing to some comments in the description of my problem but the comments are outside the code container, you have to scroll right to see the comments.

So, I'm trying to get a drawable (image) from R.drawable but has bumped into some issues. I started with attempt 1 (check code comment) and got an image with all bounds=0. But then i read here that getDrawable() might return a false scaling of the image so i followed the notice and tried attempt 2 (check code comment again) but in this case the drawable was null.

Here's the code:

private void setCurrentImage(){
    TextView text = (TextView)profileView.findViewById(R.id.profile_image);
    //mImage = "flower";
    int id = R.drawable.flower;
    if(mImage.equals("flower"))
        id = R.drawable.flower;
    if(mImage.equals("event_splash"))
        id = R.drawable.event_splash;
    if(mImage.equals("football"))
        id = R.drawable.football;
    if(mImage.equals("foxhead"))
        id = R.drawable.foxhead;
    if(mImage.equals("computer"))
        id = R.drawable.computer;
    //Drawable image = getResources().getDrawable(R.drawable.foxhead);          //attempt 1
    TypedArray attr = getActivity().obtainStyledAttributes(new int[]{id});      //attempt 2
    Drawable image = attr.getDrawable(0);                                       //attempt 2
    Log.d("bounds", image.getBounds().toString());
    text.setCompoundDrawables(image, null, null, null);
}

As you can see on the commented line mImage = "flower" I tried with 100% certainty the mImage was a valid, still didn't work.

SiXoS
  • 535
  • 3
  • 14
  • what is your image file ? foxhead for example, what is it's extention ? jpg ? png ? and in any folder is it ? drawable-hdpi ? nodpi ? ... – Tourki Oct 03 '13 at 16:29
  • it's diffrent file extensions on diffrent images (probably not optimal), jpg and png, i have tried all, they are directly placed in drawable-xhdpi although there is no diffrence when putting them in drawable. The images work when they are referenced in the xml – SiXoS Oct 03 '13 at 16:35
  • so you have a "foxhead.jpg" under your drawable-xhdpi/ folder – Tourki Oct 03 '13 at 16:39
  • no, foxhead.png :P and as i said, they work in xml using @drawable/foxhead. I also use eclipse which starts nagging if the ID:s is not valid or doesn't exist – SiXoS Oct 03 '13 at 16:42
  • i think you have no problem, look at this : http://stackoverflow.com/questions/10302163/about-android-drawable-getbounds-return-rect0-0-0-0 – Tourki Oct 03 '13 at 16:46
  • ok, that explains the bounds but it still doesn't explain why i'm not seeing any image – SiXoS Oct 03 '13 at 16:49

1 Answers1

0

from the TextView doc :

public void setCompoundDrawables (Drawable left, Drawable top, Drawable right, Drawable bottom)

Sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text. Use null if you do not want a Drawable there. The Drawables must already have had setBounds(Rect) called.

So once you pulled your drawable, you have to call setBounds upon it to specify the bounds where you want your image to appear

Tourki
  • 1,785
  • 15
  • 22