Reputation: 799
i have 30 images in my project and i want to display 10 of them in a single image view using next and previous button (like a slide show) how do i do that ? thanks!
previousButton = (Button) findViewById(R.id.backButton);
previousButton.setOnClickListener(this);
nextButton = (Button) findViewById(R.id.nextButton);
nextButton.setOnClickListener(this);
set image in onclick method
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
if (view == previousButton) {
--positionOfSelectedImage;
// set background image of
} else if (view == nextButton) {
++positionOfSelectedImage;
}
imageToBeSet.setImageURI(Uri.parse(absolutepathOfImage));
}
Upvotes: 0
Views: 5984
Reputation: 999
make array for images to store all images from drawable also your back and next button and the imageview where you want to preiview all images,and start int n from zero
ImageView previewImg,backImg,nextImg;
int[] mario = new int[]{R.drawable.nature_07,R.drawable.nature_08,R.drawable.nature_09,
R.drawable.nature_10,R.drawable.nature_11,R.drawable.nature_12,
R.drawable.nature_13,R.drawable.nature_14
};
int n =0;
**in the oncreate method do it by the following code**
previewImg = findViewById(R.id.previewImg);
backImg = findViewById(R.id.backBtn);
nextImg = findViewById(R.id.nextBtn);
//your default image to preview when ist time its is open
previewImg.setImageResource(R.drawable.nature_12);
nextImg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//the n value is 7 cause the array size is 7
if(n < 7)
n++;
previewImg.setImageResource(mario[n]);
}
});
backImg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
previewImg.setImageResource(mario[n]);
if(n>0)
n--;
}
});
Upvotes: 0
Reputation: 14489
There's a much more simple way to do that.
Try this code:
package com.example.jre;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity implements View.OnClickListener {
Button btprevious, btnext;
ImageView myImage;
public int i = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btprevious = (Button) findViewById(R.id.button1);
btnext = (Button) findViewById(R.id.button2);
myImage = (ImageView) findViewById(R.id.myImage);
btprevious.setOnClickListener(this);
btnext.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button1:
i++;
if(i==2) // switch to 11 because you got 10 images
{
i=1; // switch to 10, same reason
}
changeImage();
break;
case R.id.button2:
i--;
if(i==-1)
{
i=0; // you can leave it this way or improve it later
}
changeImage();
break;
}
}
public void changeImage()
{
myImage = (ImageView) findViewById(R.id.myImage);
switch(i)
{
case 0:
myImage.setImageResource(R.drawable.firstimage);
break;
case 1:
myImage.setImageResource(R.drawable.secondimage);
break;
// and then it goes further
}
}
}
And in your xml file:
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.jre.MainActivity" >
<ImageView
android:id="@+id/myImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="86dp"
android:src="@drawable/ic_launcher" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_toRightOf="@+id/myImage"
android:text="Next" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/myImage"
android:layout_marginTop="32dp"
android:layout_toLeftOf="@+id/myImage"
android:text="Previous" />
</RelativeLayout>
That should do the work!
Upvotes: 2
Reputation: 5220
define your images address (eg: R.drawable.xxx) in a List
and then :
List <String> imagePath = new ArrayList<String> ();
imagePath.add("image1Path");
imagePath.add("image2Path");
...
imagePath.add("image30Path");
previousButton = (Button) findViewById(R.id.backButton);
previousButton.setOnClickListener(this);
nextButton = (Button) findViewById(R.id.nextButton);
nextButton.setOnClickListener(this);
myImageView = (ImageView) findViewById(R.id.imageView);
int index = 0;
//show first image in list
myImageView.setImageBitmap(BitmapFactory.decodeFile(imagePath.get(0)));
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
if (view.getId() == R.id.backButton) {
--index;
// set background image of
} else if (view.getId() == R.id.nextButton) {
++index;
}
myImageView.setImageBitmap(BitmapFactory.decodeFile(imagePath.get(index)));
}
Upvotes: 0