0% found this document useful (0 votes)
0 views11 pages

Mobile and Application Development

The document outlines three assignments focused on Android development. Assignment 1 involves creating an app to calculate the power of a number, Assignment 2 covers implementing Google Maps in an app, and Assignment 3 details how to send SMS messages from an Android application. Each assignment includes steps for setting up the project, designing layouts, implementing Java logic, and handling permissions.

Uploaded by

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

Mobile and Application Development

The document outlines three assignments focused on Android development. Assignment 1 involves creating an app to calculate the power of a number, Assignment 2 covers implementing Google Maps in an app, and Assignment 3 details how to send SMS messages from an Android application. Each assignment includes steps for setting up the project, designing layouts, implementing Java logic, and handling permissions.

Uploaded by

ahmadrazasqa
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Assignment 1

Solution
Program to take base and power from the user and displays its result
1. Create a New Android Project:

 Open Android Studio and create a new project.

 Choose a "Basic Activity" template.

2. Design the Layout (activity_main.xml):

You will need an input field for the base, an input field for the power, and a button to perform the calculation. Also, a
TextView will display the result.

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


<LinearLayout
android:orientation="vertical"
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".PhoneCall">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/etxPhone"
android:hint="Enter Mobile Number"
android:textSize="30sp"
android:layout_marginTop="30dp"
android:inputType="phone"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnPhone"
android:text="Call"
android:textSize="30sp"
android:layout_gravity="center"
android:layout_marginTop="20dp"/>

</LinearLayout>
3. Implement Java Logic in [Link]:

In this part, you will get the base and power values from the EditText fields, calculate the result, and display it in the
TextView.

package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class ExampleProgram extends AppCompatActivity {
EditText etbase,etpower;
TextView txtResult;
Button btnResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
[Link](this);
setContentView([Link].activity_example_program);
etbase = findViewById([Link]);
etpower = findViewById([Link]);
txtResult = findViewById([Link]);
btnResult = findViewById([Link]);
[Link](new [Link]() {
@Override
public void onClick(View v) {
String base = [Link]().toString();
String power = [Link]().toString();
if([Link]()||[Link]()){
[Link]([Link], "Enter Base and Power", Toast.LENGTH_SHORT).show();
}
else{
Double b = [Link](base);
Double p = [Link](power);
Double result = [Link](b,p);
[Link]("Result is = " + result);
}
} }); }
}
Assignment 2
Solution
Implementation Of GoogleMap
1. Set up Google Maps SDK for Android

Before you start, ensure that you have set up the Google Maps API in your project.

Step 1: Create a Google Cloud Project

 Go to the Google Cloud Console: [Link]

 Create a new project (or select an existing one).

 Enable the Google Maps Android API by searching for "Maps SDK for Android" and enabling it.

 Generate an API Key under APIs & Services > Credentials.

Step 2: Add the API Key to Your Android Project

 Open your project in Android Studio.

 Open android/app/src/main/res/values/[Link] and add your Google Maps API key:

<resources>

<string name="google_maps_key">YOUR_API_KEY</string>

</resources>

Step 3: Modify [Link]

 Open the [Link] file and add the following permissions and meta-data inside the
<application> tag:

<uses-permission android:name="[Link].ACCESS_FINE_LOCATION"/>

<uses-permission android:name="[Link].ACCESS_COARSE_LOCATION"/>

<applicatio... >

<meta-data

android:name="[Link].v2.API_KEY"

android:value="@string/google_maps_key"/>

<activity

android:name=".MapsActivity"

android:label="@string/app_name"

android:theme="@style/[Link]">
<intent-filter>

<action android:name="[Link]"/>

<category android:name="[Link]"/>

</intent-filter>

</activity>

</application>

2. Add Dependencies in [Link]

Open your [Link] file (app-level) and add the Google Maps dependency under dependencies:

dependencies {

implementation '[Link]:play-services-maps:18.0.0'

implementation '[Link]:play-services-location:18.0.0' // For location services

Sync your project after adding the dependencies.

3. Create a Map Activity

Now, create an activity that will display the map. If you are using a new activity, name it MapsActivity.

Step 1: Layout for MapsActivity

Create the XML layout for your map activity, e.g., activity_maps.xml:

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

<[Link]

xmlns:android="[Link]

android:id="@+id/map"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:name="[Link]"/>

Step 2: Initialize the Map in [Link]

Next, you need to initialize and display the map in [Link].

package [Link];

import [Link];

import [Link];

import [Link];
import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;

@Override

protected void onCreate(Bundle savedInstanceState) {

[Link](savedInstanceState);

setContentView([Link].activity_maps);

// Get the SupportMapFragment and request notification when the map is ready to be used.

SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()

.findFragmentById([Link]);

if (mapFragment != null) {

[Link](this); } }

// This method will be called when the map is ready

@Override

public void onMapReady(GoogleMap googleMap) {

mMap = googleMap;

// Add a marker at a specific location (example: Sydney, Australia)

LatLng sydney = new LatLng(-34, 151);

[Link](new MarkerOptions().position(sydney).title("Marker in Sydney"));

// Move the camera to the position of the marker

[Link]([Link](sydney, 10));

// Optionally, you can enable the user's location on the map if needed

if ([Link].SDK_INT >= [Link].VERSION_CODES.M) {

// Add code to handle permission for accessing the device location here.

} }}
4. Handle Permissions for Location (Optional)

If you need to access the user's location, make sure to handle permissions for location. You can use Google's
FusedLocationProviderClient to get the user's current location.

