0% found this document useful (0 votes)
4 views4 pages

Image Application

The document outlines the creation of an Android application that handles images using a GridView and an ImageSwitcher. It includes the XML layout for the main activity and the Java code for the MainActivity class, which manages image display and navigation through previous and next buttons. The application allows users to select images from a grid and view them in a switcher with animations.

Uploaded by

Siddarth Sriram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Image Application

The document outlines the creation of an Android application that handles images using a GridView and an ImageSwitcher. It includes the XML layout for the main activity and the Java code for the MainActivity class, which manages image display and navigation through previous and next buttons. The application allows users to select images from a grid and view them in a switcher with animations.

Uploaded by

Siddarth Sriram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

8.

Create application to handle images using


GridView and ImageSwitcher

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>


<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Previous"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.146"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.95" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.84"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.95" />

<GridView
android:id="@+id/gridView1"
android:layout_width="355dp"
android:layout_height="375dp"
android:numColumns="3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.285"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.089" />

<ImageSwitcher
android:id="@+id/imageimageSwitcher1"
android:layout_width="186dp"
android:layout_height="131dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.737" />
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

package com.example.imageapplication;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.Toast;
import android.widget.ViewSwitcher;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {


ImageSwitcher imageSwitcher;
Button previous, next;
GridView gridView;
int
imageIds[]={R.drawable.rose1,R.drawable.rose2,R.drawable.rose3,R.drawable.ros
e4,R.drawable.rose5,R.drawable.rose6,R.drawable.rose7,R.drawable.rose8,R.drawa
ble.rose9, R.drawable.rose10,R.drawable.rose11,R.drawable.rose12};
int count = imageIds.length;
int currentIndex = -1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
previous=(Button)findViewById(R.id.button);
next=(Button)findViewById(R.id.button2);
gridView =(GridView)findViewById(R.id.gridView1);
gridView.setAdapter(new ImageAdapter(this));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int
position, long id) {
Toast.makeText(getApplicationContext(),"Picture"+
(position+1)+" Selected",Toast.LENGTH_LONG).show();
}
});
imageSwitcher=(ImageSwitcher)findViewById(R.id.imageimageSwitcher1);
imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
ImageView imageView = new ImageView(getApplicationContext());

imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);

imageView.setLayoutParams(new
ImageSwitcher.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT));
return imageView;
}
});
Animation in = AnimationUtils.loadAnimation(this,
android.R.anim.slide_in_left);
Animation out = AnimationUtils.loadAnimation(this,
android.R.anim.slide_out_right);

// set the animation type to ImageSwitcher


imageSwitcher.setInAnimation(in);
imageSwitcher.setOutAnimation(out);

ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v,
insets) -> {
Insets systemBars =
insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right,
systemBars.bottom);
return insets;
});
previous.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
currentIndex--;
// Check If index reaches maximum then reset it
if (currentIndex == count)
currentIndex = 0;
imageSwitcher.setImageResource(imageIds[currentIndex]); //
set the image in ImageSwitcher
}
});
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
currentIndex++;
// Check If index reaches maximum then reset it
if (currentIndex == count)
currentIndex = 0;
imageSwitcher.setImageResource(imageIds[currentIndex]); //
set the image in ImageSwitcher

}
});
}

private class ImageAdapter extends BaseAdapter {


Context c;
public ImageAdapter(Context context){
c=context;
}

@Override
public int getCount() {
return imageIds.length;
}
@Override
public Object getItem(int position) {
return position;
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ImageView imageView;
if(convertView==null){
imageView= new ImageView(c);
imageView.setLayoutParams(new
ViewGroup.LayoutParams(150,150));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(5,5,5,5);
}
else{
imageView=(ImageView) convertView;
}
imageView.setImageResource(imageIds[position]);
return imageView;
}
}
}
Output:

You might also like