0% found this document useful (0 votes)
8 views38 pages

Mobile App Development

The document provides a step-by-step guide for installing Android Studio on a Windows system, creating a 'Hello World' application, and developing a user input application that displays a greeting message. It includes detailed procedures for setting up the Android environment, modifying layout files, and implementing logic in the main activity. Additionally, it outlines the creation of a user form with various input fields and a submit button to display the entered data.

Uploaded by

gg8417881
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views38 pages

Mobile App Development

The document provides a step-by-step guide for installing Android Studio on a Windows system, creating a 'Hello World' application, and developing a user input application that displays a greeting message. It includes detailed procedures for setting up the Android environment, modifying layout files, and implementing logic in the main activity. Additionally, it outlines the creation of a user form with various input fields and a submit button to display the entered data.

Uploaded by

gg8417881
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

1.

Installation of Android studio


Aim :
To Install Android Studio on Windows System
Procedure :

1. Download Android Studio:

Go to the https://developer.android.com/studio

Click on the Download Android Studio button.

Accept the terms and conditions and start the download.

2. Install Android Studio:

Windows:

a. Run the downloaded .exe file.


b. Click Next on the setup wizard and follow the on-screen instructions.
c. Choose the components you want to install (Android Virtual Device, Android
SDK).
d. Click Next and select the installation location.
e. Click Install and wait for the installation to complete.
f. Click Finish to launch Android Studio.
3. First-Time Setup:
a. When you open Android Studio for the first time, it will guide you through the
setup wizard.
b. Select the standard setup type for the default recommended settings.
c. Download any additional components like SDK, platform tools, and emulator
images as prompted.
4. Verify the Installation:

After setup, click on New Project to verify the IDE is working correctly.

Choose a basic activity and click Finish to create a sample project.

5. Update SDK and Tools (if needed):

Go to File > Settings > Appearance & Behavior > System Settings > Android SDK.

Select the SDK Platforms tab, choose the latest version, and click Apply.
Output :

Result :

Hence the Program of Installing Android Studio on Windows System has


been Installed Successfully.
2. Hello World Application in Android Studio

Aim : To Create a Hello World Application using Android Studio

Program :

1. Locate Main Files:

MainActivity.java (or MainActivity.kt): This is the main Java/Kotlin file containing


the logic of your app. You can find it under app > java > com.example.helloworldapp.

activity_main.xml: This is the layout file where you define your UI elements. You can
find it under app > res > layout.

2. Modify the Layout File (activity_main.xml):

By default, a TextView saying "Hello World!" is already placed in the layout file.

You can customize the text or add additional UI components as needed.

<!-- res/layout/activity_main.xml -->

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent">

<TextView

android:id="@+id/helloWorldText"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Hello World!"

android:layout_centerInParent="true"

android:textSize="24sp"/>

</RelativeLayout>

3. Modify the MainActivity File (MainActivity.java )

Usually, no changes are needed unless you want to add functionality to the button or
other elements.
Here's a basic example of MainActivity.java:

// src/main/java/com/example/helloworldapp/MainActivity.java

package com.example.helloworldapp;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

4. Run the Application:

• Connect an Android device via USB or start an Android Emulator.


• Click the Run button (green triangle) at the top of the Android Studio window or press
Shift + F10.
• Select your device/emulator, and the app will build and install.

Output :

Result :

Hence the Program of Creating an Hello World Application using


Android Studio has been Executed Successfully.
3. Application that takes the name from a text box and shows hello message
along with the name entered in text box, when the user clicks the OK button

Aim :

To Create a application that takes the name from a text box and shows hello message
along with the name entered in text box, when the user clicks the OK button

Program :

1. Create a New Project in Android Studio:

Open Android Studio.

Click "New Project".

Select "Empty Activity" and click "Next".

Configure the project with the following details:

a. Name: Enter a name for your app (e.g., HelloNameApp).


