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

MAD External Solution v1

The document provides code solutions for various Android applications, including displaying user information in a Linear Layout, showcasing data types in a FrameLayout, creating a simple calculator, implementing radio buttons with and without a RadioGroup, and changing images with a button. Each section includes XML layout files and corresponding Java code for the MainActivity. Additionally, AndroidManifest.xml files are provided to define application settings and activities.

Uploaded by

meethrathod85
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 views44 pages

MAD External Solution v1

The document provides code solutions for various Android applications, including displaying user information in a Linear Layout, showcasing data types in a FrameLayout, creating a simple calculator, implementing radio buttons with and without a RadioGroup, and changing images with a button. Each section includes XML layout files and corresponding Java code for the MainActivity. Additionally, AndroidManifest.xml files are provided to define application settings and activities.

Uploaded by

meethrathod85
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/ 44

MAD External Exam Code Solution

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;

public class MainActivity extends AppCompatActivity {


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

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:
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;

public class MainActivity extends AppCompatActivity {


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

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:
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;

public class MainActivity extends AppCompatActivity {

EditText num1, num2;


TextView tvResult;
Button btnAdd, btnSub, btnMul, btnDiv;

@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" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
</application>

</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

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


<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="24dp">

<!-- Without RadioGroup -->


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Without RadioGroup:"
android:textSize="18sp"
android:layout_marginBottom="10dp" />

<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" />

<!-- With RadioGroup -->


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="With RadioGroup:"
android:textSize="18sp"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp" />

<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;

public class MainActivity extends AppCompatActivity {

RadioButton radio1, radio2, radio3, radio4;


RadioGroup radioGroup;

@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());

radioGroup.setOnCheckedChangeListener((group, checkedId) -> {


if (checkedId == R.id.radio3) {
Toast.makeText(this, "Selected: Option 3", Toast.LENGTH_SHORT).show();
} else if (checkedId == R.id.radio4) {
Toast.makeText(this, "Selected: Option 4", Toast.LENGTH_SHORT).show();
}
});
}
}

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:
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;

public class MainActivity extends 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;

public class MainActivity extends AppCompatActivity {

Button btnDate, btnTime;


TextView tvResult;
String selectedDate = "", selectedTime = "";

@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);

Calendar calendar = Calendar.getInstance();

btnDate.setOnClickListener(v -> {
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);

DatePickerDialog datePicker = new DatePickerDialog(this, (view, y, m, d) -> {


selectedDate = d + "/" + (m + 1) + "/" + y;
showResult();
}, year, month, day);
datePicker.show();
});

btnTime.setOnClickListener(v -> {
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);

TimePickerDialog timePicker = new TimePickerDialog(this, (view, h, m) -> {


selectedTime = h + ":" + String.format("%02d", m);
showResult();
}, hour, minute, true);
timePicker.show();
});
}
void showResult() {
String result = "";
if (!selectedDate.isEmpty()) result += "Date: " + selectedDate + "\n";
if (!selectedTime.isEmpty()) result += "Time: " + selectedTime;
tvResult.setText(result);
}
}

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:
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

<?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/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;

public class MainActivity extends 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

<?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:
8. Write a program to display the list of sensors supported by the mobile device.

activity_main.xml

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


<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="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;

public class MainActivity extends AppCompatActivity {

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);

StringBuilder sensorInfo = new StringBuilder("Sensors available:\n\n");


for (Sensor sensor : sensorList) {
sensorInfo.append(sensor.getName()).append("\n");
}

tvSensors.setText(sensorInfo.toString());
}
}

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:
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;

public class MainActivity extends AppCompatActivity {

private static final int REQUEST_VIDEO_CAPTURE = 1;


private static final int REQUEST_PERMISSIONS = 100;
private VideoView videoView;
private Button recordButton, playButton;
private Uri videoUri;

// List of required permissions


private final String[] REQUIRED_PERMISSIONS = new String[]{
Manifest.permission.CAMERA,
Manifest.permission.RECORD_AUDIO,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};

@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);

// Check and request permissions when app starts


if (!allPermissionsGranted()) {
ActivityCompat.requestPermissions(this, REQUIRED_PERMISSIONS,
REQUEST_PERMISSIONS);
}

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();
}
});
}

private boolean allPermissionsGranted() {


for (String permission : REQUIRED_PERMISSIONS) {
if (ContextCompat.checkSelfPermission(this, permission) !=
PackageManager.PERMISSION_GRANTED) {
return false;
}
}
return true;
}

private void dispatchTakeVideoIntent() {


Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
if (takeVideoIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE);
}
}

@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">

<!-- Permissions for Camera, Audio, and Storage -->


<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="32"
tools:ignore="ScopedStorage" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus"
android:required="false" />

<!-- Declare camera feature as optional -->

<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 &amp; 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">

<!-- Username Field -->


<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:inputType="text"/>

<!-- Password Field -->


<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"/>

<!-- Login Button -->


<Button
android:id="@+id/loginButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"/>

</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;

public class MainActivity extends AppCompatActivity {

private EditText usernameEditText;


private EditText passwordEditText;
private Button loginButton;

@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);

// Set up the login button listener


loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Get the entered username and password
String username = usernameEditText.getText().toString().trim();
String password = passwordEditText.getText().toString().trim();

// Check login credentials (simple check for demo)


if (username.equals("user") && password.equals("password")) {
// If credentials are correct, show successful toast
Toast.makeText(MainActivity.this, "Login Successful",
Toast.LENGTH_SHORT).show();
} else {
// If credentials are incorrect, show unsuccessful toast
Toast.makeText(MainActivity.this, "Login Unsuccessful",
Toast.LENGTH_SHORT).show();
}
}
});
}
}

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:
11. Write a program to send SMS.

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">

<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;

public class MainActivity extends AppCompatActivity {

EditText phoneEditText, messageEditText;


Button sendButton;
final int SMS_PERMISSION_CODE = 123;

@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();
}
});
}

private void sendSMS() {


String phone = phoneEditText.getText().toString();
String message = messageEditText.getText().toString();

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();
}
}

// Handle permission result


@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String[] permissions,
@NonNull int[] grantResults) {
if (requestCode == SMS_PERMISSION_CODE) {
if (grantResults.length > 0 &&
grantResults[0] == PackageManager.PERMISSION_GRANTED) {
sendSMS();
} else {
Toast.makeText(this, "Permission Denied", Toast.LENGTH_SHORT).show();
}
}
}
}

AndroidManifest.xml

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


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.index">

<!-- Permissions -->


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

<!-- Let system know telephony hardware is optional -->


<uses-feature android:name="android.hardware.telephony" android:required="false"
/>

<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

<?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="20dp"
android:gravity="center">

<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();

if (!source.isEmpty() && !destination.isEmpty()) {


String uri = "https://www.google.com/maps/dir/?api=1&origin="
+ Uri.encode(source) + "&destination=" + Uri.encode(destination)
+ "&travelmode=driving";

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));


intent.setPackage("com.google.android.apps.maps");
startActivity(intent);
} else {
Toast.makeText(MainActivity.this, "Enter both locations",
Toast.LENGTH_SHORT).show();
}
});
}
}
AndroidManifest.xml

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


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.index">
<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:

© 2025 Naitik Donda. All Rights Reserved.

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.

For any queries; gmail> [email protected]

You might also like