You are on page 1of 34

MOBILE APP DEVELOPMENET LAB

WEEK 1: Install Android Studio

Android Studio provides a complete integrated development environment (IDE) including an


advanced code editor and a set of app templates. In addition, it contains tools for
development, debugging, testing, and performance that make it faster and easier to develop
apps. You can test your apps with a large range of preconfigured emulators or on your own
mobile device, build production apps, and publish on the Google Play store.
PROCEDURE:
Phase 1: Install Android Studio
Phase 2: Create Hello World App
Step 1: Open Android Studio
Step 2: In the main Welcome to Android Studio window, click New Project
Step 3: choose Empty Activity
Step 4: Enter Project Details
Step 5: Explore Project Details
Step 6: Study Code
Step 7: Use Android Virtual Device (Emulator)
Step 8: Run the App on the Virtual Device

Phase 1: Install Android Studio

1
Procedure:
Step 1: Download from https://developer.android.com/studio/index.html
Step 2: Run Setup→Next... → Finish
Step 3: Import Studio Setting Option →Do not Import Settings
Step 4: Install Type Option →Standard
Step 5: Select UI Theme → Done

Phase 2: Create Hello World App


Procedure:
Step 1: Open Android Studio
Step 2: In the main Welcome to Android Studio window, click New Project

Step 3: choose Empty Activity

2
Step 4: Enter Project Details

Enter Name→Accept Default Package Name→Choose Save Location→Select Language→ Select


Minimum SDK →Click Finish Button

Android Studio creates a folder for your projects, and builds the project with Gradle.
You may also see a "Tip of the day" message with keyboard shortcuts and other useful tips.
Click Close to close the message.
The Android Studio editor appears. Follow these steps:
1. Click the activity_main.xml tab to see the layout editor.
2. Click the layout editor Design tab, if not already selected, to show a graphical
rendition of the layout as shown below.

Step 5: Explore the Project


MainActivity.xml

3
activity_main.xml

Palette

Component Tree

4
Design View

Split View

Project Pane

5
Gradle Scripts

build.gradle

6
AndroidMainfest.xml

Step 6: Study Code (Text View Component)

7
Step 7: Use Android Virtual Device (Emulator)
Create Android Virtual Device (AVD):
Menu →Tools →Device Manger → Create Device

Choose Device Definition →Select Category (Phone)→ Select Pixel 2 →Next →Select
System Image →Install →Next

Verify Configuration →Finish


Device will be added.

8
Step 8: Run the App on the Virtual Device

9
WEEK-2: Create Hello World App

Objective:
Create Hello World App
Step 1: Open Android Studio
Step 2: In the main Welcome to Android Studio window, click New Project
Step 3: choose Empty Activity
Step 4: Enter Project Details
Enter Name (Week2-257) →Accept Default Package Name→ Choose Save Location → Select
Language →Select Minimum SDK (API 24: Android 7.0 – Nougat) →Click Finish Button
Step 5: Write the Code to Hello world
Design UI
Open activity_main.xml →Select Design View→ Add Widgets - Text View
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Step 6: Open MainActivity.java and write Code


package com.example.week2-257;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

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

10
Step 7: Use Android Virtual Device (Emulator)
Step 8: Run the App on the Virtual Device
Output:

11
WEEK 3: Create “Say Hello” App
Objective:
Create Mobile App and Implement Button Click Event
PROCEDURE:
Step 1: Open Android Studio
Step 2: In the main Welcome to Android Studio window, click New Project
Step 3: choose Empty Activity
Step 4: Enter Project Details
Step 5: Write the Code to implement Button Click Event
Step 6: Use Android Virtual Device (Emulator)
Step 8: Run the App on the Virtual Device

Phase 1: Create Hello World App


Procedure:
Step 1: Open Android Studio
Step 2: In the main Welcome to Android Studio window, click New Project
Step 3: choose Empty Activity
Step 4: Enter Project Details
Enter Name (Demo1) → Accept Default Package Name →Choose Save Location→ Select
Language→ Select Minimum SDK (API 24: Android 7.0 – Nougat) →Click Finish Button
Step 5: Design UI
Open activity_main.xml→ Select Design View →Add Widgets - EditText, TextView and
Button.
Step 6: Add Attribute Data
Widget Id Other Attributes
EditText editTextName id="@+id/editTextName"
hint="Enter Name"
inputType="text"
TextView textViewName id="@+id/textViewName"
text="Hi"
Button btnSubmit id="@+id/btnSubmit"
layout_marginTop="8dp"
onClick="SayHello"
text="Say Hello"

