Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3854960
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T17:42:09+00:00 2026-05-19T17:42:09+00:00

I am trying to make a little application that simulates the dice. I want

  • 0

I am trying to make a little application that simulates the dice. I want the picture of the dice side randomly change 6 times after clicking the button. It should randomly change 6 times with 0,3 second delay after every random change. The problem is that it changes always only one time not six times as wished. I guess it will be just some trivial mistake but I could not find the right answer anywhere on the web. Here is the code and I thank u all in advance:

   import java.util.Random;

import android.app.Activity;
import android.os.*;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class OneDice extends Activity{

    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.onedicelayout);


        final ImageView image = (ImageView) findViewById(R.id.OneDiceRollImage);
        final int[] arrayOfDices = {
                R.drawable.icon,
                R.drawable.icon2,
                R.drawable.icon3,
        };
        final Random rand = new Random();

        View.OnClickListener rollOneDiceListener = new View.OnClickListener() {

            @Override
            public void onClick(View v){
                int j = 0;
                for(int i = 0; i<6; i++){
                    try {
                        j = rand.nextInt(3);
                        image.setImageResource(arrayOfDices[j]);
                        Thread.sleep(300);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };

        Button oneDiceRollButton = (Button) findViewById(R.id.OneDiceRollButton);
        oneDiceRollButton.setOnClickListener(rollOneDiceListener);

    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T17:42:10+00:00Added an answer on May 19, 2026 at 5:42 pm

    The onClick method that circles through your dice images is a callback from the system.
    Meaning that the UI thread which calls the callback will only operate again when the method returns. And the system is not recording what your are doing internally, but just taking the last state (i.e. the last selected image).

    You can solve this with an AsyncTask where you have doInBackground() (pseudo code):

    for (int i = 0; i< 6 ; i++) {
       publishProgress(randomNumber);
       Thread.sleep(300);
    }
    

    And within onProgressUpdate() you can then display the image

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make a little console application that is able to deal with
Okay so I'm trying to make a little gag program that will run away
After trying to make access to a storage class a little easier, I ended
I have been trying to make this to be a little jQuery plugin that
I'm trying to make a basic application that displays an image from the camera,
I'm trying to make a little side-bar menu so people can access area of
I am trying to make an application for android that is based on IPAD
I've decided to try to make a little Android application that streams live audio
I am trying to make a stop watch application that when you press start
I'm trying to make a little web layout. What I have so far is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.