1

I'm writing a turn based game for Android. The main game has 3 sections. An introduction screen showing which player is to play next, the actual game screen where the player plays for points, and a board screen showing the rankings of the players.

In my implementation of this, I use 3 fragments (one for each section) and navigate between them using fragment transactions (FragmentTransaction.replace) as required.

This approach works fine, however I'd like to now add an extra feature, in that for large screen devices, the introduction screen, and the board screen would be displayed simultaneously. I understand how to do this in the xml, so when we first navigate to the introduction screen, we have both fragments displayed.

The problem I have relates the fragment transactions. When performing the transaction to pass out of the board screen, I need to determine whether to transition to a two fragment layout, or a one fragment layout? What is the best way to do this? Do I have to programmatically get the screen size before determining what transactions to perform, or is there a neater (XML?) way of doing this?

Thanks,

Will

WMycroft
  • 249
  • 1
  • 11

1 Answers1

2

Yes, there is the XML-way of doing this. But it will need some refactoring.

Create different layouts with FrameLayout containers. Phone-Layout usually contains only one container. Tablet-Layout two or three.

You have to place the layouts according to the screensizes into proper directory:

res/layout/main_activity.xml           # For handsets (smaller than 600dp available width)
res/layout-sw600dp/main_activity.xml   # For 7” tablets (600dp wide and bigger)
res/layout-sw720dp/main_activity.xml   # For 10” tablets (720dp wide and bigger)

see here: Layout for tablets in Android

Then put the fragments into container, if container exists, like user did it here: Trying to add a fragment to my fragment container FrameLayout

Community
  • 1
  • 1
AndacAydin
  • 1,416
  • 19
  • 26
  • I think I see what you mean, I'll try and get this implemented and see how it goes. My worry at the moment is that even on tablets, the game screen should take up the entire screen, if I have a two frame layout, won't the (empty) other frame take up some space? Or will the frame with the game expand to fill the whole screen? – WMycroft Apr 30 '14 at 10:05