mad exp 8 and 10

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

213CSE2316 Mobile Application Development Laboratory

Ex.No.8 Develop an Android Application with a splash Screen


Date:
Aim:
To develop an android application with a splash screen.

Procedure:
Step 1: Form design
1. Create new project.

2. Click res directory -> layout -> activity_main.xml -> Design

3. Insert an image with ImageView

4. Create a New EmptyActivity from right click on the java folder and name it as
“HomeActivity”.

5. Add the message in the new page document.

Step 2: Open java -> MainActivity.java and write the code needed for splash screen.
Step 3: Run the program using following steps in Android Emulator.
Select the project in Package Explorer and Click Run icon in the tool bar.
(or)
Click Run in the menu bar and choose Run option.
(or)
Right-click the project in the Package Explorer, select “Run As” menu and click “Android
Application” menu.

Program:
Design View code file name: activity main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
tools:context=".MainActivity">

Department of Computer Science and Engineering, KARE


213CSE2316 Mobile Application Development Laboratory

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/instragram"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.5" />
</androidx.constraintlayout.widget.ConstraintLayout>
Splashscreen page activity code : HomeActivity.java

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


<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="HomeMainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="InstragramApp"
android:textColor="@color/black"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.5" />
</androidx.constraintlayout.widget.ConstraintLayout>

Splashscreen page activity code: MainActivity.java

Department of Computer Science and Engineering, KARE


213CSE2316 Mobile Application Development Laboratory

package com.example.splash;

import android.content.Intent;

import android.os.Bundle;

import android.os.Handler;

import androidx.activity.EdgeToEdge;

import androidx.appcompat.app.AppCompatActivity;

import androidx.core.graphics.Insets;

import androidx.core.view.ViewCompat;

import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {

public static int SPLASH_TIMER = 3000;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

EdgeToEdge.enable(this);

setContentView(R.layout.activity_main);

new Handler().postDelayed(new Runnable() {

@Override

public void run() {

Intent Intent = new Intent(MainActivity.this,HomeActivity.class);

startActivity(Intent);

finish();

}, SPLASH_TIMER);

Department of Computer Science and Engineering, KARE


213CSE2316 Mobile Application Development Laboratory

ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {

Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());

v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);

return insets;

});

Output:

Result:

Thus, the splash screen was developed using Android studio and the output was
verified.

Department of Computer Science and Engineering, KARE


213CSE2316 Mobile Application Development Laboratory

Ex.No.9 Develop an Android Application that uses layout managers and Event

listeners

Date:
Aim:
To develop an android application that uses layout managers and Event listeners

Procedure:
Step 1: Form design
1. Create new project.

2. Click res directory -> layout -> activity_main.xml -> Design

3. Create different layout managers and add the View elements in the xml code

4. Add the necessary properties in the xml code for the Layouts and View Elements in
the MainActivity.xml

Step 2: Open java -> MainActivity.java and write the code needed for splash screen.
Step 3: Run the program using following steps in Android Emulator.
Select the project in Package Explorer and Click Run icon in the tool bar.
(or)
Click Run in the menu bar and choose Run option.
(or)
Right-click the project in the Package Explorer, select “Run As” menu and click “Android
Application” menu.

Program:
Design View code file name: activity main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">

<EditText
android:id="@+id/editTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content"

Department of Computer Science and Engineering, KARE


213CSE2316 Mobile Application Development Laboratory

android:hint="Enter Name" />

<EditText
android:id="@+id/editTextReg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Registration Number" />

<EditText
android:id="@+id/editTextDept"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Department" />

<Button
android:id="@+id/buttonSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="submit"
android:layout_gravity="center_horizontal"/>
</LinearLayout>

Layout page activity code: MainActivity.java

package com.example.layout;

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 editTextName,editTextReg,editTextDept;


private Button buttonSubmit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextName=findViewById(R.id.editTextName);

Department of Computer Science and Engineering, KARE


213CSE2316 Mobile Application Development Laboratory

editTextReg=findViewById(R.id.editTextReg);
editTextDept=findViewById(R.id.editTextDept);
buttonSubmit=findViewById(R.id.buttonSubmit);
buttonSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = editTextName.getText().toString();
String reg = editTextReg.getText().toString();
String dept = editTextDept.getText().toString();
if (!name.isEmpty() && !reg.isEmpty() && !dept.isEmpty()) {
Toast.makeText(MainActivity.this, "Submitted:\nName: " + name +
"\nReg: " + reg + "\nDept: " + dept, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Please fill all fields",
Toast.LENGTH_SHORT).show();
}
}
});
}
}

Department of Computer Science and Engineering, KARE


213CSE2316 Mobile Application Development Laboratory

Output:

Result:

Thus, uses layout managers and event listeners was developed using Android studio and the
output was verified.

Department of Computer Science and Engineering, KARE


213CSE2316 Mobile Application Development Laboratory

Ex.No.10 Develop an Android Application that to Create Alarm Clock

Date:
Aim:
To develop an android application that to Create alarm clock.
Procedure:
Step 1: Form design
1. Create new project.

2. Click res directory -> layout -> activity_main.xml -> Design

3. Create different layout managers and add the View elements in the xml code

4. Add the necessary properties in the xml code for the Layouts and View Elements in
the MainActivity.xml

5. Add the Alarm Clock Picker View in the xml page.

Step 2: Open java -> MainActivity.java and write the code needed for splash screen.
Step 3: Run the program using following steps in Android Emulator.
Select the project in Package Explorer and Click Run icon in the tool bar.
(or)
Click Run in the menu bar and choose Run option.
(or)
Right-click the project in the Package Explorer, select “Run As” menu and click “Android
Application” menu.

Department of Computer Science and Engineering, KARE