Step 7: Open MainActivity.java and write Code

12
package com.example.week3_257;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
TextView textViewName;
EditText editTextName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void SayHello(View view) {
editTextName = findViewById(R.id.editTextName); //EditText
textViewName = findViewById(R.id.textViewName); //TextView
textViewName.setText(editTextName.getText());
}
}
Output:

13
WEEK 4: Create “Registration” App

Objective:
Design and Development of Simple Student Registration App
PROCEDURE:
Step 1: Open Android Studio
Step 2: In the main Welcome to Android Studio window, click New Project
Step 3: choose Empty Activity
Step 4: Enter Project Details
Step 5: Create Student.java Class
Step 6: Design UI and Write Code for MainActivity
Step 7: Create and Select Android Virtual Device (Emulator)
Step 8: Run the App on the Virtual Device

Procedure:
Step 5: Create Student Class
Open Student.java → Write Code
package com.example.week4_257;
import java.io.Serializable;
public class Student implements Serializable {
public String Name;
public String Password;
public String Address;
public String Gender;
public String Age;
public String DOB;
public String State;
}
Step 6: Design UI in MainActivity
6a. Open activity_main.xml→ Select Design View
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"

14
android:layout_height="match_parent"
android:padding="16sp"
android:scrollbarSize="25sp"

tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp">

<EditText
android:id="@+id/editTextName"
android:hint="Enter Name"
android:inputType="text"
android:layout_width="match_parent"
android:layout_marginTop="10dp"
android:layout_height="40dp">
</EditText>
<EditText
android:id="@+id/editTextPassword"
android:hint="Enter Password"
android:inputType="textPassword"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="40dp">
</EditText>
<EditText
android:id="@+id/editTextAddress"
android:hint="Enter Address"
android:inputType="textMultiLine"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="40dp">
</EditText>

<RadioGroup
android:id="@+id/rbGroupGender"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:checkedButton="@id/rbMale">
<TextView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="Gender:"
android:textSize="20dp">
</TextView>
<RadioButton
android:id="@+id/rbMale"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="Male"
android:textSize="20dp"
android:layout_marginLeft="10dp"/>
<RadioButton
android:id="@+id/rbFemale"

15
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="Female"
android:textSize="20dp"
android:layout_marginLeft="10dp"/>
</RadioGroup>
<EditText
android:id="@+id/editTextAge"
android:hint="Enter Age"
android:inputType="number"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="40dp">
</EditText>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="Date of Birth:"
android:textSize="20dp">
</TextView>
<EditText
android:id="@+id/editTextDOB"
android:hint="DD/MM/YYYY"
android:inputType="date"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_marginLeft="10dp"
android:textSize="18dp"

android:layout_height="40dp">
</EditText>
<Button
android:id="@+id/btnDatePicker"
android:layout_width="50dp"
android:layout_height="40dp"
android:text="..."
android:layout_marginLeft="10dp"
android:onClick="ShowDatePicker">

</Button>
</LinearLayout>
<Spinner
android:id="@+id/spinnerState"
android:layout_width="match_parent"
android:layout_height="40sp"
android:layout_marginTop="10dp"/>
<Button
android:id="@+id/btnSubmit"
android:text="SUBMIT"
android:onClick="SaveData"
android:layout_marginRight="10dp"
android:layout_width="match_parent"
android:layout_height="40dp">
</Button>
<TextView
android:id="@+id/textViewName"
android:layout_width="match_parent"
android:layout_height="40dp"

16
android:text="Name:"
android:layout_marginTop="10dp"
android:textSize="20dp"/>
<TextView
android:id="@+id/textViewPassword"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="Password: "
android:layout_marginTop="10dp"
android:textSize="20dp"/>
<TextView
android:id="@+id/textViewAddress"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="Address: "
android:layout_marginTop="10dp"
android:textSize="20dp"/>
<TextView
android:id="@+id/textViewGender"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="Gender: "
android:layout_marginTop="10dp"
android:textSize="20dp"/>
<TextView
android:id="@+id/textViewAge"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="Age (in Years): "
android:layout_marginTop="10dp"
android:textSize="20dp"/>
<TextView
android:id="@+id/textViewDOB"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="Date of Birth: "
android:layout_marginTop="10dp"
android:textSize="20dp"/>
<TextView
android:id="@+id/textViewState"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="State: "
android:layout_marginTop="10dp"
android:textSize="20dp"/>

