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])
}
}