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

Image Handling with GridView & ImageSwitcher

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)
18 views4 pages

Image Handling with GridView & ImageSwitcher

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

8.

Create application to handle images using


GridView and ImageSwitcher

Activity_main.xml

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


<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
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" />
</[Link]>

[Link]

package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {


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

[Link]([Link].FIT_CENTER);

[Link](new
[Link]([Link].FILL_PARENT,
[Link].FILL_PARENT));
return imageView;
}
});
Animation in = [Link](this,
[Link].slide_in_left);
Animation out = [Link](this,
[Link].slide_out_right);

// set the animation type to ImageSwitcher


[Link](in);
[Link](out);

[Link](findViewById([Link]), (v,
insets) -> {
Insets systemBars =
[Link]([Link]());
[Link]([Link], [Link], [Link],
[Link]);
return insets;
});
[Link](new [Link]() {
@Override
public void onClick(View v) {
currentIndex--;
// Check If index reaches maximum then reset it
if (currentIndex == count)
currentIndex = 0;
[Link](imageIds[currentIndex]); //
set the image in ImageSwitcher
}
});
[Link](new [Link]() {
@Override
public void onClick(View v) {
currentIndex++;
// Check If index reaches maximum then reset it
if (currentIndex == count)
currentIndex = 0;
[Link](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 [Link];
}
@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);
[Link](new
[Link](150,150));
[Link]([Link].CENTER_CROP);
[Link](5,5,5,5);
}
else{
imageView=(ImageView) convertView;
}
[Link](imageIds[position]);
return imageView;
}
}
}
Output:

You might also like