Professional Documents
Culture Documents
Sem. II 2019/20
Lab 2c: Android Fragment
Week 2
Introduction
Fragment usage
The 3 main usage of Fragments in Android, for which Fragments were introduced:
1. Modularity: If a single activity is having too many functional components, it’s better
to divide it into independent fragments, hence making the code more organized and
easier to maintain.
2. Reusability: If we define any particular feature in a fragment, then that feature more
or less becomes a reusable component which can be easily integrated into any
activity.
1
PART I: Fragment Example
Application specifications
The app contains two Fragments and loading them by the clicking the buttons. The screen
display two Button’s those perform setOnClickListener event and a FrameLayout,
see Figure 2. On the click of First Button the First Fragment is being replaced and on click of
Second Button we replace the Second Fragment with the layout(FrameLayout). In the both
Fragment’s , display a TextView and a Button. The onclick of Button displays the
name of the Fragment with the help of Toast.
2
<resources>
<dimen name="horizontal_margin">16dp</dimen>
<dimen name="vertical_margin">16dp</dimen>
</resources>
Step 3: Modify the activity_main layout file by adding two buttons and framelayout.
3.2 The main layout design structure for the screen is LinearLayout.
// 1st button
<Button
// 2nd button
<Button
// FrameLout
<FrameLayout
<!—- frameLayout id -->
<!—- match_parent layout_width and layout_height-->
<!—- 10 dp layout_marginTop -->
3
/>
</LinearLayout>
4.1 Add the code for initiate the Button’s defined in the layout file by perform the
setOnClickListener event.
4.2 On the click of First Button, the First Fragment is replaced and on click of Second
Button, the Second Fragment is replaced with the FrameLayout style.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//2. Map the the defined button objects to the GUI button
// defined in layout file
//5. loadFragment()
4
fragmentTransaction.replace(R.id.frameLayout, fragment);
fragmentTransaction.commint(); // save the changes
}
}
Step 5: Fragments
5.2 Right click on app package folder and create classes and name them as
FirstFragment and SecondFragment.
5.3.1 FirstFragment.java
The FirstFragment class must extends from the Fragment class.
The Fragment inflates the layout and get the reference of Button. After
that, perform setOnClickListener event on Button so whenever
a user click on the button a message “First Fragment“ is
displayed on the screen by using a Toast.
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState){
5.3.2 SecondFragment.java
5
whenever a user click on the button a message “Second
Fragment“ is displayed on the screen by using a Toast.
5.4.1 Add the following code in respective files. In both files, design the
basic simple UI by using TextView and Button.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.androidfragment.FirstFragment"
>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.androidfragment.SecondFragment"
>
6
<!-- true layout_centerHorizontal -->
<!-- 100dp layout_marginTop -->
<!-- black textColor -->
<!-- 25sp textSize -->
<!—- string name this_is_second_fragment with text
string “This is Second Fragment” text -->
6.1 Only one Activity (MainActivity) involved and it is already being defined.
6.2 Fragment don’t need to define the it in manifest because Fragment is a part of an
Activity.