Akhanda MAD7

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment 3.1
Student Name: Akhanda Pal Biswas UID: 21BCS5828
Branch: B.E./CSE Section/Group: 21BCS_KRG-CC-2(A)
Semester: 6th Date of Performance:28/03/24
Subject Name: MADLAB Subject Code: 21CSH-355

1. Aim: Create an Android-based application using Fragments.

2. Objective: The objective of an Android application using Fragments can be to


enhance the user interface and improve the overall user experience by leveraging
the benefits of fragment-based design. Fragments allow developers to create
more modular, flexible, and scalable UI components.

3. Apparatus/Requirements:
1. Android Studio
2. Computer Storage
3. Android Virtual Device or USB debugging
4. Minimum Coding knowledge about JAVA.

4. Procedure:

System Requirements:
The required tools to develop Android applications are open source and can be
downloaded from the Web. Following is the list of software's we will need before
we start your Android application programming.

(Minimum Requirements)
1. Java JDK5 or later version
2. Java Runtime Environment (JRE) 6 Android Studio
3. AMD or M1 chipset.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Step 1
Firstly, creation of an Empty Views Activity will take place.

Step 2
Then we will name the App and select the App build language as JAVA and then proceed.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Step 3
Now we will have to create 3 buttons for 3 different fragments and then declare a frame-
layout that will hold the fragments dynamically.

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
android:layout_width="wrap_content"

android:layout_height="wrap_content"
android:id="@+id/but1"
android:layout_weight="1"
android:text="fragA"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/but2"
android:layout_weight="1"
android:text="fragB"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/but3"
android:layout_weight="1"
android:text="fragC"/>
</LinearLayout>

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container"/>
</LinearLayout>

Step 4
Now in the Java file we have to define the working of the buttons and also define the loading
of the fragments.

package com.example.myapplication7;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Button bt1,bt2,bt3;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

bt1 = findViewById(R.id.but1);
bt2 = findViewById(R.id.but2);
bt3 = findViewById(R.id.but3);

loadFrag(new BBlankFragment(), 0);

bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loadFrag(new ABlankFragment(),1);

}
});

bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

loadFrag(new BBlankFragment(), 1);}});


bt3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loadFrag(new CBlankFragment(), 1);

}});}
public void loadFrag(Fragment fragment, int flag)
{
FragmentManager fm= getSupportFragmentManager();
FragmentTransaction ft= fm.beginTransaction();
if(flag==0)
ft.add(R.id.container,fragment);
else
ft.replace(R.id.container,fragment);
ft.commit();}}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Step 5

After all this is done we have to create 3 different fragment’s xml and Java files. We will
remove all the unnecessary info from the java file and only keep a constructor along with
the “OnCreateView” method for its functioning.

XML file for Fragment-A:

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout
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"
android:background="#41EA48"
tools:context=".ABlankFragment">

<TextView
android:id="@+id/txtfrag2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="45dp"
android:text="This is Fragment A" />

</LinearLayout>

Java file for Fragment A:

package com.example.myapplication7;
import android.os.Bundle;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class ABlankFragment extends Fragment {
public ABlankFragment() {
// Required empty public constructor}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view =inflater.inflate(R.layout.fragment_a_blank, container, false);
// TextView textview =view.findViewById(R.id.txtfrag2);
return view;}}
Step 6
Now we will go to the Device Manager and then see if a virtual device is available for the
Application to run if yes then we run the App or else we install one of the devices.

Step 7
Now we start the and the click on three respective buttons to check different fragments.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

5. Observation:
• Project Workspace.
• SDK Manager Verification.
• Creation of Activities.
• Creation of Text fields.
• Creation of Buttons.
• Learn functions to store Text and display them.
• Sending Messages.

Learning Outcomes:
1. Learnt about Android Studio.
2. Learnt about Fragments.
3. Learnt about Creating multiple Fragments in a single Activity.
4. Learnt about different files in Android Studio.

You might also like