Corina Racasan
Corina Racasan

Reputation: 53

I want to make a splash screen made with multiple images

I want to make a random splash screen, so that everytime I open the app another image will be loaded.

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.splash_screen);
    private int[] splashImages = {R.drawable.splash1, R.drawable.splash2, R.drawable.splash3};
    Random random = new Random(System.currentTimeMillis());
    int postOfImage = random.nextInt(splashImages.length -1);

Could someone please tell me how to do it?

Upvotes: 0

Views: 2720

Answers (1)

Pratik Dasa
Pratik Dasa

Reputation: 7439

public class MainActivity extends Activity {
    private static int[] splashImages = { R.drawable.splash1, R.drawable.splash2, R.drawable.splash3 };
    ImageView imgSplash;
    private SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

        imgSplash = (ImageView) findViewById(R.id.splash);
        int selected;
        selected = randomBox();
        if (selected == sharedPreferences.getInt("SELECTED", 0)) {
            selected = randomBox();
        }

        Editor editor = sharedPreferences.edit();
        editor.putInt("SELECTED", selected);
        editor.commit();

        for (int i = 0; i < splashImages.length; i++) {
            if (selected == i) {
                imgSplash.setImageResource(splashImages[i]);
            }
        }

    }

    public static int randomBox() {

        Random rand = new Random();
        int pickedNumber = rand.nextInt(splashImages.length);
        return pickedNumber;

    }

}

Upvotes: 1

Related Questions