Q:

Moving the array/string in reverse

I have few images and two buttons. I am changing my image views with the help of two buttons, Next and Back. My next button is working fine it changes the images in the forward arrangement, whereas I want my back button to take the arrangement in the reverse direction. It works for a while but when it reaches the first image in the array the app crashes. Here is my code:
    private int[] drawables = { R.drawable.bg, R.drawable.green,
            R.drawable.blue };
    private int clicks = 0;
    private int clicks1 = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.learn_main);

        Button btnNext = (Button) findViewById(R.id.button2);
        Button btnBack = (Button) findViewById(R.id.button1);

        final ImageView img = (ImageView) findViewById(R.id.imageView1);

        // INITIALIZING OVER

        btnNext.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (clicks < drawables.length - 1) {
                    clicks++;

                } else {

                    clicks = 0;
                }
                img.setImageResource(drawables[clicks]);
            }

        });

        btnBack.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (clicks1 < drawables.length + 1) {
                    clicks1++;

                } else {

                    clicks1 = 0;
                }
                img.setImageResource(drawables[clicks1]);
            }

        });

    }
}

logcat
04-28 09:56:31.782: E/AndroidRuntime(2563): FATAL EXCEPTION: main
04-28 09:56:31.782: E/AndroidRuntime(2563): java.lang.ArrayIndexOutOfBoundsException
04-28 09:56:31.782: E/AndroidRuntime(2563):     at com.example.duh.Learn$2.onClick(Learn.java:60)
04-28 09:56:31.782: E/AndroidRuntime(2563):     at android.view.View.performClick(View.java:2533)
04-28 09:56:31.782: E/AndroidRuntime(2563):     at android.view.View$PerformClick.run(View.java:9320)
04-28 09:56:31.782: E/AndroidRuntime(2563):     at android.os.Handler.handleCallback(Handler.java:587)
04-28 09:56:31.782: E/AndroidRuntime(2563):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-28 09:56:31.782: E/AndroidRuntime(2563):     at android.os.Looper.loop(Looper.java:150)
04-28 09:56:31.782: E/AndroidRuntime(2563):     at android.app.ActivityThread.main(ActivityThread.java:4389)
04-28 09:56:31.782: E/AndroidRuntime(2563):     at java.lang.reflect.Method.invokeNative(Native Method)
04-28 09:56:31.782: E/AndroidRuntime(2563):     at java.lang.reflect.Method.invoke(Method.java:507)
04-28 09:56:31.782: E/AndroidRuntime(2563):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
04-28 09:56:31.782: E/AndroidRuntime(2563):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
04-28 09:56:31.782: E/AndroidRuntime(2563):     at dalvik.system.NativeStart.main(Native Method)

I tried my best to explain my problem in detail, if I didn't do it correctly do let me know I will edit my question for that matter.

A:

here is the solution to your problem
declare variable int index = 0; then inside oncreate write code below and you are done
    img.setImageResource(drawables[index]);
    btnNext.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            if (index < drawables.length-1) {
                index++;

            } else {

                index = 0;
            }
            img.setImageResource(drawables[index]);
        }

    });

    btnBack.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            if (index > 0) {
               index--;

            } else {

                index = drawables.length-1;
            }
            img.setImageResource(drawables[index]);
        }

    });

