I am new to Android programming.
I am creating a very simple application in which I have more than 5 pictures which are static (stored in drawable folder). Now, I have created an activity which is displaying all the pictures in gridView. Suppose, If I click on 2nd position image, it will open a new activity displaying that particular image in full size (using ImageView) mode plus next next/previous button to navigate on the image (It is not necessary that user can start only with first Image, he/she can select any image from the gridView and then navigation will start from that point itself).
My problem is, I don’t want to harcode the number of switch cases equal to number of images to navigate through the buttons. Is there any other way to implement the required scenario? Or any other feature of Android that I can use to achieve the same?
Thanks in advance!!!
I am adding my code also (First Activity):
public class GridViewActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grid_view);
GridView gridview = (GridView) findViewById(R.id.gridView);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//Create an Intent
Intent intent = new Intent(getApplicationContext(), FullImageActivity.class);
intent.putExtra("image", position);
startActivity(intent);
}
});
}
}
Second Activity:
public class FullImageActivity extends Activity implements OnClickListener {
Button button;
int drawable;
ImageView display;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_full_image);
Intent intent = getIntent();
display = (ImageView) findViewById(R.id.fullImageView);
ImageView imageView = null;
String idName = "ivImage";
int id;
for (int i = 1; i <= 2; i++) {
id = getResources().getIdentifier(idName + i, "id",
getPackageName());
imageView = (ImageView) findViewById(id);
imageView.setTag((Integer) i);
imageView.setOnClickListener(this);
if (i == 1) {
// make sure that your "drawable" has a value different than 0!
// otherwise a click on setWall button might result into a crash
drawable = id;
}
}
int pos = intent.getExtras().getInt("image");
ImageAdapter imageAdapter = new ImageAdapter(this);
imageView.setImageResource(imageAdapter.mThumbIds[pos]);
Log.i("pos", "" + pos);
// System.out.println("pos: " + pos);
button = (Button) findViewById(R.id.btnNextScreen);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
InputStream inputStream = getResources().openRawResource(
drawable);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
try {
getApplicationContext().setWallpaper(bitmap);
} catch (IOException e) {
// use Log.e() with all 3 params instead of
// e.printStackTrace();
Log.e("MyTag", "couldn't set wallpaper", e);
}
}
});
}
@Override
public void onClick(View v) {
// just to make sure that the clicked view is a ImageView
if (v instanceof ImageView) {
ImageView imageView = (ImageView) v;
int place = (Integer) imageView.getTag();
int drawableId = getResources().getIdentifier("image" + place,
"drawable", getPackageName());
display.setImageResource(drawableId);
drawable = drawableId;
}
}
}
I have added that code what @WarrenFaith has suggested me. After adding suggested code, it is throwing me an error on “imageView.setTag((Integer) i);”. PLease tell me where I am going wrong.
I just answered a question where the goal was to get the hardcoded stuff dynamically.
It is not exactly your type of app but the basic idea behind my answer should give you the right direction.