b. Package Name: Auto-generated based on your app name (e.g.,
com.example.hellonameapp).
c. Language: Choose Java or Kotlin.
d. Click "Finish".

2. Modify the Layout File (activity_main.xml):

Go to res > layout > activity_main.xml.

Add a TextView, EditText, and Button inside the layout.

Here's the complete code for activity_main.xml:


<!-- res/layout/activity_main.xml -->

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent">

<!-- EditText for entering the name -->

<EditText

android:id="@+id/editTextName"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:hint="Enter your name"

android:layout_centerHorizontal="true"

android:layout_marginTop="100dp"

android:inputType="textPersonName"/>

<!-- Button to submit the name -->

<Button

android:id="@+id/buttonOK"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="OK"

android:layout_below="@id/editTextName"

android:layout_centerHorizontal="true"

android:layout_marginTop="20dp"/>

<!-- TextView to display the hello message -->

<TextView

android:id="@+id/textViewMessage"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@id/buttonOK"

android:layout_centerHorizontal="true"

android:layout_marginTop="20dp"

android:textSize="18sp"/>

</RelativeLayout>
3. Modify the Main Activity (MainActivity.java):

• Navigate to java > com.example.hellonameapp > MainActivity.java (or MainActivity.kt).


• Add logic to read the name from the EditText and display the greeting message in the
TextView when the button is clicked.

Java Code (MainActivity.java):

// src/main/java/com/example/hellonameapp/MainActivity.java
package com.example.hellonameapp;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private EditText editTextName;
private TextView textViewMessage;
private Button buttonOK;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize views
editTextName = findViewById(R.id.editTextName);
textViewMessage = findViewById(R.id.textViewMessage);
buttonOK = findViewById(R.id.buttonOK);
// Set a click listener on the OK button
buttonOK.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Get the name entered in the EditText
String name = editTextName.getText().toString().trim();
// Display the hello message
if (!name.isEmpty()) {
textViewMessage.setText("Hello, " + name + "!");
} else {
textViewMessage.setText("Please enter your name.");
}
}
});
}}
4. Run the Application:

• Click the Run button (green triangle) at the top of the Android Studio or press Shift +
F10.
• Choose your connected device or emulator to run the app.

Output :

Result :
Hence the Program of Creating a application that takes the name
from a text box and shows hello message along with the name entered in text box,
when the user clicks the OK button has been Executed Successfully.
4. Create a screen that has input boxes for User Name, Password, Address,
Gender (radio buttons for male and female), Age (numeric), Date of Birth
(Date Picket), State (Spinner) and a Submit button. On clicking the submit
button, print all the data below the Submit Button

Aim :
To Create Create a screen that has input boxes for User Name, Password,
Address, Gender (radio buttons for male and female), Age (numeric), Date of Birth
(Date Picket), State (Spinner) and a Submit button. On clicking the submit button, print
all the data below the Submit Button.
Program :

Create a New Project in Android Studio:

o Open Android Studio.


o Click "New Project".
o Select "Empty Activity" and click "Next".
o Configure the project with:
▪ Name: Enter a name (e.g., UserFormApp).
▪ Package Name: Auto-generated (e.g., com.example.userformapp).
▪ Language: Choose Java or Kotlin.
▪ Click "Finish".

2. Modify the Layout File (activity_main.xml):


o Go to res > layout > activity_main.xml.
o Define the UI elements for user input and the Submit button.

Here's the complete XML code for activity_main.xml:

<!-- res/layout/activity_main.xml -->


