MAD External Solution v1
MAD External Solution v1
1. Write a program to place Name, Age and mobile No. linearly (Vertical) on the
display screen using Linear Layout.
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="24dp"
android:gravity="center">
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name: Mobile Application Development"
android:textSize="20sp" />
<TextView
android:id="@+id/tvAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Age: 25"
android:textSize="20sp" />
<TextView
android:id="@+id/tvMobile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mobile No: 1234567890"
android:textSize="20sp" />
</LinearLayout>
MainActivity.java
package com.example.index;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Index"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
Output:
2. Write a program, to display all the data types in object-oriented programming in
FrameLayout
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="24dp">
<TextView
android:id="@+id/type1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Class"
android:textSize="20sp"
android:layout_marginTop="0dp" />
<TextView
android:id="@+id/type2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Object"
android:textSize="20sp"
android:layout_marginTop="40dp" />
<TextView
android:id="@+id/type3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Interface"
android:textSize="20sp"
android:layout_marginTop="80dp" />
<TextView
android:id="@+id/type4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Method"
android:textSize="20sp"
android:layout_marginTop="120dp" />
<TextView
android:id="@+id/type5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Constructor"
android:textSize="20sp"
android:layout_marginTop="160dp" />
</FrameLayout>
MainActivity.java
package com.example.index;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Index"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
Output:
3. Write a program to create a simple calculator.
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="24dp"
android:gravity="center">
<EditText
android:id="@+id/num1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter first number"
android:inputType="numberDecimal" />
<EditText
android:id="@+id/num2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter second number"
android:inputType="numberDecimal" />
<Button
android:id="@+id/btnAdd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add" />
<Button
android:id="@+id/btnSub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Subtract" />
<Button
android:id="@+id/btnMul"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Multiply" />
<Button
android:id="@+id/btnDiv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Divide" />
<TextView
android:id="@+id/tvResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Result:"
android:textSize="20sp"
android:layout_marginTop="20dp" />
</LinearLayout>
MainActivity.java
package com.example.index;
import android.os.Bundle;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
num1 = findViewById(R.id.num1);
num2 = findViewById(R.id.num2);
tvResult = findViewById(R.id.tvResult);
btnAdd = findViewById(R.id.btnAdd);
btnSub = findViewById(R.id.btnSub);
btnMul = findViewById(R.id.btnMul);
btnDiv = findViewById(R.id.btnDiv);
btnAdd.setOnClickListener(v -> {
double a = Double.parseDouble(num1.getText().toString());
double b = Double.parseDouble(num2.getText().toString());
tvResult.setText("Result: " + (a + b));
});
btnSub.setOnClickListener(v -> {
double a = Double.parseDouble(num1.getText().toString());
double b = Double.parseDouble(num2.getText().toString());
tvResult.setText("Result: " + (a - b));
});
btnMul.setOnClickListener(v -> {
double a = Double.parseDouble(num1.getText().toString());
double b = Double.parseDouble(num2.getText().toString());
tvResult.setText("Result: " + (a * b));
});
btnDiv.setOnClickListener(v -> {
double a = Double.parseDouble(num1.getText().toString());
double b = Double.parseDouble(num2.getText().toString());
tvResult.setText("Result: " + (a / b));
});
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Index"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
Output:
4. Write a program to show the following output. First two radio buttons are without
using radio button group and next two radio buttons are using radio group. Note
the change between these two. Also toast which button has been selected.
activity_main.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="24dp">
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 1" />
<RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 2" />
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/radio3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 3" />
<RadioButton
android:id="@+id/radio4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 4" />
</RadioGroup>
</LinearLayout>
</ScrollView>
MainActivity.java
package com.example.index;
import android.os.Bundle;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radio1 = findViewById(R.id.radio1);
radio2 = findViewById(R.id.radio2);
radio3 = findViewById(R.id.radio3);
radio4 = findViewById(R.id.radio4);
radioGroup = findViewById(R.id.radioGroup);
radio1.setOnClickListener(v ->
Toast.makeText(this, "Selected: Option 1", Toast.LENGTH_SHORT).show());
radio2.setOnClickListener(v ->
Toast.makeText(this, "Selected: Option 2", Toast.LENGTH_SHORT).show());
AndroidManifest.xml
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Index"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Output:
5. Write a program to display an image using Image View and Button named as
“Change Image”. Once you click on button another image should get displayed.
Step:
Put two image files (image1.png and image2.png) in the res/drawable folder.
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:gravity="center"
android:padding="24dp">
<ImageView
android:id="@+id/imageView"
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@drawable/image1"
android:contentDescription="Image" />
<Button
android:id="@+id/btnChange"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Image"
android:layout_marginTop="20dp" />
</LinearLayout>
MainActivity.java
package com.example.index;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
ImageView imageView;
Button btnChange;
boolean isImage1 = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.imageView);
btnChange = findViewById(R.id.btnChange);
btnChange.setOnClickListener(v -> {
if (isImage1) {
imageView.setImageResource(R.drawable.image2);
} else {
imageView.setImageResource(R.drawable.image1);
}
isImage1 = !isImage1;
});
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Index"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Output:
6. WAP to display following output. Select and display date and time on click of
“select date” and “select time” respectively.
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="24dp"
android:gravity="center">
<Button
android:id="@+id/btnDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Date" />
<Button
android:id="@+id/btnTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Time"
android:layout_marginTop="16dp" />
<TextView
android:id="@+id/tvResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Selected Date and Time"
android:textSize="18sp"
android:layout_marginTop="24dp" />
</LinearLayout>
MainActivity.java
package com.example.index;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Calendar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnDate = findViewById(R.id.btnDate);
btnTime = findViewById(R.id.btnTime);
tvResult = findViewById(R.id.tvResult);
btnDate.setOnClickListener(v -> {
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
btnTime.setOnClickListener(v -> {
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
AndroidManifest.xml
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Index"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
Output:
7. Write a program to create a text field and a button “Navigate”. When you enter
www.google.com and press navigate button it should open google page.
activity_main.xml
<EditText
android:id="@+id/editUrl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter website URL"
android:text="www.google.com"
android:inputType="textUri" />
<Button
android:id="@+id/btnNavigate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Navigate"
android:layout_marginTop="20dp" />
</LinearLayout>
MainActivity.java
package com.example.index;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
EditText editUrl;
Button btnNavigate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editUrl = findViewById(R.id.editUrl);
btnNavigate = findViewById(R.id.btnNavigate);
btnNavigate.setOnClickListener(v -> {
String url = editUrl.getText().toString();
if (!url.startsWith("http://") && !url.startsWith("https://")) {
url = "http://" + url;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
});
}
}
AndroidManifest.xml
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Index"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Output:
8. Write a program to display the list of sensors supported by the mobile device.
activity_main.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="24dp">
<TextView
android:id="@+id/tvSensors"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sensors will be listed here..."
android:textSize="16sp" />
</LinearLayout>
</ScrollView>
MainActivity.java
package com.example.index;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.List;
TextView tvSensors;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvSensors = findViewById(R.id.tvSensors);
SensorManager sensorManager = (SensorManager)
getSystemService(SENSOR_SERVICE);
List<Sensor> sensorList = sensorManager.getSensorList(Sensor.TYPE_ALL);
tvSensors.setText(sensorInfo.toString());
}
}
AndroidManifest.xml
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Index"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
Output:
9. Write a program to record a video using various camera methods.
(not working properly )
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">
<VideoView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginBottom="16dp"/>
<Button
android:id="@+id/recordButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Record Video"/>
<Button
android:id="@+id/playButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Play Video"/>
</LinearLayout>
MainActivity.java
package com.example.index;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.Button;
import android.widget.Toast;
import android.widget.VideoView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
videoView = findViewById(R.id.videoView);
recordButton = findViewById(R.id.recordButton);
playButton = findViewById(R.id.playButton);
recordButton.setOnClickListener(v -> {
if (allPermissionsGranted()) {
dispatchTakeVideoIntent();
} else {
Toast.makeText(this, "Permissions not granted",
Toast.LENGTH_SHORT).show();
}
});
playButton.setOnClickListener(v -> {
if (videoUri != null) {
videoView.setVideoURI(videoUri);
videoView.start();
} else {
Toast.makeText(this, "No video recorded yet",
Toast.LENGTH_SHORT).show();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (requestCode == REQUEST_VIDEO_CAPTURE && resultCode == RESULT_OK) {
videoUri = intent.getData();
Toast.makeText(this, "Video saved", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[]
permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_PERMISSIONS) {
if (allPermissionsGranted()) {
Toast.makeText(this, "Permissions granted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Permissions not granted",
Toast.LENGTH_SHORT).show();
}
}
}
}
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.index">
<uses-feature android:name="android.hardware.camera.front"
android:required="false" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Index">
<activity
android:name=".MainActivity"
android:label="Record Video"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Output:
10. Write a program to the login form & display login Successful /Unsuccessful
toast message.
Username: user
Password: password
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">
</LinearLayout>
MainActivity.java
package com.example.index;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize views
usernameEditText = findViewById(R.id.username);
passwordEditText = findViewById(R.id.password);
loginButton = findViewById(R.id.loginButton);
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Index"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
Output:
11. Write a program to send SMS.
activity_main.xml
<EditText
android:id="@+id/phoneEditText"
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/sendButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send SMS" />
</LinearLayout>
MainActivity.java
package com.example.index;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
phoneEditText = findViewById(R.id.phoneEditText);
messageEditText = findViewById(R.id.messageEditText);
sendButton = findViewById(R.id.sendButton);
sendButton.setOnClickListener(v -> {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.SEND_SMS}, SMS_PERMISSION_CODE);
} else {
sendSMS();
}
});
}
try {
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phone, null, message, null, null);
Toast.makeText(this, "SMS Sent", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(this, "SMS Failed", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
AndroidManifest.xml
<application
android:allowBackup="true"
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Index">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Output:
12. Write a program to draw a route between two locations.
activity_main.xml
<EditText
android:id="@+id/fromEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter source location"
android:inputType="text" />
<EditText
android:id="@+id/toEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter destination"
android:inputType="text"
android:layout_marginTop="16dp" />
<Button
android:id="@+id/drawRouteButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Draw Route"
android:layout_marginTop="20dp" />
</LinearLayout>
MainActivity.java
package com.example.index;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
EditText fromEditText, toEditText;
Button drawRouteButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fromEditText = findViewById(R.id.fromEditText);
toEditText = findViewById(R.id.toEditText);
drawRouteButton = findViewById(R.id.drawRouteButton);
drawRouteButton.setOnClickListener(v -> {
String source = fromEditText.getText().toString().trim();
String destination = toEditText.getText().toString().trim();
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Output:
This document is created by Naitik Donda. It may not be copied, distributed, or shared
without giving proper credit. Please provide five credits before sharing or using any part
of this document. Unauthorized use is prohibited.