213CSE2316 Mobile Application Development Laboratory

Program:
Design View code file name: activity main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Alarm On/Off"
android:id="@+id/alarmToggle"
android:layout_centerHorizontal="true"
android:layout_below="@+id/alarmTimePicker"
android:onClick="onToggleClicked" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text=""
android:id="@+id/alarmText"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:layout_below="@+id/alarmToggle" />
<TimePicker
android:id="@+id/alarmTimePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="36dp" />

</RelativeLayout>

Alarm Clock page activity code: MainActivity.java

package com.example.alarmclock;

import java.util.Calendar;

import android.os.Bundle;

import android.app.Activity;

import android.app.AlarmManager;

import android.app.PendingIntent;

import android.content.Intent;

import android.util.Log;

Department of Computer Science and Engineering, KARE


213CSE2316 Mobile Application Development Laboratory

import android.view.Menu;

import android.view.View;

import android.widget.TextView;

import android.widget.TimePicker;

import android.widget.ToggleButton;

public class MainActivity extends Activity {

AlarmManager alarmManager;

private PendingIntent pendingIntent;

private TimePicker alarmTimePicker;

private static MainActivity inst;

private TextView alarmTextView;

public static MainActivity instance() {

return inst;

@Override

public void onStart() {

super.onStart();

inst = this;

@Override

protected void onCreate(

Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

alarmTimePicker = (TimePicker) findViewById(R.id.alarmTimePicker);

alarmTextView = (TextView) findViewById(R.id.alarmText);

ToggleButton alarmToggle =(ToggleButton)findViewById(R.id.alarmToggle);

alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

Department of Computer Science and Engineering, KARE


213CSE2316 Mobile Application Development Laboratory

public void onToggleClicked(View view) {

if (((ToggleButton) view).isChecked()) {

Log.d("MyActivity", "Alarm On");

Calendar calendar = Calendar

.getInstance();

calendar.set(Calendar.HOUR_OF_DAY,

alarmTimePicker

.getCurrentHour());

calendar.set(Calendar.MINUTE,

alarmTimePicker

.getCurrentMinute());

Intent myIntent = new Intent(

MainActivity.this,

AlarmReceiver.class);

pendingIntent = PendingIntent

.getBroadcast(

MainActivity.this, 0,

myIntent, 0);

alarmManager.set(AlarmManager.RTC,

calendar.getTimeInMillis(),

pendingIntent);

} else {

alarmManager.cancel(pendingIntent);

setAlarmText("");

Log.d("MyActivity", "Alarm Off");

public void setAlarmText(String alarmText) {

alarmTextView.setText(alarmText);

Department of Computer Science and Engineering, KARE


213CSE2316 Mobile Application Development Laboratory

Alarm Clock page activity code: AlarmReceiver.java

package com.example.alarmclock;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.util.Log;
public class AlarmReceiver extends BroadcastReceiver {
private MediaPlayer mediaPlayer;
@Override
public void onReceive(final Context context, Intent intent) { Log.d("AlarmReceiver", "Alarm received!");
MainActivity inst = MainActivity.instance();
if (inst != null) {
inst.setAlarmText("Wake Up... It is a bright day ahead!..");
}
playAlarmSound(cont
Intent serviceIntent = new Intent(context, com.example.alarmclock.AlarmService.class);
context.startForegroundService(serviceIntent);
setResultCode(Activity.RESULT_OK);
}
private void playAlarmSound(Context context) {
try {
Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if (alarmUri == null) {
alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
}
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(context, alarmUri);
mediaPlayer.setLooping(true); // Keep alarm looping
mediaPlayer.prepare();
mediaPlayer.start();
Log.d("AlarmReceiver", "Alarm sound is playing.");
} catch (Exception e) {
Log.e("AlarmReceiver", "Error playing alarm sound", e);
}
}
public void stopAlarmSound() {
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer.release();

Department of Computer Science and Engineering, KARE


213CSE2316 Mobile Application Development Laboratory

mediaPlayer = null;
Log.d("AlarmReceiver", "Alarm sound stopped.");
}
}
}

Alarm Clock page activity code: AlarmService.java

package com.example.alarmclock;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import androidx.core.app.NotificationCompat;

public class AlarmService extends Service {


private static final String CHANNEL_ID = "AlarmChannel";
private NotificationManager alarmNotificationManager;
@Override
public void onCreate() {
super.onCreate();
createNotificationChannel();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
sendNotification("Wake Up! Wake Up!");
return START_NOT_STICKY;
}
private void sendNotification(String msg) {
Log.d("AlarmService", "Preparing to send notification...: " + msg);
alarmNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(
this, 0, notificationIntent, PendingIntent.FLAG_IMMUTABLE);
NotificationCompat.Builder alarmNotificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Alarm")
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_HIGH);
alarmNotificationBuilder.setContentIntent(contentIntent);
alarmNotificationManager.notify(1, alarmNotificationBuilder.build());

Department of Computer Science and Engineering, KARE


213CSE2316 Mobile Application Development Laboratory

Log.d("AlarmService", "Notification sent.");


}
private void createNotificationChannel() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
CHANNEL_ID, "Alarm Notifications", NotificationManager.IMPORTANCE_HIGH);
channel.setDescription("Channel for alarm notifications");
alarmNotificationManager = getSystemService(NotificationManager.class);
if (alarmNotificationManager != null) {
alarmNotificationManager.createNotificationChannel(channel);
}
}
}

@Override
public IBinder onBind(Intent intent) {
// This service does not support binding.
return null;
}
}

Output:

Result:

Thus, to Create Alarm Clock was developed using Android studio and the output was verified

Department of Computer Science and Engineering, KARE

You might also like