0

So, I am making a game, but I am pretty much a beginner in Android Studio. I've tried to search for this, but didn't find anything I really wanted.

So, I researched on how to make an image disappear when tapped, and didn't really find how to do so, therefore that's the first thing I want to happen.

So, once that's done, I want another image to appear behind it, but the image should be randomly chosen from a group of 5 images.

That's it, I apologise for not having any code tried out or anything, as like I said I'm still a beginner. Please help me soon.

Thanks in advance.

3 Answers3

1

First take an image array of drawables e.g.

int[] imgArray = {
    R.drawable.img1,
    R.drawable.img2,
    R.drawable.img3,
    R.drawable.img4,
    R.drawable.img5
}

//random number between 1 to 5, in your case max=5, min =1
int randomNum = rand.nextInt((max - min) + 1) + min;

yourImageView.setBackgroundResource(imgArray[randomNum])

If you just want to change the previous image you don't have to dissapear first one. Just set the new image in that ImageView then automatically it will override.

However you can make an ImageView Visible, Invible or Gone by setting the following property

yourImageView.setVisibility(View.INVISIBLE);
yourImageView.setVisibility(View.GONE);
Kunu
  • 5,078
  • 6
  • 33
  • 61
  • for using drawable ids instead of bitmaps up-voted i have – insomniac Jan 02 '16 at 05:24
  • Thanks for the reply, but I have 2 questions regarding this. Firstly, does it disappear on being tapped, because I require it to disappear. Secondly, where do I put the code? Quite a pathetic question, I know. Thanks a lot for the reply! –  Jan 02 '16 at 06:20
  • Which one is your second? – Kunu Jan 02 '16 at 06:22
0

Assuming your 5 images are in drawable folder,

  1. int img_arr[] = {R.id.img1, R.id.img2, ...};

  2. set your image to imageview by using:

    imageview.setOnClickListener(new OnClickListener() {
       public void onClick(View v)
        {
            int a = random.nextInt(5); 
            //fade-in animation code
            imageview.setImageResource(0);
            //fade-out animation code
            imageview.setImageResource(img_array[a]);
    
        }
    

for animation, refer this link: Link

Community
  • 1
  • 1
kiturk3
  • 549
  • 10
  • 30
  • Ok so here's what I get: When I type in "random" it tells me "Cannot resolve symbol 'random' " Then also when I type in "setOnClickListener" and "setImageResource" it tells me "Non-static method '...' cannot be referenced from a static context". Finally, I change "imageview" to "ImageView" because if not it would give me the error "cannot resolve symbol 'imageview'" Please help! –  Jan 03 '16 at 04:45
0

First take an image array of drawables like in Kunu's Answer e.g.

int[] imgArray = {
    R.drawable.img1,
    R.drawable.img2,
    R.drawable.img3,
    R.drawable.img4,
    R.drawable.img5
}

And for your first question,

1.How to make the image disappear when tapped

Consider you have an ImageView called img1 inside a RelativeLayout rl

We'll set an onClickListener to it like

img.setOnClickListener(new View.OnClickListener(View v)
{
v.setVisibility(View.INVISIBLE)
});

2.Make another Image appear behind it

call this function while passing the RelativeLayout like

randomBg(rl);

The randomBg function:

public void randomBg(RelativeLayout rl)
    {
    //random number between 1 to array limit
   /* int randomNum = rand.nextInt((minRange - maxRange) + 1) + minRange;
      Can be simplified as below*/

    int randomNum =rand.nextInt((0-imgArray.length)+1)+0;    
    rl.setBackgroundResource(imgArray[randomNum])
    }

To sum it up use the above code in an Activity like this

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.mobilityeye.knowyourcustomer.R;

import java.util.Random;

/**
 * Created by PS on 1/4/2016.
 */
public class MyActivity extends Activity {
    RelativeLayout rl;
    ImageView imgView;
    int[] imgArray = {
            R.drawable.img1,
            R.drawable.img2,
            R.drawable.img3,
            R.drawable.img4,
            R.drawable.img5
    };
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_layout);
        rl=(RelativeLayout) findViewById(R.id.my_relative_layout);
        imgView=(ImageView) findViewById(R.id.my_image_view);
        imgView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                v.setVisibility(View.INVISIBLE);
                //If you want to set Image at relativelayout
                randomBg(rl);
            }
        });
    }
    public void randomBg(RelativeLayout rl)
    {
        //random number between 1 to array limit
       /* int randomNum = rand.nextInt((minRange - maxRange) + 1) + minRange;
          Can be simplified as below*/
        Random rand = new Random();
        int randomNum =rand.nextInt((0-imgArray.length)+1)+0;
        rl.setBackgroundResource(imgArray[randomNum])
    }


}
insomniac
  • 11,146
  • 6
  • 44
  • 55
  • Ok so everything is going good. I searched for a tutorial on how to use OnClickListeners and I found out that I have to type lines of code like "img = (ImageView) findViewById(R.id.img);". So I did that for the Relative Layout but I keep getting an error: "Incompatible types: Required: android.widget.LinearLayout. Found: android.widget.RelativeLayout". Also, how do I call the function randomBg(rl);? Where do I put it? Thanks! –  Jan 04 '16 at 03:26
  • Oh yea one more question, under the commented line: //random number between 1 to array limit, my array limit is 5 as I have 5 images, so how do I do that? –  Jan 04 '16 at 03:27
  • Check if you have declared the variable rl as LinearLayout somewhere or maybe in the XML file,And for the second question You can put it somewhere inside the activity class and pass the relative layout object through the fn when you want the background to be changed – insomniac Jan 04 '16 at 05:13
  • You don't have to do anything about the array length as the function decides the start and end limit in the code – insomniac Jan 04 '16 at 05:14
  • that is start is 1 and end is length or the array,technically the start should be 0 since array indexing starts from 0,I'll edit my answer – insomniac Jan 04 '16 at 05:16
  • Thanks, I realized that I had declared it as LinearLayout, so my mistake. However, I still don't understand where to put 'randomBg(rl);'. It would be great if you gave me an example or something. –  Jan 04 '16 at 09:45
  • One more thing, if you would email me as I have a little more questions one this is done, that would be great! –  Jan 04 '16 at 10:10
  • I hope you don't mind but could you check your inbox please? Sorry if I'm bothering you –  Jan 06 '16 at 07:40