<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<!-- Username Input -->
<EditText
android:id="@+id/editTextUserName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter User Name"
android:inputType="textPersonName" />
<!-- Password Input -->
<EditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Password"
android:inputType="textPassword"
android:layout_marginTop="10dp"/>
<!-- Address Input -->
<EditText
android:id="@+id/editTextAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Address"
android:inputType="textPostalAddress"
android:layout_marginTop="10dp"/>
<!-- Gender Radio Group -->
<RadioGroup
android:id="@+id/radioGroupGender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<RadioButton
android:id="@+id/radioButtonMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male" />
<RadioButton
android:id="@+id/radioButtonFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female" />
</RadioGroup>
<!-- Age Input -->
<EditText
android:id="@+id/editTextAge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Age"
android:inputType="number"
android:layout_marginTop="10dp"/>
<!-- Date of Birth Picker -->
<EditText
android:id="@+id/editTextDOB"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Select Date of Birth"
android:focusable="false"
android:layout_marginTop="10dp"/>
<!-- State Spinner -->
<Spinner
android:id="@+id/spinnerState"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"/>
<!-- Submit Button -->
<Button
android:id="@+id/buttonSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:layout_gravity="center"
android:layout_marginTop="20dp"/>
<!-- TextView to display user data -->
<TextView
android:id="@+id/textViewResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:layout_marginTop="20dp" />

</LinearLayout>
</ScrollView>

3. Modify the MainActivity File (MainActivity.java):

• Add logic to capture user input when the Submit button is clicked and display the
entered information.

Java Code (MainActivity.java):

// src/main/java/com/example/userformapp/MainActivity.java
package com.example.userformapp;
import android.app.DatePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
private EditText editTextUserName, editTextPassword, editTextAddress,
editTextAge, editTextDOB;
private RadioGroup radioGroupGender;
private Spinner spinnerState;
private Button buttonSubmit;
private TextView textViewResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize views
editTextUserName = findViewById(R.id.editTextUserName);
editTextPassword = findViewById(R.id.editTextPassword);
editTextAddress = findViewById(R.id.editTextAddress);
editTextAge = findViewById(R.id.editTextAge);
editTextDOB = findViewById(R.id.editTextDOB);
radioGroupGender = findViewById(R.id.radioGroupGender);
spinnerState = findViewById(R.id.spinnerState);
buttonSubmit = findViewById(R.id.buttonSubmit);
textViewResult = findViewById(R.id.textViewResult);
// Set up the state spinner with sample states
ArrayAdapter<CharSequence> adapter =
ArrayAdapter.createFromResource(this,
R.array.states_array,
android.R.layout.simple_spinner_item);adapter.setDropDownViewResource(an
droid.R.layout.simple_spinner_dropdown_item);
spinnerState.setAdapter(adapter);
// Set up Date Picker dialog for Date of Birth
editTextDOB.setOnClickListener(v -> showDatePickerDialog());
// Set up Submit button click listener
buttonSubmit.setOnClickListener(v -> displayUserData());
}
// Function to show DatePicker dialog
private void showDatePickerDialog() {
final Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(this,
(view, year1, monthOfYear, dayOfMonth) ->
editTextDOB.setText(dayOfMonth + "/" + (monthOfYear + 1) + "/" + year1),
year, month, day);
datePickerDialog.show();
}
// Function to display user data below the Submit button
private void displayUserData() {
String userName = editTextUserName.getText().toString();
String password = editTextPassword.getText().toString();
String address = editTextAddress.getText().toString();
String age = editTextAge.getText().toString();
String dob = editTextDOB.getText().toString();
String gender = ((RadioButton)
findViewById(radioGroupGender.getCheckedRadioButtonId())).getText().toStr
ing();
String state = spinnerState.getSelectedItem().toString();

// Display all the user-entered data


textViewResult.setText("User Name: " + userName + "\n" +
"Password: " + password + "\n" +
"Address: " + address + "\n" +
"Gender: " + gender + "\n" +
"Age: " + age + "\n" +
"Date of Birth: " + dob + "\n" +
"State: " + state);
}
}

4. Define States Array in strings.xml:

• Add the list of states to res > values > strings.xml:

<!-- res/values/strings.xml -->


<resources>
<string name="app_name">UserFormApp</string>
<string-array name="states_array">
<item>California</item>
<item>Texas</item>
<item>New York</item>
<item>Florida</item>
<item>Illinois</item>
</string-array>
</resources>

5. Run the Application:

