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

I am making an app and am wanting to assign an image to a text view please help.

I am using the FunFacts app as a template and i have gotten images onto the screen but I want a way to assign certain images to certain facts.

2 Answers

Hello Dalton Coble,

Now, as you can see from my username, I am no Ben Jakuben. But I think I might have an answer for you. First, it would be much faster to reply if you were to give us the code, but that is okay. Second, maybe you can use (if/else if/ else) conditionals to set photos for every fun fact, and, good idea. So, I will only use three fun facts "President Obama is the 44th United States President" and "Russia is the the most populated country" and "The Washington Monument was a site of a hostage incident", (Yes, search it up, or search someone named Norman Mayer, age 66). And I am going to use corresponding pictures. Lets Start! :-)

String[] facts = {
              "President Obama is the 44th United States President",
              "Russia is the most populated country",
              "The Washington Monument was a site of a hostage incident"
}

Random randomGenerator = new randomGenerator Random()
int randomNumber = randomGenerator.nextInt(facts.length)

fact = facts[randomNumber]
<your textView>.setText(fact)

//Now this is where the Images come through

(ImageView) <your imageView> = (ImageView) findViewById(R.id.<your imageView)

//Make sure you have your three photos in your drawables folder

if (randomNumber == 0) {
android:src="@drawable/obama"

//And so on

I am using the FunFacts app as a Template so were would this go in the code?

I get what it is doing on here but when I put it in my code I got errors. you can get the same code I am using from the android course how to build a simple android app and use that to show me if you want. I get errors when I put in your code.

Ben Jakuben if you could help it would be much appriciated