</LinearLayout>
</ScrollView>

6b. Open MainActivity.java and write Code


package com.example.week4_257;

import androidx.appcompat.app.AppCompatActivity;
import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;

17
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import java.util.Calendar;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.Html;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements
DatePickerDialog.OnDateSetListener {
Student student;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

student = new Student();


FillState();
}
public void SaveData(View view){
EditText editTextName = findViewById(R.id.editTextName);
EditText editTextPassword = findViewById(R.id.editTextPassword);
EditText editTextAddress = findViewById(R.id.editTextAddress);
EditText editTextAge = findViewById(R.id.editTextAge);
EditText editTextDOB = findViewById(R.id.editTextDOB);
student.Name = editTextName.getText().toString();
student.Password = editTextPassword.getText().toString();
student.Address = editTextAddress.getText().toString();
student.Gender = GetGender();
student.Age = editTextAge.getText().toString();
student.DOB = editTextDOB.getText().toString();
String name = "Name: <b>" + student.Name + "</b>";
String password = "Password: <b>*****</b>";

String address = "Address: <b>" + student.Address + "</b>";


String gender = "Gender: <b>" + student.Gender + "</b>";
String age = "Age (in Years): <b>" + student.Age + "</b>";
String dob = "Date of Birth: <b>" + student.DOB + "</b>";
String state = "State: <b>" + student.State + "</b>";
TextView textViewName = findViewById(R.id.textViewName);
textViewName.setText(Html.fromHtml(name));
TextView textViewPassword = findViewById(R.id.textViewPassword);
textViewPassword.setText(Html.fromHtml(password));
TextView textViewAddress = findViewById(R.id.textViewAddress);
textViewAddress.setText(Html.fromHtml(address));
TextView textViewGender = findViewById(R.id.textViewGender);
textViewGender.setText(Html.fromHtml(gender));
TextView textViewAge = findViewById(R.id.textViewAge);
textViewAge.setText(Html.fromHtml(age));
TextView textViewDOB = findViewById(R.id.textViewDOB);
textViewDOB.setText(Html.fromHtml(dob));
TextView textViewState = findViewById(R.id.textViewState);
textViewState.setText(Html.fromHtml(state));

}
private String GetGender(){
RadioGroup rbGroupGender = findViewById(R.id.rbGroupGender);
RadioButton selectedRadioButton;

18
int selectedRadioButtonId =
rbGroupGender.getCheckedRadioButtonId();
if (selectedRadioButtonId != -1) {
selectedRadioButton = findViewById(selectedRadioButtonId);
String selectedGender =
selectedRadioButton.getText().toString();
return selectedGender;
}else {
return "";
}
}
private void FillState(){
String[] data = { "Telangana", "Andhra Pradesh"};
Spinner spinnerState = findViewById(R.id.spinnerState);
//Creating the ArrayAdapter instance having the country list
ArrayAdapter aa = new

ArrayAdapter(this,android.R.layout.simple_spinner_item,data);

aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//Setting the ArrayAdapter data on the Spinner
spinnerState.setAdapter(aa);
spinnerState.setOnItemSelectedListener(new

AdapterView.OnItemSelectedListener() {
@Override
public void
onItemSelected(AdapterView<?> adapterView, View view, int
i, long
l) {

student.State = adapterView.getItemAtPosition(i).toString();
}

@Override
public void
onNothingSelected(AdapterView<?> adapterView) {
}
});
}
public void ShowDatePicker(View view){
DatePickerDialog dialog = new DatePickerDialog(
this,
this,
Calendar.getInstance().get(Calendar.YEAR),
Calendar.getInstance().get(Calendar.MONTH),
Calendar.getInstance().get(Calendar.DAY_OF_MONTH));
dialog.show();
}
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int
dayOfMonth) {
monthOfYear++;
student.DOB = dayOfMonth + "-" + monthOfYear + "-" + year;
EditText editTextDOB = findViewById(R.id.editTextDOB);
editTextDOB.setText(student.DOB);

}
}

19
Output:

20
WEEK 5: Create “Say Hello” App using Intent with Object