• Click the Run button in Android Studio or press Shift + F10.


• Choose your emulator or connected device.

Output :

Result :
Hence the Program of Creating a screen that has input boxes for User
Name, Password, Address, Gender (radio buttons for male and female),
Age (numeric), Date of Birth (Date Picket), State (Spinner) and a Submit
button. On clicking the submit button, print all the data below the Submit
Button has been Executed Successfully.
5. Design an android application to create page using Intent and one
Button and pass the Values from one Activity to second Activity.

Aim :

To Design an android application to create page using Intent and one Button and
pass the Values from one Activity to second Activity.

Procedure:

1. Create a New Project:

• Open Android Studio.


• Click on New Project > Empty Activity and click Next.
• Name the project (e.g., IntentExampleApp).
• Choose Java or Kotlin as the language and click Finish.

2. Create a Second Activity:

• Go to File > New > Activity > Empty Activity to create a second activity.
• Name the second activity as SecondActivity.java (for Java) or SecondActivity.kt (for
Kotlin).
• Ensure that SecondActivity.java is automatically added to the AndroidManifest.xml.

3. Modify the Layout of the First Activity:

<!-- res/layout/activity_main.xml -->


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<!-- Input field for name -->
<EditText
android:id="@+id/editTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Your Name"
android:inputType="textPersonName"
android:layout_marginBottom="16dp"/>
<!-- Button to send data to the second activity -->
<Button
android:id="@+id/buttonSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"
android:layout_gravity="center"/>
</LinearLayout>
4. Modify the Layout of the Second Activity:

<!-- res/layout/activity_second.xml -->


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<!-- TextView to display the received name -->
<TextView
android:id="@+id/textViewResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"/>
</LinearLayout>

5. Modify the MainActivity.java:

// src/main/java/com/example/intentexampleapp/MainActivity.java
package com.example.intentexampleapp;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private EditText editTextName;
private Button buttonSend;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize views
editTextName = findViewById(R.id.editTextName);
buttonSend = findViewById(R.id.buttonSend);
// Set onClickListener for the button to send data
buttonSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Get the name from EditText
String name = editTextName.getText().toString();
// Create an Intent to navigate to the SecondActivity
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
// Pass the name using putExtra
intent.putExtra("user_name", name);
// Start SecondActivity
startActivity(intent);
}
});
}
}
6. Modify the SecondActivity.java:

// src/main/java/com/example/intentexampleapp/SecondActivity.java
package com.example.intentexampleapp;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class SecondActivity extends AppCompatActivity {
private TextView textViewResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
// Initialize TextView
textViewResult = findViewById(R.id.textViewResult);
// Get the Intent that started this activity
Intent intent = getIntent();
// Retrieve the name from the Intent extras
String userName = intent.getStringExtra("user_name");
// Display the name in the TextView
textViewResult.setText("Hello, " + userName);
}
}

7. Update AndroidManifest.xml:

<!-- res/AndroidManifest.xml -->


<application
... >
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Declare SecondActivity -->
<activity android:name=".SecondActivity" />
</application>

8. Run the Application:

• Click the Run button or press Shift + F10 to run the application.
• Choose your emulator or connected device.
Output :

Result :
Hence the Program of Designing an android application to
create page using Intent and one Button and pass the Values from one
Activity to second Activity has been Executed Successfully.
6. Design an android application Send SMS using Internet

Aim :

To Design an android application Send SMS using Internet

1. Set Up Twilio Account:

• Step 1: Go to Twilio's website and sign up for a free trial account.


• Step 2: Once you sign up, Twilio will give you an Account SID and an Auth Token.
• Step 3: You will also get a Twilio phone number that you can use to send SMS.

2. Add Internet Permission to AndroidManifest.xml:

<!-- res/AndroidManifest.xml -->


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sendsms">
<uses-permission android:name="android.permission.INTERNET" />
<application
... >
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

3. Add Dependencies for HTTP Requests:

// build.gradle (Module: app)


