Balaji Balaji - 2 months ago 6
Android Question

load images and clicking on previous button doesnot load the image in android

Here is my code

private int[] imageArray;
private int currentIndex;
private int startIndex;
private int endIndex;
Button nxt,prv;


in OnCreate method

prv = (Button)findViewById(R.id.zoomIn);
nxt = (Button)findViewById(R.id.zoomOut);
imageArray = new int[4];
imageArray[0] = R.drawable.ic_launcher;
imageArray[1] = R.drawable.abc_ab_share_pack_mtrl_alpha;
imageArray[2] = R.drawable.abc_btn_check_to_on_mtrl_000;
imageArray[3] = R.drawable.abc_btn_rating_star_on_mtrl_alpha;

startIndex = 0;
endIndex = 3;

nxt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
nextImage();
}
});
prv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
previousImage();
}
});


Methods

public void nextImage() {
System.out.println(" currentIndex nextImage" + currentIndex);
if(currentIndex != -1 && currentIndex <= endIndex ) {
ivImage.setImageResource(imageArray[currentIndex]);
currentIndex++;
}
else if(currentIndex == -1){
currentIndex++;
}

new Runnable() {
@Override
public void run() {
if (currentIndex > endIndex) {
currentIndex--;
previousImage();
} else {
nextImage();
}

}
}; // here 1000(1 second) interval to change from current to next image

}

public void previousImage() {
System.out.println(" currentIndex previousImage" + currentIndex);
if(currentIndex != -1 && currentIndex <= endIndex) {
ivImage.setImageResource(imageArray[currentIndex]);
currentIndex--;
}
else if(currentIndex > endIndex){
currentIndex--;
}

new Runnable() {
@Override
public void run() {
if (currentIndex < startIndex) {
currentIndex++;
nextImage();
} else {
previousImage(); // here 1000(1 second) interval to change from current to previous image
}
}
};
}


When the currentIndex = 4 after 2 clicks it loads previous or next images.
I need it work with one click it should move to previous or next images.
What i am missing in my logic. Can anyone help. Thanks in advance.

Answer

Full working demo with your code

public class MainActivity extends AppCompatActivity {

    private Context context;
    private int[] imageArray;
    private int currentIndex = 0;
    Button nxt, prv;
    ImageView image;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        image = (ImageView) findViewById(R.id.imageView);
        prv = (Button) findViewById(R.id.previous);
        nxt = (Button) findViewById(R.id.next);

        imageArray = new int[4];
        imageArray[0] = R.mipmap.ic_launcher;
        imageArray[1] = R.drawable.abc_ab_share_pack_mtrl_alpha;
        imageArray[2] = R.drawable.abc_btn_check_to_on_mtrl_000;
        imageArray[3] = R.drawable.abc_btn_rating_star_on_mtrl_alpha;

        nxt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentIndex++;
                nextImage();
            }
        });
        prv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentIndex--;
                previousImage();
            }
        });

    }

    private void nextImage() {
        System.out.println(" currentIndex previousImage" + currentIndex);
        if (currentIndex != -1) {
            if (currentIndex < imageArray.length) {
                image.setImageResource(imageArray[currentIndex]);
            } else {
                currentIndex = 0;
                image.setImageResource(imageArray[currentIndex]);
            }
        } else {
            currentIndex = imageArray.length;
            image.setImageResource(imageArray[currentIndex]);
        }
    }

    private void previousImage() {
        System.out.println(" currentIndex previousImage" + currentIndex);
        if (currentIndex != -1) {
            if (currentIndex < imageArray.length) {
                image.setImageResource(imageArray[currentIndex]);
            } else {
                currentIndex = 0;
                image.setImageResource(imageArray[currentIndex]);
            }
        } else {
            currentIndex = imageArray.length-1;
            image.setImageResource(imageArray[currentIndex]);
        }
    }
}

activity_main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/base"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="horizontal">


    <Button
        android:id="@+id/next"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_margin="10dp"
        android:text="Next"
        android:textSize="20dp" />

    <Button
        android:id="@+id/previous"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignEnd="@+id/next"
        android:layout_alignStart="@+id/next"
        android:layout_below="@+id/next"
        android:text="Previous"
        android:textSize="20dp" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/previous" />

</RelativeLayout>
Comments