activity_main
<?xml version="1.0" encoding="utf-8"?>
<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:background="@color/purple_700"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_centerInParent="true"
android:layout_marginTop="112dp"
android:layout_marginEnd="8dp"
android:src="@drawable/ic_launcher_background" />
<Button
android:id="@+id/pre"
android:layout_width="90dp"
android:layout_height="70dp"
android:layout_marginStart="11dp"
android:layout_below="@+id/imageView"
android:layout_marginTop="20dp"
android:text="Pre" />
<Button
android:id="@+id/next"
android:layout_width="90dp"
android:layout_height="70dp"
android:layout_below="@+id/imageView"
android:layout_alignParentEnd="true"
android:layout_marginTop="20dp"
android:layout_marginEnd="13dp"
android:text="Next" />
</RelativeLayout>
Here we have created two buttons pre and next, on which the user will click, then the image will be next position and back.
MainActivity.java
package com.example.nextpre;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Button next,pre;
int p=0;
private ImageView imageView;
public int per[]=new int[]{R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
next=findViewById(R.id.next);
pre=findViewById(R.id.pre);
imageView=findViewById(R.id.imageView);
imageView.setImageResource(per[p]);
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (p < 4) {
p++;
imageView.setImageResource(per[p]);
}
else
{
Toast.makeText(MainActivity.this, "No next item", Toast.LENGTH_SHORT).show();
}
}
});
pre.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (p >0) {
p--;
imageView.setImageResource(per[p]);
}
else
{
Toast.makeText(MainActivity.this, "No next item", Toast.LENGTH_SHORT).show();
}
}
});
}
}
First we will store the image in an array, after that we will create an integer type variable in which we will assign 0 And will create a variable of image type in which the image is displayed on pressing the back next button.Thank you
0 Comments
Please do not enter any spam link in the comment box
Emoji