I am very new to Eclipse, and have been using it only for a day so far, and also have no previous experience with Java, although I don't know much I still understand how it goes.
Could someone please help me out with a code to randomly generate a picture from my drawings folder?
I got my android application blank and on full screen mode, I have added a picture in imageView1, fitCenter'ed it and it really works good, I cleaned out all the errors that occurred along the way also.
This is how my ImageView code looks so far:
<ImageView
android:id="@+id/imageView1"
android:contentDescription="@string/desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:scaleType="fitCenter"
android:src="@drawable/img1" />
Now what I really need is that my application only shows a random image, and every time I run the app it shows a different image, and also that once in application, I can On Click on the image and then it shows a different random image.
I guess I have to add some strings or something, I'm really new to this and don't know much yet.
Really appreciate if you could help me out here :).
Try this
int[] photos={R.drawable.image1, R.drawable.image2,R.drawable.image3,R.drawable.image4};
ImageView image = (ImageView) findViewById(R.id.imageview1);
Random ran=new Random();
int i=ran.nextInt(photos.length);
image.setImageResource(photos[i]);
image.setOnClickListener(new View.onClickListener()
{
public void onClick(View v)
{
int k=ran.nextInt(photos.length);
image.setImageResource(photos[k]);
}
}
);
Note: I haven't typed this in Eclipse or in any java editor, if you find any syntax error correct it by yourself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With