2

I'm just now learning android, and I have an application that has the main activity add a fragment on startup using the fragment manager. The program then switches between two fragments to perform its function using, again, the fragment manager to replace fragments.

I want this app to display the two fragments together when on a large screen. How do I

A) Detect that there is a large screen

and

B) Have the main activity load the two fragments into the single activity if this is the case?

There are layouts for each fragment and a layout for the main activity which is simply a blank framelayout that is filled in with a fragment during the onCreate. Please remember: there are two fragments, and one activity.The activity loads the fragments into its container using the fragment manager.

tobydog
  • 23
  • 1
  • 7

2 Answers2

4

A) To detect Large Screens you can use this code

public static boolean isTablet(Context context) {
return (context.getResources().getConfiguration().screenLayout
        & Configuration.SCREENLAYOUT_SIZE_MASK)
        >= Configuration.SCREENLAYOUT_SIZE_LARGE;
}

which will return true if the device is operating on a large screen. Also check this link

B) You can have two fragments on same activity by adding fragments by code. Have 2 layouts in you main layout, which will be containers for both the fragments. then add fragment into each of them by code if screen size suits you. Use this code.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:orientation="vertical"
    android:layout_height="fill_parent">

<RelativeLayout
    android:id="@+id/FragmentContainer1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
<RelativeLayout
    android:id="@+id/FragmentContainer2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

</LinearLayout>

And then to add Fragments.

fragmentTransaction.add(R.id.FragmentContainer1, fragment);
fragmentTransaction.add(R.id.FragmentContainer2, fragment);

Check this link for a nice tutorial by Lars Vogel.

rachit
  • 1,981
  • 15
  • 23
  • Thank you, this makes a lot of sense to me, and the program works. What can I do to preserve the application's current fragment states when I press back? When I press home, and return to the app everything is saved, but when I press back and return, everything is reset. Thanks again! – tobydog Jul 06 '14 at 22:40
  • Why not FrameLayouts? But RelativeLayouts. – Yuliia Ashomok Aug 10 '16 at 09:32
0

How do I detect that there is a large screen ?

Solution -

Let the application detect if there is a large screen.

  • Define different layout files for different screen sizes.
  • Ensuring your layout can be adequately resized to fit the screen
  • Providing appropriate UI layout according to screen configuration
  • Ensuring the correct layout is applied to the correct screen

Read - Multiple Screen Support and Support Screen training docs.

How do I have the main activity load the two fragments into the single activity if this is the case?

Solution -

enter image description here

You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. This is essentially useful when you have defined your fragment container at different layouts. You just need to replace with any other fragment in any layout.

When you navigate to the current layout, you have the id of that container to replace it with the fragment you want.

You can also go back to the previous fragment in the backStack with the popBackStack() method. For that you need to add that fragment in the stack using addToBackStack() and then commit() to reflect. This is in reverse order with the current on top.

Description:

  • Let's we want to use two fragments to handle landscape and portrait modes of the device.
  • Next based on number of fragments, create classes which will extend the Fragment class. The Fragment class has its own callback functions. You can override any of the functions based on your requirements.
  • Corresponding to each fragment, you will need to create layout files in XML file. These files will have layout for the defined fragments.
  • Finally modify activity file to define the actual logic of replacing fragments based on your requirement.

Steps to create two fragments in Activity -

1 You will use Eclipse IDE to create an Android application and name it as MyFragments under a package com.example.myfragments, with blank Activity.

2 Modify main activity file MainActivity.java as shown below in the code. Here we will check orientation of the device and accordingly we will switch between different fragments.

3 Create a two java files PM_Fragment.java and LM_Fragement.java under the package com.example.myfragments to define your fragments and associated methods.

4 Create layouts files res/layout/lm_fragment.xml and res/layout/pm_fragment.xml and define your layouts for both the fragments.

5 Modify the detault content of res/layout/activity_main.xml file to include both the fragments.

6 Define required constants in res/values/strings.xml file

7 Run the application to launch Android emulator and verify the result of the changes done in the aplication.

Read - FragmentTransaction and Android Fragments.

sjain
  • 23,126
  • 28
  • 107
  • 185