YouTip LogoYouTip

Android Single Fragment

** **Single Fragment**: A single fragment is designed for small-screen devices, such as handheld devices (mobile phones), and is supported in Android 3.0 and above. * * * ## Example This example explains how to create your own fragments. Here, we create two fragments: one is used when the device is in landscape mode, and the other is used when the device is in portrait mode. Let's start by following the steps. | Step | Description | | --- | --- | | 1 | Use the Android Studio IDE to create an Android application named Single Fragments with the package name cn.uprogrammer.singlefragments. | | 2 | Modify the main activity file MainActivity.java as shown below. Here, we will check the device's orientation and switch between different fragments based on it. | | 3 | Create two files, PortraitFragment.java and LandscapeFragment.java, under the package cn.uprogrammer.singlefragments, and associate the methods. | | 4 | Create layout files res/layout/landscape_fragment.xml and res/layout/portrait_fragment.xml to define the layouts for the two fragments. | | 5 | Modify res/layout/activity_main.xml to include the two fragments. | | 6 | Define the required constants in res/values/strings.xml. | | 7 | Launch the Android emulator to run the application and verify the results of the changes made by the application. | Below is the content of the main activity file src/cn.uprogrammer.singlefragments/MainActivity.java: ```java package cn.uprogrammer.singlefragment; import android.os.Bundle; import android.app.Activity; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.content.res.Configuration; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Configuration config = getResources().getConfiguration(); FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); /** * Detect the device orientation and perform corresponding operations. */ if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) { /** * Device is in landscape mode. */ LandscapeFragment ls_fragment = new LandscapeFragment(); fragmentTransaction.replace(android.R.id.content, ls_fragment); } else { /** * Device is in portrait mode. */ PortraitFragment pm_fragment = new PortraitFragment(); fragmentTransaction.replace(android.R.id.content, pm_fragment); } fragmentTransaction.commit(); } } Create two fragment files, LandscapeFragment.java and PortraitFragment.java, under the package cn.uprogrammer.singlefragments. Below is the content of the LandscapeFragment.java file: ```java package cn.uprogrammer.singlefragment; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class LandscapeFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { /** * Inflate the layout for this fragment */ return inflater.inflate( R.layout.landscape_fragment, container, false); } } Below is the content of the PortraitFragment.java file: ```java package cn.uprogrammer.singlefragment; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class PortraitFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { /** * Inflate the layout for this fragment */ return inflater.inflate( R.layout.portrait_fragment, container, false); } } Create two layout files, landscape_fragment.xml and portrait_fragment.xml, in the directory res/layout. Below is the content of the landscape_fragment.xml file: ```xml Below is the content of the portrait_fragment.xml file: ```xml Below is the content of the res/layout/activity_main.xml file, which includes the two fragments: ```xml Ensure the res/values/strings.xml file contains the following content: ```xml Single Fragment Hello world! Settings This is the landscape mode fragment This is the portrait mode fragment Let's run the Single Fragments application we just modified. I assume you have already created an AVD during the environment setup. Open the activity file in your project and click the ![Image 1: Image](#) icon in the toolbar to run the application in Android Studio. Android Studio installs the application on the AVD and launches it. If everything goes well, the following will be displayed on the emulator window: ![Image 2: Image](#) Follow the steps below to change the orientation mode of the emulator screen: * fn+control+F11 on Mac to switch between landscape and portrait, and vice versa * ctrl+F11 on Windows * ctrl+F11 on Linux When you change the mode, you will see the page implementation for landscape mode: ![Image 3: Image](#) In this way, you can implement different interfaces within the same activity by using different fragments. You can build the interface using different types of interface components according to your needs.
← Go Nested If StatementsAndroid Content Providers β†’