Objective: Implementation of Intent with Object to create “Say Hello” App


PROCEDURE:
Step 1: Open Android Studio
Step 2: In the main Welcome to Android Studio window, click New Project
Step 3: choose Empty Activity
Step 4: Enter Project Details
Step 5: Create Student.java Class
Step 6: Write the Code to implement Button Click Event in MainActivity
Step 7: Create SecondActivity to Receive Message from MainActivity
Step 8: Create and Select Android Virtual Device (Emulator)
Step 9: Run the App on the Virtual Device

Procedure:
Step 5: Create Student Class
Open Student.java → Write Code
package com.example.week5_257;
import java.io.Serializable;
public class Student implements Serializable {
public String name;
}
Step 6: Design UI in MainActivity
6a. Open activity_main.xml →Select Design View →Add Widgets – EditText and Button
Widget Id Other Attributes

Edit Text editTextName id="@+id/editTextName"


hint="Enter Name"
inputType="text"
Button btnSubmit id="@+id/btnSubmit"
layout_marginTop="8dp"
onClick="SayHello"
text="Say Hello"

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


<androidx.constraintlayout.widget.ConstraintLayout

21
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"
tools:context=".MainActivity">
<EditText
android:id="@+id/editTextName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter Name"
android:inputType="text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<Button
android:id="@+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="SayHello"
android:text="Submit"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.60" />
</androidx.constraintlayout.widget.ConstraintLayout>

22
6b. Open MainActivity.java and write Code
package com.example.week5_257;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void SayHello(View view){
EditText editTextName;
editTextName = findViewById(R.id.editTextName);
Student student = new Student();
student.name = editTextName.getText().toString();
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("KEY",student);
startActivity(intent);
}
}
7a. Open activity_second.xml → Select Design View→ Add Widgets – TextView

Widget Id Other Attributes


Text View textReceived Id=”@+id/textReceived”
text=”Hi”

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


<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"

23
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SecondActivity">
<TextView
android:id="@+id/textReceived"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

7b. Open SecondActivity.java and write Code


package com.example. week5_257;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent = getIntent();
Student student = (Student) intent.getSerializableExtra("KEY");
TextView textReceived = findViewById(R.id.textReceived);
textReceived.setText(student.name);
}
}

24
Output:

25
WEEK 6: Create “Send SMS” App

Objective: Design and Development of Send SMS App using implicit Intent.
Requirements:
1. Design UI with 2 Input Text Controls and 1 Button
2. Write Java Code using Intent
XML Tab Attribute Value

Edit Text Id textMobileNo


hint Enter Mobile No.
input type phone or number
Edit Text Id textMessage
hint Enter Message
input type textMultiline
Button Id btnSubmit
text SEND SMS
onClick SendSMS

activity_main.xml
<?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"
tools:context=".MainActivity"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:orientation="vertical">

<TextView
android:id="@+id/textViewName"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="S.Triveni Reddy"
android:textSize="20dp"
android:inputType="text"/>

<TextView
android:id="@+id/textViewRollNO"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:textSize="20dp"
android:text="2011cs010257"
android:inputType="text"/>

<EditText
android:layout_width="match_parent"

26
android:layout_height="50dp"
android:hint="Enter Mobile No"
android:id="@+id/editTextMobileNo"
android:layout_marginTop="10dp"
android:inputType="phone"/>
<EditText
android:layout_width="match_parent"
android:layout_height="70dp"
android:hint="Enter Message"
android:id="@+id/editTextMessage"
android:inputType="textMultiLine"
android:layout_marginTop="10dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnSubmit"
android:onClick="SendSMS"
android:text="SEND SMS"/>

</LinearLayout>

MainActivity.java
package com.example.sendsms;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void SendSMS(View view) {

TextView textViewName=findViewById(R.id.textViewName);
TextView textViewRollNO=findViewById(R.id.textViewRollNO);

EditText editTextMobileNo=findViewById(R.id.editTextMobileNo);
EditText editTextMessage=findViewById(R.id.editTextMessage);

String mobileNo=editTextMobileNo.getText().toString();
String message =editTextMessage.getText().toString();
Intent intent=new Intent(Intent.ACTION_SENDTO,
Uri.fromParts("sms",mobileNo,null));
intent.putExtra("sms_body",message);
startActivity(intent);

27
}
}

