eg8842
eg8842

Reputation: 1

Random Image Generator Android

I am working with Android in Eclipse. I have my random Image generator working. At this moment when the page is loaded there's a random image loaded in ImageView1 automatically before the user clicks the button. (after the user clicks the button another random image is loaded). *When the page is loaded I don't want any images to show on the ImageView1 until the user clicks the button. How may I be able to change my code so that no image is loaded automatically until the user clicks the button?

    package com.senior.callacab2;

    import java.util.Random;
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.widget.ImageView;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;

    public class Generatedrinks extends Activity implements OnClickListener {


private int[] mPhotoIds = new int [] { R.drawable.img1,
        R.drawable.img2, R.drawable.img3, R.drawable.img4, R.drawable.img5,
        R.drawable.img6, R.drawable.img7 };

private static final Random rgenerator = new Random();

private ImageView iv;

// Makes new images appear after every button click
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.generatedrinks);

    Integer q = mPhotoIds[rgenerator.nextInt(mPhotoIds.length)];
    iv = (ImageView) findViewById(R.id.imageView1);
    iv.setImageResource(q);

    View nextButton = findViewById(R.id.button2);
    nextButton.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.button2:

 iv.setImageResource(mPhotoIds[rgenerator.nextInt(mPhotoIds.length)]);
        break;
    }

    // Makes button2 invisible after two clicks the first click generates a
    // random image and the second click makes the button disappear
    Button generator = (Button) findViewById(R.id.button2);
    generator.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Button button = (Button) v;
            button.setVisibility(View.INVISIBLE);
        }
    });
    // When this button is pressed it takes the user to the next layout
    // called Scoreclass.class

    Button next = (Button) findViewById(R.id.button1);
    next.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Intent myIntent = new Intent(view.getContext(), Scorecard.class);
            startActivityForResult(myIntent, 0);
        }

    });

}

    }

Upvotes: 0

Views: 3457

Answers (1)

kabuko
kabuko

Reputation: 36302

Well, if you don't want the activity to load the image before you click, then don't load the image when you start the activity...

You have this code in your onCreate:

Integer q = mPhotoIds[rgenerator.nextInt(mPhotoIds.length)];
iv = (ImageView) findViewById(R.id.imageView1);
iv.setImageResource(q);

Remove the first and third lines (the second one is still useful to find the view for later use) and you won't load an image when your activity starts.

Upvotes: 1

Related Questions