Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Layout changes after turning the emulator, but not the device

I need to show 2 columns in RecyclerView when the orientation is portrait and 4 columns when orientation is land. It works correct on the emulator.

enter image description here

It's RecyclerView in portrait orientation.

Turning the emulator:

enter image description here

As you can see, there is refresh button. And after clicking on it we see 4 columns on screen:

enter image description here

But when I run the project on my mobile phone, there is no refresh button and I see 2 columns in land orientation.

Here's a part of my code:

@Override
public void onConfigurationChanged(Configuration newConfig){
    super.onConfigurationChanged(newConfig);
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
        previewImages.setLayoutManager(new GridLayoutManager(this, 4));
    }
    else {
        previewImages.setLayoutManager(new GridLayoutManager(this, 2));
    }
}

And I added this line to manifest:

android:configChanges="orientation"

So, what's the problem and how can I solve it?

like image 200
Sergei Mikhailovskii Avatar asked Jan 28 '26 21:01

Sergei Mikhailovskii


1 Answers

can you change your code block in onConfigurationChanged method like this. maybe can help.

new GridLayoutManager(context, 1, GridLayoutManager.HORIZONTAL, false)
like image 112
ozanurkan Avatar answered Jan 30 '26 13:01

ozanurkan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!