Skip to content Skip to sidebar Skip to footer

Making Image Full Screen On Android Tutorial App

Using the Hello, World Gridview tutorial example, I am attempting to make the image fullscreen on click instead of display the position of the image in the array. As I am unfamila

Solution 1:

imgView.setImageResource(position); 

This gives you an error because you are not using it correctly. The parameter should be pointing to the resource ID of the image (which is an int) e.g. R.id.imageid. Even if you were to supply the right resource ID, what you want to do would not work.

To get the right resource ID, you would need to call the adapter of the view and use the getItemId(position) method to get the correct resource ID. in your ImageAdapter, if you change your getItemId() method to return mThumbsIds[position] rather than 0 (as in Google's example).

However, to achieve your result I would suggest for you to create a new Activity and just load the image like that.

For example:

gridview.setOnItemClickListener(new OnItemClickListener() {
    publicvoidonItemClick(AdapterView<?> parent, View v, int position, long id) {
        long imageId = (ImageAdapter)parent.getAdapter.getItemAt(position);

        Intent fullScreenIntent = new Intent(v.getContext(), FullScreenImage.class);
        fullScreenIntent.putExtra(thisClassName.class.getName(), imageId);

        thisClassName.this.startActivity(fullScreenIntent);
    }
});

Assuming you create a full_image.xml layout file, containing just an ImageView with an id of "fullImage", your FullScreenImage class should look like this:

publicclassFullScreenImageextendsActivity
{
    protectedvoidonCreate(Bundle savedInstanceState) {
        setContentView(R.layout.full_image);
        Intentintent= getIntent();
        longimageId= intent.getExtras().get(thisClassName.class.getName());
        ImageViewimageView= (ImageView) v.findViewById(R.id.fullImage);

        imageView.setLayoutParams( newViewGroup.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT));

        imageView.setImageResource(imageId);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
    }
}

Something along these lines should work well for you, with some tweaking. Make sure you add FullScreenImage in your AndroidManifest.xml file as an <activity> as well.

Solution 2:

Here, you're taking the ID of the view that was clicked (passed in the view argument) and using it to look up itself again:

ImageViewimgView= (ImageView) findViewById(view.getId()); 

That makes no sense, especially since the clicked view apparently has no ID, causing findViewById to return null.

You probably want to pass the ID of your ImageView instead:

ImageViewimgView= (ImageView) findViewById(R.id.whatever_the_id_of_the_image_view_is);

Post a Comment for "Making Image Full Screen On Android Tutorial App"