A.Schtolc A.Schtolc - 4 months ago 11
Java Question

How to get new image (from array) after start activity?

I have button which starts the same activity. How can I get next image from array, after start activity? Here is code of button

Button btnNext = (Button) dialog.findViewById(R.id.btnNext);
btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
finish();
startActivity(getIntent());
}

});


Here is array

int[] array_images = {
R.drawable.apple,
R.drawable.p_dolor
};

Neo Neo
Answer

Why you exactly want to restart activity every-time, You can just change your resource on button click without Restarting the activity. For example -

int index = 0;
int[] imgRes = {R.id.image1, R.id.image2, R.id.image3, R.id.image4};
    Button btnNext = (Button) dialog.findViewById(R.id.btnNext);
btnNext.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                    updateImage();
                }

            });
private void updateImage(){
    index++;
    if(index >= imgRes.lenght)
        index = 0;
    ImageView imageView = findViewById(R.id.myImage);
    imageView.setResource(imgRes[index])
}

Hope it will help :)

Comments