1

I'm passing a string value, which is also the name of an image in the drawable folder using intent.putExtra. The string is then successfully pulled from the onCreate of the intended class.

But I'm wondering how I can set the passed over string message as the image name when setting the Image Resource of the image view for that activity.

I'm guessing that the message variable needs to be cast or converted before I use it to set as an image resource name.

Does anyone have any advice on how to achieve this?

This is what I've tried so far in said activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search_result);

    imageView = (ImageView) findViewById(R.id.capturedDebriImageView);


    Bundle bundle = getIntent().getExtras();
    //contents of message is "aluminium"
    String message = bundle.getString("message");


    imageView.setImageResource(R.drawable.message);

   //if I set it as the actual image name it works but not when
   //I set the image resource to the message which contains the string of the 
   //same name.
   imageView.setImageResource(R.drawable.aluminium);


}

1 Answer 1

2

try this:

int id = getResources().getIdentifier(message, "drawable", this.getPackageName());
if(id != 0){
   imageView.setImageResource(id);
}else{
   imageView.setImageResource(R.drawable.aluminium);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.