How To Display Random images on image view

 ̄綄美尐妖づ 提交于 2019-11-28 13:05:37

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

[UPDATE]

The layout name must match that in setContentView.
So, if you have (why?) this:

setContentView(R.layout.activity_main);

in your MainActivity.java/onCreate, then rename the layout "activity_main.xml"

OR, better, USE MY CODE AS IS.

It works without modifications.

[UPDATE]

Check this line:

final Random rnd = new Random();

It requires the following import:

import java.util.Random;

My code works as is. I tested it before giving it to you.
Just place my layout in res/layout, the images in res/drawable and the MainActivity.java to replace the default one.

Please, notice that the images names MUST be "img_#" where # is a number.
This number must be 0 to (max - 1).

Or give names like "my_city_#" or whatever.
But then you must update the java code to match these names.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!