dependencies {
implementation 'com.squareup.okhttp3:okhttp:4.9.1'
}
5. Design the Layout (activity_main.xml):

<!-- res/layout/activity_main.xml -->


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 5.
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<!-- Input for recipient's phone number -->
<EditText
android:id="@+id/editTextPhoneNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Phone Number"
android:inputType="phone"
android:layout_marginBottom="16dp"/>
<!-- Input for the message -->
<EditText
android:id="@+id/editTextMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Message"
android:inputType="text"
android:layout_marginBottom="16dp"/>
<!-- Button to send SMS -->
<Button
android:id="@+id/buttonSendSMS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send SMS"
android:layout_gravity="center"/>
</LinearLayout>

Implement the Java Code to Send SMS Using Twilio API:


// src/main/java/com/example/intentexampleapp/MainActivity.java
package com.example.intentexampleapp;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private EditText editTextName;
private Button buttonSend;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize views
editTextName = findViewById(R.id.editTextName);
buttonSend = findViewById(R.id.buttonSend);
// Set onClickListener for the button to send data
buttonSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Get the name from EditText
String name = editTextName.getText().toString();
// Create an Intent to navigate to the SecondActivity
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
// Pass the name using putExtra
intent.putExtra("user_name", name);
// Start SecondActivity
startActivity(intent);
}
});
}
}
7. Update AndroidManifest.xml:

<!-- res/AndroidManifest.xml -->


<application
... >
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Declare SecondActivity -->
<activity android:name=".SecondActivity" />
</application>

8. Run the Application:

Click the Run button or press Shift + F10 to run the application.

Choose your emulator or connected device.

Output :

No Output.
Result :

Hence the Program of Designing Design an android application Send


SMS using Internet has been Sent Successfully.

7. Create an android application using Fragments

Aim :

1. To Create an android application using Fragments

1. Set Up Android Studio Project

• Open Android Studio and create a new project.


• Choose an Empty Activity template.
• Set your project name and other configurations.

2. Create Fragments

We will create two fragments, FragmentOne and FragmentTwo.


FragmentOne.java (First Fragment)

package com.example.fragmentapp;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class FragmentOne extends Fragment {
public FragmentOne() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_one, container, false);
// Button to switch fragments
Button button = view.findViewById(R.id.buttonGoToFragmentTwo);
button.setOnClickListener(v -> {
// Fragment transaction to replace FragmentOne with FragmentTwo
FragmentTwo fragmentTwo = new FragmentTwo();
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, fragmentTwo)
.addToBackStack(null)
.commit();
});
return view;
}
}
FragmentTwo.java (Second Fragment)

package com.example.fragmentapp;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentTwo extends Fragment {
public FragmentTwo() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_two, container, false);
}
}

MainActivity.java

In MainActivity, we'll load the first fragment (FragmentOne) when the activity starts.

package com.example.fragmentapp;
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);

// Load FragmentOne when the activity starts


if (savedInstanceState == null) {
FragmentOne fragmentOne = new FragmentOne();
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, fragmentOne)
.commit();
}
}
}
Layout Files
activity_main.xml

This layout defines a container (FrameLayout) where fragments will be dynamically loaded.

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


<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />

fragment_one.xml

This is the layout for FragmentOne, containing a button that switches to FragmentTwo.

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="16dp">

<!-- Button to go to FragmentTwo -->


<Button
android:id="@+id/buttonGoToFragmentTwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go to Fragment Two" />
</LinearLayout>

fragment_two.xml

This is the layout for FragmentTwo, which will replace FragmentOne.

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">

<!-- Simple Text in FragmentTwo -->


<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is Fragment Two"
android:textSize="24sp"/>
</LinearLayout>
Run the Application:

• When the app is launched, FragmentOne will be shown with a button.


• Clicking the button will replace FragmentOne with FragmentTwo.

Output :

Result :
Hence the Program of Create an android application using
Fragments has been Executed Successfully.

