Zai Khan
Zai Khan

Reputation: 47

Using if condition in Android ImageView

I am developing a game for android in which I will be using random images for it. My problem is I whenever there is a certain image in that image view (For ex. Image1 id generated randomly in the ImageView) I want the TextView to display a message saying, Image1 can be seen in the image view now. I know it can be done using if condition, saying if imageview=imageview1, textview1.settext "blah blah blah".

How can we do that. If my question is not clear, please ask for some more details, REQUEST!!!

package com.random.image;

import java.util.Random;



import android.app.Activity;

import android.app.AlertDialog;

import android.content.DialogInterface;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.ImageView;

import android.widget.TextView;


public class RandomImageActivity extends Activity {

    /** Called when the activity is first created. */





private static final Random rgenerator = new Random();

private ImageView iv;

private static final Integer[] mImageIds = 
    { R.drawable.tailss, R.drawable.headss, };

@Override
public void onCreate(Bundle savedInstanceState)

{
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    final TextView t=(TextView) findViewById(R.id.textView1);

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

    final Button b=(Button) findViewById(R.id.button1);
    b.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            AlertDialog a=new AlertDialog.Builder(RandomImageActivity.this).create();
            a.setTitle("Flip the Coin");
             a.setMessage("Click the button below to find which side of the coin you got");
             a.setButton("Try Your Luck", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int which) {
                  // here you can add functions

                 changeImageResource();





               }
            });
       a.setIcon(R.drawable.coin);
       a.show();


        }
    });
}

public void changeImageResource()
{

    int i = rgenerator.nextInt(2);
    iv.setImageResource(mImageIds[i]);

}
}

Upvotes: 1

Views: 5858

Answers (2)

Ted Hopp
Ted Hopp

Reputation: 234847

I assume that you are randomly selecting an image from a known list of images. You can instead define a data structure that groups an image id with the id of a caption string:

ImageData {
    int imageId;
    int captionId;
}

Then create an array of these objects (instead of an int array of image ids) and when you select one at random, use the imageId field to load the image and the captionId to load the caption string.

EDIT

Your code has this:

private static final Integer[] mImageIds = 
    { R.drawable.tailss, R.drawable.headss, };

I'm suggesting this:

private static final class ImageData {
    ImageData(int imageId, int captionId) {
        this.imageId = imageId;
        this.captionId = captionId;
    }
    int imageId;
    int captionId;
}

private static final ImageData[] mImageData = {
    new ImageData(R.drawable.tailss, R.string.tailss),
    new ImageData(R.drawable.headss, R.string headss)
};

Then, in your code you would do this:

public void changeImageResource()
{
    int i = rgenerator.nextInt(mImageData.length);
    ImageData data = mImageData[i];
    iv.setImageResource(data.imageId);
    t.setText(getString(data.captionId));
}

Upvotes: 2

Jug6ernaut
Jug6ernaut

Reputation: 8325

Just use the setTag and getTag method on the ImageView and do your conditional statment off of that. No need for additional data structures.

Example.

imageView.setImageResource(mImageIds[i]);
imageView.setTag(mImageIds[i]);

then do

if(imageView.getTag().equals(String.valueOf(mImageIds[i]))
{
    textView.setText("blabla");
}

Or even better

imageView.setImageResource(mImageIds[i]);
imageView.setTag(mImageIds[i]);

then

switch(Integer.valueOf(imageView.getTag()){
    case R.drawable.id1: textView.setText("bla1"); break;
    case R.drawable.id2: textView.setText("bla2"); break;
}

Upvotes: 1

Related Questions