MainActivity.
java
package [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, openGrid;
int[] imageIds = {[Link].rose1, [Link].rose2, [Link].rose3, [Link].rose1, [Link].rose2,
[Link].rose3};
int count = [Link];
int currentIndex = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
imageSwitcher = findViewById([Link].imageimageSwitcher1);
previous = findViewById([Link]);
next = findViewById([Link].button2);
openGrid = findViewById([Link]);
[Link](new [Link]() {
@Override
public View makeView() {
ImageView imageView = new ImageView(getApplicationContext());
[Link]([Link].FIT_CENTER);
[Link](new [Link](
[Link].MATCH_PARENT, [Link].MATCH_PARENT));
return imageView;
}
});
Animation in = [Link](this, [Link].slide_in_left);
Animation out = [Link](this, [Link].slide_out_right);
[Link](in);
[Link](out);
[Link](imageIds[currentIndex]);
[Link](v -> {
currentIndex--;
if (currentIndex < 0)
currentIndex = count - 1;
[Link](imageIds[currentIndex]);
});
[Link](v -> {
currentIndex++;
if (currentIndex == count)
currentIndex = 0;
[Link](imageIds[currentIndex]);
});
[Link](v -> {
Intent intent = new Intent([Link], [Link]);
startActivity(intent);
});
}
}
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class GridActivity extends AppCompatActivity {
int[] imageIds = {[Link].rose1, [Link].rose2, [Link].rose3, [Link].rose1, [Link].rose2,
[Link].rose3};
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_grid);
GridView gridView = findViewById([Link].gridView1);
[Link](new ImageAdapter(this, imageIds));
[Link]((parent, view, position, id) -> {
[Link](getApplicationContext(), "Picture " + (position + 1) + " Selected",
Toast.LENGTH_SHORT).show();
});
}
private static class ImageAdapter extends BaseAdapter {
Context context;
int[] imageIds;
public ImageAdapter(Context context, int[] imageIds) {
[Link] = context;
[Link] = imageIds;
}
@Override
public int getCount() {
return [Link];
}
@Override
public Object getItem(int position) {
return imageIds[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(context);
[Link](new [Link](150, 150));
[Link]([Link].CENTER_CROP);
[Link](5, 5, 5, 5);
} else {
imageView = (ImageView) convertView;
}
[Link](imageIds[position]);
return imageView;
}
}
}
activity_main.xml
<LinearLayout xmlns:android="[Link]
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<ImageSwitcher
android:id="@+id/imageimageSwitcher1"
android:layout_width="match_parent"
android:layout_height="300dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_marginTop="16dp">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Previous" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:layout_marginLeft="16dp" />
</LinearLayout>
<Button
android:id="@+id/openGridButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open Grid View"
android:layout_marginTop="24dp"
android:layout_gravity="center_horizontal" />
</LinearLayout>
activity_grid.xml
<GridView xmlns:android="[Link]
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:padding="10dp"
android:stretchMode="columnWidth"
android:gravity="center" />