8. Create an Android Application Using Radio Buttons

Aim :

To Create an Android Application Using Radio Buttons

1. Set Up Android Studio Project

• Open Android Studio and create a new project.


• Choose an Empty Activity template.
• Set your project name and other configurations.

2. Add Radio Buttons in the Layout

We will use RadioGroup to group the radio buttons and ensure only one option can be selected
at a time. A button will trigger the action to display the selected radio button's value.

MainActivity.java

package com.example.radiobuttonapp;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
RadioGroup radioGroup;
RadioButton selectedRadioButton;
Button buttonSubmit;
TextView textViewResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroup = findViewById(R.id.radioGroup);
buttonSubmit = findViewById(R.id.buttonSubmit);
textViewResult = findViewById(R.id.textViewResult);
// Set OnClickListener on the Submit button
buttonSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Get the selected radio button ID from the RadioGroup
int selectedId = radioGroup.getCheckedRadioButtonId();
// Check if any radio button is selected
if (selectedId != -1) {
// Find the selected RadioButton by its ID
selectedRadioButton = findViewById(selectedId);
String selectedOption = selectedRadioButton.getText().toString();
// Display the selected option in the TextView
textViewResult.setText("Selected: " + selectedOption);
} else {
// Show a toast if no option is selected
Toast.makeText(MainActivity.this, "Please select an option",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
activity_main.xml

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<!-- RadioGroup to group the RadioButtons -->
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- RadioButton options -->
<RadioButton
android:id="@+id/radioButtonOption1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 1" />
<RadioButton
android:id="@+id/radioButtonOption2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 2" />
<RadioButton
android:id="@+id/radioButtonOption3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 3" />
</RadioGroup>
<!-- Submit Button -->
<Button
android:id="@+id/buttonSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
<!-- TextView to display the selected option -->
<TextView
android:id="@+id/textViewResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Selected: None"
android:textSize="18sp"
android:paddingTop="20dp" />
</LinearLayout>

Run the Application:

1. The user sees three radio button options.


2. The user selects one and clicks the Submit button.
3. The selected option is displayed below the button.
Output :

Result :
Hence the Program of Android Application Using Radio Buttons
has been Executed Successfully.
9. Design an android application with menu

Aim :

To Design an android application with menu

1. Set Up Android Studio Project

• Open Android Studio and create a new project.


• Choose an Empty Activity template.
• Set your project name and other configurations.

2. Create Menu Resource

We will define the menu items in an XML file stored in the res/menu folder.

3. Add Menu in the Activity

The menu is added to the toolbar in the activity, and clicking the menu items will trigger
specific actions.

MainActivity.java

package com.example.menuapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.settings:
Toast.makeText(this, "Settings selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.about:
Toast.makeText(this, "About selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.help:
Toast.makeText(this, "Help selected", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Create the Menu XML File

1. Right-click on the res folder.


2. Create a new directory called menu under res.
3. Right-click on the menu folder and create a new menu resource file called main_menu.xml.

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


<menu xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Settings menu item -->
<item
android:id="@+id/settings"
android:title="Settings"
android:orderInCategory="100"
android:showAsAction="never" />
<!-- About menu item -->
<item
android:id="@+id/about"
android:title="About"
android:orderInCategory="101"
android:showAsAction="never" />
<!-- Help menu item -->
<item
android:id="@+id/help"
android:title="Help"
android:orderInCategory="102"
android:showAsAction="never" />
</menu>

activity_main.xml

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:padding="16dp">

<TextView

android:id="@+id/textView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Hello! This is the main screen."

android:textSize="24sp"

android:layout_centerInParent="true"/>

</RelativeLayout>
Run the Application:

1. When the app runs, you'll see the main screen with a TextView.
2. Tap the three-dot menu in the top-right corner to see the menu options (Settings, About,
Help).
3. When you tap one of the menu options, a Toast message displays the corresponding action
(e.g., "Settings selected").

Output :

Result :
Hence the Program of Design an android application with menu
has been Executed Successfully.
10. Create a user registration application that stores the user details in a
database table.

Aim :

To Create a Create a user registration application that stores the user details in a
database table.

1. Set Up Android Studio Project

• Open Android Studio and create a new project.


• Choose an Empty Activity template.
• Set your project name and other configurations.
2. Add the Database Helper Class
This class will manage the SQLite database, creating tables and storing user data.
3. Design the Layout for User Registration
The registration form will have input fields like username, password, email, and phone
number.
4. Insert Data into the Database
When the user clicks the "Register" button, their details will be stored in the database.

MainActivity.java
package com.example.registrationapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText username, password, email, phone;
Button registerButton;
DatabaseHelper databaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize views
username = findViewById(R.id.editTextUsername);
password = findViewById(R.id.editTextPassword);
email = findViewById(R.id.editTextEmail);
phone = findViewById(R.id.editTextPhone);
registerButton = findViewById(R.id.buttonRegister);
// Initialize database helper
databaseHelper = new DatabaseHelper(this);
// Set onClickListener for the register button
registerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String user = username.getText().toString();
String pass = password.getText().toString();
String mail = email.getText().toString();
String phoneNo = phone.getText().toString();
// Check if any field is empty
if(user.isEmpty() || pass.isEmpty() || mail.isEmpty() || phoneNo.isEmpty()) {
Toast.makeText(MainActivity.this, "All fields are required", Toast.LENGTH_SHORT).show();
} else {
// Insert data into database
boolean isInserted = databaseHelper.insertUserData(user, pass, mail, phoneNo);
if(isInserted) {
Toast.makeText(MainActivity.this, "User Registered Successfully", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Registration Failed", Toast.LENGTH_SHORT).show();
}
}
}
});
}
}
DatabaseHelper.java

package com.example.registrationapp;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
// Database name and version
private static final String DATABASE_NAME = "UserDB";
private static final int DATABASE_VERSION = 1;
// Table and column names
private static final String TABLE_NAME = "users";
private static final String COLUMN_ID = "id";
private static final String COLUMN_USERNAME = "username";
private static final String COLUMN_PASSWORD = "password";
private static final String COLUMN_EMAIL = "email";
private static final String COLUMN_PHONE = "phone";
// Constructor
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// Create users table
String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ COLUMN_USERNAME + " TEXT,"
+ COLUMN_PASSWORD + " TEXT,"
+ COLUMN_EMAIL + " TEXT,"
+ COLUMN_PHONE + " TEXT" + ")";
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
// Create tables again
onCreate(db);
}
// Insert user data into database
public boolean insertUserData(String username, String password, String email, String phone) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COLUMN_USERNAME, username);
contentValues.put(COLUMN_PASSWORD, password);
contentValues.put(COLUMN_EMAIL, email);
contentValues.put(COLUMN_PHONE, phone);
// Insert data into table
long result = db.insert(TABLE_NAME, null, contentValues);
db.close();
// Check if insertion is successful
return result != -1;
}
}
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

android:padding="16dp">

<!-- Username Input -->

<EditText

android:id="@+id/editTextUsername"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="Username" />

<!-- Password Input -->

<EditText

android:id="@+id/editTextPassword"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="Password"

android:inputType="textPassword" />

<!-- Email Input -->

<EditText

android:id="@+id/editTextEmail"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="Email"

android:inputType="textEmailAddress" />

<!-- Phone Number Input -->

<EditText

android:id="@+id/editTextPhone"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="Phone Number"

android:inputType="phone" />

<!-- Register Button -->

<Button

android:id="@+id/buttonRegister"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Register" />

</LinearLayout>
Run the Application:

1. The user enters their details (username, password, email, and phone number).
2. The user clicks the Register button.
3. If all fields are filled, the app stores the user’s data in the SQLite database and displays a
success message.

Output :

Result :
Hence the Program of Create a user registration application that
stores the user details in a database table has been Executed Successfully.

You might also like