Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Building a simple android app >learning the language> extra credit I want diff random background images for each answer

Building a simple android app >learning the language> extra credit Can someone please help me... I want different random background images like 9 0r 10, so when i tap ENLIGHTEN ME... APPEARS A DIFFERENT ANSWER AND A DIFERENT BACKGROUND IMAGE... How i can do that.. please help I'll be very grateful

2 Answers

Thomas Anthony
seal-mask
.a{fill-rule:evenodd;}techdegree
Thomas Anthony
iOS Development Techdegree Student 22,352 Points

There are several approaches you can take to achieve the desired result. I recommend storing your images in the respective drawable resource folder(s). Then, create a reference to the imageView within the void onCreate(View) method. To change the background image with every random prediction, in your void onCreate(View) method, use a series of if or a switch statement to determine the value of the String answer variable by comparing it to the desired element within the set of answers declared in your CrystalBall class. Finally, set the image resource of the imageView to the appropriate drawable resource. Your code may look something like this:

public void onCreate(View arg0)
{
  String answer = mCrystalBall.getAnAnswer();
  // Reference the existing ImageView within you layout.
  ImageView imageView = (ImageView)findViewById(R.id.imageView1);

  switch(true) {
    // Check if the random answer return by the `getAnAnswer()` method is equal to the desired answer.
    case answer.equals(mCrystalBall.mAnswer[0]):
      // Set the desired drawable (image) as the content of the `imageView`
      imageView.setImageResource(R.drawable.someImageName);
      break;
     
    default:
       // Declare any fall-through cases
       break;
  }

  answerLabel.setText(answer);
}

thanks for the help! u r very kind