Add this in [Link]:

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

// Inside your onCreate method, check for permissions:

if ([Link](this, [Link].ACCESS_FINE_LOCATION)

== PackageManager.PERMISSION_GRANTED) {

[Link](true);

} else {

if ([Link].SDK_INT >= Build.VERSION_CODES.M) {

[Link](this,

new String[]{[Link].ACCESS_FINE_LOCATION}, 1); }}

Override onRequestPermissionsResult to handle the permission result:

@Override

public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {

[Link](requestCode, permissions, grantResults);

if (requestCode == 1) {

if ([Link] > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

[Link](true);

} else {

[Link](this, "Permission denied", Toast.LENGTH_SHORT).show(); } }}

5. Run the App

Now you can run your app on an emulator or a physical device. When the app launches, it will display the map with a
marker placed at the coordinates for Sydney, Australia. You can replace these coordinates with your desired
location.
Assignment 3

Solution

Send SMS in Android Studio


To send an SMS in an Android application using Java, you need to use the Android SMS Manager. Below is the code
that demonstrates how to send an SMS from an Android app using Java.

Steps to Send SMS in Android:

1. Set Permissions: You need to declare the appropriate permissions in the [Link] file to send
SMS.

2. Implement the Sending SMS Logic in Java Code:

Here’s the step-by-step implementation:

1. Update [Link]:

You need to request permission to send an SMS in your Android app. Add the following permission to the
[Link]:

<uses-permission android:name="[Link].SEND_SMS"/>

<uses-permission android:name="[Link].READ_SMS"/>

<uses-permission android:name="[Link].RECEIVE_SMS"/>

If you want to use a feature like reading incoming messages or receiving messages, these permissions are also
required, but for sending SMS, the first permission is enough.

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

Here’s a simple layout with an EditText for the phone number, a message field, and a Button to send the SMS.

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

<LinearLayout xmlns:android="[Link]

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

android:padding="16dp">

<EditText

android:id="@+id/phoneNumberEditText"
android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="Enter phone number"

android:inputType="phone"/>

<EditText

android:id="@+id/messageEditText"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="Enter message"

android:inputType="text"/>

<Button

android:id="@+id/sendSmsButton"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Send SMS"

android:layout_marginTop="16dp"/>

</LinearLayout>

3. Implement the SMS Sending Logic in [Link]:

In the Java file ([Link]), you need to write the logic to send the SMS when the user clicks the button.

package [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

public class MainActivity extends AppCompatActivity {

private EditText phoneNumberEditText, messageEditText;

private Button sendSmsButton;


@Override

protected void onCreate(Bundle savedInstanceState) {

[Link](savedInstanceState);

setContentView([Link].activity_main);

// Initialize the UI elements

phoneNumberEditText = findViewById([Link]);

messageEditText = findViewById([Link]);

sendSmsButton = findViewById([Link]);

// Set a listener on the Send SMS button

[Link](new [Link]() {

@Override

public void onClick(View v) {

// Get the phone number and message from EditText fields

String phoneNumber = [Link]().toString();

String message = [Link]().toString();

// Check if both fields are filled

if (![Link]() && ![Link]()) {

sendSMS(phoneNumber, message);

} else {

[Link]([Link], "Please enter both phone number and message", Toast.LENGTH_SHORT).show();


}} }}

// Method to send SMS

private void sendSMS(String phoneNumber, String message) {

try {

// Get an instance of the SmsManager

SmsManager smsManager = [Link]();

[Link](phoneNumber, null, message, null, null);

[Link]([Link], "SMS Sent", Toast.LENGTH_SHORT).show();

} catch (Exception e) {

[Link]();

[Link]([Link], "SMS failed to send", Toast.LENGTH_SHORT).show();


}

4. Handle Runtime Permissions (Optional for Android 6.0 and Above):

For Android 6.0 (API level 23) and above, you must request runtime permissions. Below is how you can handle
permissions in your [Link].

Step 1: Add Runtime Permission Request

In the onCreate() method, check if the permission is granted and request it if necessary.

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

@Override

protected void onCreate(Bundle savedInstanceState) {

[Link](savedInstanceState);

setContentView([Link].activity_main);

// Check for SMS permission

if ([Link](this, [Link].SEND_SMS) !=
PackageManager.PERMISSION_GRANTED) {

// Request permission if not granted

[Link](this, new String[]{[Link].SEND_SMS}, 1);

// Initialize UI elements

phoneNumberEditText = findViewById([Link]);

messageEditText = findViewById([Link]);

sendSmsButton = findViewById([Link]);

[Link](new [Link]() {

@Override

public void onClick(View v) {

String phoneNumber = [Link]().toString();


String message = [Link]().toString();

if (![Link]() && ![Link]()) {

sendSMS(phoneNumber, message);

} else {

[Link]([Link], "Please enter both phone number and message",


Toast.LENGTH_SHORT).show() } } });}

@Override

public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {

[Link](requestCode, permissions, grantResults);

if (requestCode == 1) {

if ([Link] > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

// Permission granted, now you can send SMS

[Link](this, "Permission granted", Toast.LENGTH_SHORT).show();

} else {

// Permission denied

[Link](this, "Permission denied", Toast.LENGTH_SHORT).show();

5. Run the Application:

 After implementing the above steps, you can now build and run your app.

 When you enter a phone number and message and click "Send SMS," the app will attempt to send the SMS
message.

You might also like