I build a android application in which i want display image one by one after a random time. means
image 1 show
after 10 sec
image 2 show
after 30 sec
image 3 show
after 50 sec
image 4 show
i have code for displaying image but it shows images continuously every 10 sec while i want image show after a random time.
public class MainActivity extends Activity {
ImageView imageView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView1);
final int []imageArray=
{R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e};
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
int i=0;
public void run() {
imageView.setImageResource(imageArray[i]);
i++;
if(i>imageArray.length-1)
{
i=0;
}
handler.postDelayed(this, 10000); //for interval...
}
};
handler.postDelayed(runnable, 10000); //for initial delay..
}
xml file is-
<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"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>