title: How To Display Random images on image view
body: <p>Could someone please help me out with a code to randomly display a picture from the my drawings folder? i am new to java so i have no idea how to do it.
  Any help will be appreciated. 
  My Requirements are:-
  1.Display Random image (image should change on each start up)
  2.that's all</p>
  
  <pre><code>import java.util.Random;
  import java.util.Random;
  import android.app.Activity;
  import android.content.Context;
  import android.content.Intent;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.ImageView;
  
  public class MainMenu extends Activity {
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main_menu);
  }
  
  public void openNewActivity(View view) { 
  Intent intent = new Intent(this,MainActivity.class);
  startActivity(intent);//button to open main
  }
  public void browser(View view) { 
      Intent intent = new Intent(this,Browser.class);
      startActivity(intent);//button to open browser
  }
  
  
  final Random rnd = new Random();
  
  {
  
      setContentView(R.layout.activity_main_menu);
  
      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(9);
      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;
      }
  
  
  }
  }
  </code></pre>