-1

I have an Activity and two Fragment layouts and classes.
All I need to do is that when I change the screen orientation, reorganize the same content differently.

My question is where should I put my code?
Which class Activity class or Fragment class?

If I code in Fragment class, should I put code in both classes, or if I use main Activity, how would I find objects (like TextViews) from each layout?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

2 Answers2

0

You put the code where the views are... It's really that simple.

res/layout-land should keep the exact same view IDs.

For example, MainActivity would load both res/layout/activity_main.xml and res/layout-land/activity_main.xml automatically for you depending on orientation.

findViewById will work appropriately if you don't change any ID values

There is a whole documentation page on this. Notice they use retained Fragments, but that is not necessary.

Also Android Studio: Creating landscape layouts

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
-1

Based on Comments what i have understood so far.

You have an activity and you have two fragments. These two fragments have different layouts and you want to show these layouts based on the orientation of the device.

Well there are two approach for this problem

1) You want to achieve this using fragments

Create an abstract class for Fragment put common functionality in base class and provide different implementation in different Fragment child class

You can refer this

2) Second approach

It can be achieved in a much simpler way without using fragment. Create a layout-land directory and put the landscape version of your layout XML file in that directory.

See this thread in stackoverflow

Hope it helps.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Rohit Singh
  • 16,950
  • 7
  • 90
  • 88