Output:

28
WEEK 7: Working with Fragments

PROCEDURE:
Step 1: Open Android Studio
Step 2: Create New Project
Step 3: Design UI for MainActivity
Step 4: Create Blank Fragments (FirstFragment & SecondFragment)
Step 5: Write Java Code in MainActivity.java
Step 6: Create and Select Android Virtual Device (Emulator)
Step 7: Run the App on the Virtual Device

activity_main.xml
<?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:orientation="vertical"
android:layout_margin="10dp"
tools:context=".MainActivity">
<Button
android:id="@+id/firstFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="First Fragment"
android:textSize="20sp" />
<Button
android:id="@+id/secondFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Second Fragment"

29
android:textSize="20sp" />
<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp" />
</LinearLayout>

MainActivity.java
package com.example.Week6_257;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

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

Button btnFirstFragment, btnSecondFragment;

// get the reference of Button's


btnFirstFragment = findViewById(R.id.firstFragment);
btnSecondFragment = findViewById(R.id.secondFragment);

// perform setOnClickListener event on First Button


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

loadFragment(new FirstFragment());

}
});

// perform setOnClickListener event on Second Button


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

SecondFragment secondFragment = new SecondFragment();

// load Second Fragment

30
Bundle args = new Bundle();
args.putString("Regno", "257");
args.putString("Name","S.Triveni Reddy");

secondFragment.setArguments(args);

loadFragment(secondFragment);
}
});
}

private void loadFragment(android.app.Fragment fragment) {


// create a FragmentManager
FragmentManager fm = getFragmentManager();
// create a FragmentTransaction to begin the transaction and
replace the Fragment
FragmentTransaction fragmentTransaction = fm.beginTransaction();
// replace the FrameLayout with new Fragment
fragmentTransaction.replace(R.id.frameLayout, fragment);
fragmentTransaction.commit(); // save the changes

Output:

31
WEEK 8: Working with Fragments using Radio Button

Objective: Create Mobile App using Fragments with Radio Buttons


PROCEDURE:
Step 1: Open Android Studio
Step 2: Create New Project
Step 3: Design UI for MainActivity
Step 4: Create Blank Fragments (FirstFragment & SecondFragment)
Step 5: Write Java Code in MainActivity.java
Step 6: Create and Select Android Virtual Device (Emulator)
Step 7: Run the App on the Virtual Device

activity_main.xml
<?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"
android:layout_margin="10dp"
tools:context=".MainActivity">
<RadioGroup
android:id="@+id/rbGroupFragment"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:checkedButton="@id/rbOne">
<TextView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="Select Fragment:"
android:textSize="20dp">
</TextView>
<RadioButton
android:id="@+id/rbOne"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="One"
android:tag="1"
android:layout_marginLeft="10dp"/>
<RadioButton
android:id="@+id/rbTwo"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="Two"
android:tag="2"

32
android:layout_marginLeft="10dp"/>
<RadioButton
android:id="@+id/rbThree"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="Three"
android:tag="3"
android:layout_marginLeft="10dp"/>
</RadioGroup>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Load Fragment"
android:onClick="LoadSelectedFragment"
></Button>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:id="@+id/frmLayout"
></FrameLayout>

</LinearLayout>

MainActivity.java

package com.example.fragmentsradio;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void LoadSelectedFragment(View view){
RadioGroup rbGroupFragment = findViewById(R.id.rbGroupFragment);
int selectedRadioButtonId =
rbGroupFragment.getCheckedRadioButtonId();
RadioButton selectedRadioButton =
findViewById(selectedRadioButtonId);
String selectedTag = selectedRadioButton.getTag().toString();
if (selectedTag.equals("1")) {
loadFragment(new FirstFragment());
}
else if (selectedTag.equals("2")) {
loadFragment(new SecondFragment());
}
else {
loadFragment(new ThreeFragment());
}
}

33
private void loadFragment(androidx.fragment.app.Fragment fragment) {
// create a FragmentManager
FragmentManager fm = getSupportFragmentManager();
// create a FragmentTransaction to begin the transaction and replace the
Fragment
FragmentTransaction fragmentTransaction = fm.beginTransaction();
// replace the FrameLayout with new Fragment
fragmentTransaction.replace(R.id.frmLayout, fragment);
fragmentTransaction.commit(); // save the changes
}
}

Output:

34

You might also like