I have a 2 column grid layout but I would like to change it to 3 columns when the orientation changes to landscape. How do I go about doing so?
Here is my OnCreate method where I set up the RecyclerView and Grid.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
birdList = new ArrayList<>();
getJSONfromFile();
// Recycler View
mainRecyclerView = findViewById(R.id.main_recycler_view);
mainRecyclerView.setLayoutManager(new GridLayoutManager(this, 2));
// Set Adapter
BirdAdapter adapter = new BirdAdapter(this, birdList);
mainRecyclerView.setAdapter(adapter);
}
Try handling this inside your onCreateView method instead since it will be called each time there's an orientation change:
if(getActivity().getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
mainRecyclerView.setLayoutManager(new GridLayoutManager(this, 2));
}
else{
mainRecyclerView.setLayoutManager(new GridLayoutManager(this, 3));
}