Picasso load different images in portrait and landscape mode

I have a fragment which downloads an image using Picasso and displays it in ImageView. Problem is the image is in portrait mode and if the phone changes to landscape mode the same image gets stretched. I looked for solutions but what I got in general sense is to simply rotate the to 90 degrees. But that's not what I want. I want to load two different images i.e. one image url will be for portrait mode and the other will be for landscape mode. I tried detecting orientation change and then loading the images accordingly. This didn't worked. In landscape mode also, the portrait images were loading. Any suggestions?

public class FragmentOne extends Fragment{

    Button next, previous;
    ProportionalImageView imageView;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragmentone, container, false);

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        next = (Button)view.findViewById(R.id.next);
        previous = (Button)view.findViewById(R.id.previous);
        imageView = (ProportionalImageView)view.findViewById(R.id.image);

        Picasso.with(getActivity())
                .load("http://i.imgur.com/h5iwVmh.jpg")
                .placeholder(R.drawable.placeholder)
                .into(imageView);



        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((ViewPager) ((MainActivity)getActivity()).findViewById(R.id.pager)).setCurrentItem(1);
            }
        });

        previous.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((ViewPager) ((MainActivity)getActivity()).findViewById(R.id.pager)).setCurrentItem(-1);
            }
        });

    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        // Checks the orientation of the screen
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            Picasso.with(getActivity())
                    .load("http://i.imgur.com/H0IXUy0.jpg")
                    .placeholder(R.drawable.placeholder)
                    .into(imageView);

        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
            Picasso.with(getActivity())
                    .load("http://i.imgur.com/h5iwVmh.jpg")
                    .placeholder(R.drawable.placeholder)
                    .into(imageView);


        }
    }








}

Answers


you can use this code for get the orientation

Display display = ((WindowManager)    context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int rotation = display.getRotation();

and load the image according rotation condition. exm :- if(rotation== your condition) { }

you can also use to below code:

public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    // refresh the instructions image
    ImageView instructions = (ImageView) findViewById(R.id.img_instructions);

   if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        //load image in picasso here
    } else {
        //load image in picasso here
    }
}

The problem is when you rotate the device the onViewCreated is called again (but it called after onConfigurationChanged called)

In your code, the image have changed to landscape onConfigurationChanged, but after that the image change to portrait when onViewCreated called. This is the reason why you always have portrait image

Therefore you should change the image in onViewCreated instead of in onConfigurationChanged

@Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    ...
    int currentOrientation = getResources().getConfiguration().orientation;
    if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) {
        // load you landscape image
    }
    else {
       // load your portrait image
    }
    ...
}

Hope this help


Need Your Help

How to write log file when no need to close file in running application?

c# file logging

We have static class Logger in C#, where log file initializes at starting of application. Log files gets roll back (new log file gets created) after each 512 bytes. Now problem is, I am not able to...

Renaming start of all file names in folder before first underscore

windows batch-file command-line command

In a batch file i would like to rename the start of all files in one folder, the files at the moment are named as such