WMycroft
WMycroft

Reputation: 249

Fragments for different screen sizes

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

Upvotes: 1

Views: 2108

Answers (1)

AndacAydin
AndacAydin

Reputation: 1426

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

Upvotes: 2

Related Questions