title: Changing Images continously with button click Android
body: <p>I hereby thanks to you all who helps me out. </p>
  
  <p>I am trying to make an app which has 10 pictures in it, 
  only one imageview on the main layout and one button.</p>
  
  <p>when i pressed the button, image changes to next one. when i press the button again, it changes to other one.</p>
  
  <p>I couldnt do it for some days. I am trying this;</p>
  
  <p>Main.java;</p>
  
  <pre><code>package com.example.denemeemre;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.ImageView;
  
  
  public class Main extends Activity {
  private ImageView hImageViewSemafor;
  public Button hButton;
  
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
  
          hImageViewSemafor = (ImageView)findViewById(R.id.idImageViewSemafor);
          hButton = (Button) findViewById(R.id.idBtnChangeImage);
          hButton.setOnClickListener(aButtonChangeImageListener);
  
      }
          View.OnClickListener aButtonChangeImageListener = new OnClickListener() {
  
          public void onClick(View v) {
                  // setImageResource will change image in ImageView
              hImageViewSemafor.setImageResource(R.drawable.r1);
  
          }
      };
  
      public void onCreate1(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
  
          hImageViewSemafor = (ImageView)findViewById(R.id.idImageViewSemafor);
          hButton = (Button) findViewById(R.id.idBtnChangeImage);
          hButton.setOnClickListener(bButtonChangeImageListener);
  
  
      }
  
      View.OnClickListener bButtonChangeImageListener = new OnClickListener() {
          public void onClick(View v) {
                  // setImageResource will change image in ImageView
              hImageViewSemafor.setImageResource(R.drawable.r2);
  
          }
      };
  
      View.OnClickListener cButtonChangeImageListener = new OnClickListener() {
          public void onClick(View v) {
                  // setImageResource will change image in ImageView
              hImageViewSemafor.setImageResource(R.drawable.r3);
  
          }
      };
  
      View.OnClickListener dButtonChangeImageListener = new OnClickListener() {
          public void onClick(View v) {
                  // setImageResource will change image in ImageView
              hImageViewSemafor.setImageResource(R.drawable.r4);
  
          }
      };    
  
      View.OnClickListener eButtonChangeImageListener = new OnClickListener() {
          public void onClick(View v) {
                  // setImageResource will change image in ImageView
              hImageViewSemafor.setImageResource(R.drawable.r5);
  
          }
      };   
  
  }
  </code></pre>
  
  <p>and main.xml;</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="vertical" &gt;
  
      &lt;ImageView
          android:id="@+id/idImageViewSemafor"
          android:layout_width="match_parent"
          android:layout_height="342dp"
          android:layout_weight="1.04"
          android:adjustViewBounds="true"
          android:background="#66FFFFFF"
          android:maxHeight="91dip"
          android:maxWidth="47dip"
          android:padding="10dip"
          android:src="@drawable/r0" /&gt;
  
      &lt;Button
          android:id="@+id/idBtnChangeImage"
          style="?android:attr/buttonStyleSmall"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:text="Sonraki" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>UPDATE;</p>
  
  <p>Now its like this, crashing by going backwards from the first picture;</p>
  
  <p>the code;</p>
  
  <pre><code>package com.galerionsekiz;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.ImageView;
  
  
  public class Main extends Activity {
  
      private ImageView hImageViewPic;
      private Button iButton, gButton;
  
      private int currentImage = 0;
      int[] images = { R.drawable.r1, R.drawable.r2, R.drawable.r3, R.drawable.r4, R.drawable.r5, R.drawable.r6, R.drawable.r7, R.drawable.r8, R.drawable.r9, R.drawable.r10 }; 
  
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
  
          hImageViewPic = (ImageView)findViewById(R.id.idImageViewPic);
          iButton = (Button) findViewById(R.id.bIleri);
          gButton = (Button) findViewById(R.id.bGeri);
          //Just set one Click listener for the image
  
          iButton.setOnClickListener(iButtonChangeImageListener);
          gButton.setOnClickListener(gButtonChangeImageListener);
      }
      View.OnClickListener iButtonChangeImageListener = new OnClickListener() {
  
          public void onClick(View v) {
              //Increase Counter to move to next Image
              currentImage++;
              currentImage = currentImage % images.length;
  
              hImageViewPic.setImageResource(images[currentImage]);
  
          }
      };
  
      View.OnClickListener gButtonChangeImageListener = new OnClickListener() {
  
          public void onClick(View v) {
              //Increase Counter to move to next Image
              currentImage--;
              currentImage = currentImage % images.length;
  
              hImageViewPic.setImageResource(images[currentImage]);
  
          }
      };
  }
  </code></pre>
  
  <p>the main;</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:gravity="fill"
      android:orientation="vertical"
      android:weightSum="100" &gt;
  
      &lt;ImageView
          android:id="@+id/idImageViewPic"
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="100"
          android:adjustViewBounds="true"
          android:background="#66FFFFFF"
          android:maxHeight="91dip"
          android:maxWidth="47dip"
          android:padding="10dip"
          android:src="@drawable/r0" /&gt;
  
      &lt;LinearLayout
          android:id="@+id/linearLayout1"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content" &gt;
  
          &lt;Button
              android:id="@+id/bGeri"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:text="Önceki" &gt;
          &lt;/Button&gt;
  
          &lt;Button
              android:id="@+id/bIleri"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:text="Sonraki" &gt;
          &lt;/Button&gt;
      &lt;/LinearLayout&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>any suggestions?</p>