Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Android and the fly likes I can't  show random images in Android Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Android
Bookmark "I can Watch "I can New topic

I can't show random images in Android

D Hagy

Joined: Jul 05, 2011
Posts: 12
Nice day!
I tried to show random images in my application in an ImageView. I
tried this code:
public class rr extends Activity{
        ImageView secret;
        protected void onCreate(Bundle savedInstanceState) {
                // TODO Auto-generated method stub
                TextView txt = (TextView) findViewById(;
                secret = (ImageView) findViewById(;
                Runnable r1 = new Runnable() {
                        public void run() {
                                // TODO Auto-generated method stub
                                int imageArr[] = new int[3];
                                imageArr[0] = R.drawable.r1_mirror;
                                imageArr[1] = R.drawable.s1_mirror;
                                imageArr[2] = R.drawable.p2_mirror;
                                int n = (int) Math.random()*3;
                secret.postDelayed(r1, 5000);

But it only show the r1_mirror image, everytime, when I start my
application in the emulator. What I made bad? I thought that it will
working, but.
Thanks for any helps

Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42959
You seem to assume that "(int) Math.random()*3" is the same as "(int) (Math.random()*3)" - have you verified that assumption?
I agree. Here's the link:
subject: I can't show random images in Android
It's not a secret anymore!