How to show imageView full screen on imageView click?

Amit Jayaswal picture Amit Jayaswal · Jun 28, 2014 · Viewed 148.1k times · Source

I am getting images from url and showing it on the imageView. This functionality is working properly. But I want that when I click on that image, then it must be full screen. So how to achieve this functionality? I know I am missing something. Please help me. Screenshot is attached. I want the same functionality in my app also.

Before clicking After clicking

Here is my code, which I am trying on Image click:

    @Override
    public void onClick(View v) {
        if (isTouch1) {
            horizontalScrollView.setVisibility(View.GONE);
            isTouch1 = false;
            // mSystemUiHider.toggle();
            setTheme(R.style.FullscreenTheme);
            Log.d("Here isTouch is true", ">");
            // ChangeThemeUtils.changeToTheme(FullScreenImageAdapter.this, ChangeThemeUtils.THEME_HIDE_ALL_WINDOW);
            getSupportActionBar().hide();

        } else {
            isTouch1 = true;
            horizontalScrollView.setVisibility(View.VISIBLE);
            getSupportActionBar().show();
            setTheme(R.style.ExampleTheme);
            //mSystemUiHider.show();
            Log.d("Here isTouch is false", ">");    
        }
    }

Answer

Haresh Chhelana picture Haresh Chhelana · Jun 28, 2014

You can use ImageView below two properties to show image based on your requirement :

  1. android:adjustViewBounds : Set this to true if you want the ImageView to adjust its bounds to preserve the aspect ratio of its drawable.

  2. android:scaleType :Controls how the image should be resized or moved to match the size of this ImageView

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:src="@drawable/ic_launcher"/>
    

Above two properties can be use either xml or java code.

As you need to decide at run time need to show image into full screen or not so will apply above two properties at java code as below :

public class MainActivity extends Activity {

    ImageView imageView;

    boolean isImageFitToScreen;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = (ImageView) findViewById(R.id.imageView);

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isImageFitToScreen) {
                    isImageFitToScreen=false;
                    imageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
                    imageView.setAdjustViewBounds(true);
                }else{
                    isImageFitToScreen=true;
                    imageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
                    imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                }
            }
        });

    }
}