1

Could someone please help me out with a code EXAMPLE to randomly display a picture from the my drawings folder? i am new developer so i have no idea how to do it. Thanks!

My Requirement is: Display Random image (image should change on each start up)

mali
  • 21
  • 5
  • 3
    That isn't how SO works. First you can google for some ideas, try them out and if it still doesn't work come back and show us what you have done. – The_Cthulhu_Kid Mar 28 '14 at 08:10

3 Answers3

2

Considering you have 10 images in drawable with name format as your_image_1, your_image_2, .... up to your_image_10 you can use the code below for a random image setting to ImageView on start of application each time

public void onCreate(Bundle instance){
//....
     Random r = new Random();
     int randomNumber = r.nextInt(10 - 1) + 1;

     ImageView image = (ImageView) findViewById(R.id.image);
     String imageName = "your_image_" + randomNumber;
     image_ID = getResources().getIdentifier(imageName, "drawable", getPackageName());

     image.setBackgroundResource(image_ID);
}
Saqib
  • 1,120
  • 5
  • 22
  • 40
1

Put some images named img_0 to img_n in your res/drawable folder

Layout (res/layout/rnd_images.xml):

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    >
    <ImageView
        android:id="@+id/imgRandom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
    />
</RelativeLayout>

Code:

package com.example.app;

import java.util.Random;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;

public class MainActivity
extends Activity
{
    final Random rnd = new Random();

    @Override
    protected void onCreate(
        final Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.rnd_images);

        final ImageView img = (ImageView) findViewById(R.id.imgRandom);
        // I have 3 images named img_0 to img_2, so...
        final String str = "img_" + rnd.nextInt(2);
        img.setImageDrawable
        (
            getResources().getDrawable(getResourceID(str, "drawable",
                getApplicationContext()))
            );
    }

    protected final static int getResourceID
    (final String resName, final String resType, final Context ctx)
    {
        final int ResourceID =
            ctx.getResources().getIdentifier(resName, resType,
                ctx.getApplicationInfo().packageName);
        if (ResourceID == 0)
        {
            throw new IllegalArgumentException
            (
                "No resource string found with name " + resName
                );
        }
        else
        {
            return ResourceID;
        }
    }
}

Note that you have to set rnd.nextInt(2) to rnd.nextInt(Max - 1), since rnd starts from 0

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
0

You can show a random image on start up as @Saqib has mentioned, or you can cycle through the images ie in first launch you can show the first image and in the second the second one and then repeat the cycle. For this all you need to do is store an integer in preference and increment the value of the integer every time the app is launched and store the updated value in preference

pvn
  • 2,016
  • 19
  • 33