0% found this document useful (0 votes)
97 views

EX8 Activity - Main - XML: Android App Tools Android Android Tools

This document contains code for creating a custom countdown timer app with a countdown text view and start/pause/reset buttons. It also contains code for creating a custom expandable notification with collapsed and expanded views using remote views. The main activity code initializes the views and buttons, starts the countdown timer on click of start which updates the text view, and resets or pauses the timer on other button clicks. The notification code creates a notification channel, builds the notification with custom collapsed and expanded remote views, and notifies the user on a button click from the main activity.

Uploaded by

murugan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views

EX8 Activity - Main - XML: Android App Tools Android Android Tools

This document contains code for creating a custom countdown timer app with a countdown text view and start/pause/reset buttons. It also contains code for creating a custom expandable notification with collapsed and expanded views using remote views. The main activity code initializes the views and buttons, starts the countdown timer on click of start which updates the text view, and resets or pauses the timer on other button clicks. The notification code creates a notification channel, builds the notification with custom collapsed and expanded remote views, and notifies the user on a button click from the main activity.

Uploaded by

murugan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

EX 8

Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.exercise8.MainActivity">

<TextView
android:id="@+id/text_view_countdown"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="00:00"
android:textColor="@android:color/black"
android:textSize="60sp" />

<Button
android:id="@+id/button_start_pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text_view_countdown"
android:layout_centerHorizontal="true"
android:text="start" />

<Button
android:id="@+id/button_reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text_view_countdown"
android:layout_marginStart="11dp"
android:layout_toEndOf="@+id/button_start_pause"
android:text="reset"
android:visibility="invisible"
tools:visibility="visible"
android:layout_marginLeft="11dp"
android:layout_toRightOf="@+id/button_start_pause" />

</RelativeLayout>

Main activity
package com.example.exercise8;

import android.os.CountDownTimer;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {


private static final long START_TIME_IN_MILLIS = 600000;

private TextView mTextViewCountDown;


private Button mButtonStartPause;
private Button mButtonReset;

private CountDownTimer mCountDownTimer;

private boolean mTimerRunning;

private long mTimeLeftInMillis = START_TIME_IN_MILLIS;

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

mTextViewCountDown = findViewById(R.id.text_view_countdown);

mButtonStartPause = findViewById(R.id.button_start_pause);
mButtonReset = findViewById(R.id.button_reset);

mButtonStartPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mTimerRunning) {
pauseTimer();
} else {
startTimer();
}
}
});

mButtonReset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
resetTimer();
}
});

updateCountDownText();
}

private void startTimer() {


mCountDownTimer = new CountDownTimer(mTimeLeftInMillis, 1000) {
@Override
public void onTick(long millisUntilFinished) {
mTimeLeftInMillis = millisUntilFinished;
updateCountDownText();
}

@Override
public void onFinish() {
mTimerRunning = false;
mButtonStartPause.setText("Start");
mButtonStartPause.setVisibility(View.INVISIBLE);
mButtonReset.setVisibility(View.VISIBLE);
}
}.start();

mTimerRunning = true;
mButtonStartPause.setText("pause");
mButtonReset.setVisibility(View.INVISIBLE);
}

private void pauseTimer() {


mCountDownTimer.cancel();
mTimerRunning = false;
mButtonStartPause.setText("Start");
mButtonReset.setVisibility(View.VISIBLE);
}

private void resetTimer() {


mTimeLeftInMillis = START_TIME_IN_MILLIS;
updateCountDownText();
mButtonReset.setVisibility(View.INVISIBLE);
mButtonStartPause.setVisibility(View.VISIBLE);
}

private void updateCountDownText() {


int minutes = (int) (mTimeLeftInMillis / 1000) / 60;
int seconds = (int) (mTimeLeftInMillis / 1000) % 60;

String timeLeftFormatted = String.format(Locale.getDefault(), "%02d:%02d",


minutes, seconds);

mTextViewCountDown.setText(timeLeftFormatted);
}
}

EX 7

Activity main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="showNotification"
android:text="show"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.965" />

</RelativeLayout>

Mainactivity.java
package com.example.e7;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RemoteViews;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;

import static com.example.e7.App.CHANNEL_ID;


public class MainActivity extends AppCompatActivity {
private NotificationManagerCompat notificationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
notificationManager = NotificationManagerCompat.from(this);
}
public void showNotification(View v) {
RemoteViews collapsedView = new RemoteViews(getPackageName(),
R.layout.notification_collapsed);
RemoteViews expandedView = new RemoteViews(getPackageName(),
R.layout.notification_expanded);
Intent clickIntent = new Intent(this, NotificationReceiver.class);
PendingIntent clickPendingIntent = PendingIntent.getBroadcast(this,
0, clickIntent, 0);
collapsedView.setTextViewText(R.id.text_view_collapsed_1, "Hello World!");
expandedView.setImageViewResource(R.id.image_view_expanded,
R.drawable.ic_launcher_background);
expandedView.setOnClickPendingIntent(R.id.image_view_expanded,
clickPendingIntent);
Notification notification = new NotificationCompat.Builder(this,
CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setCustomContentView(collapsedView)
.setCustomBigContentView(expandedView)
//.setStyle(new NotificationCompat.DecoratedCustomViewStyle())
.build();
notificationManager.notify(1, notification);
}
}

App.java
package com.example.e7;
import android.app.Application;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Build;

public class App extends Application {


public static final String CHANNEL_ID = "exampleChannel";

@Override
public void onCreate() {
super.onCreate();
createNotificationChannel();
}

private void createNotificationChannel() {


if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
CHANNEL_ID,
"Example Channel",
NotificationManager.IMPORTANCE_HIGH
);

NotificationManager manager =
getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel);
}
}
}

notification_collapsed.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="64dp"
android:gravity="center"
android:orientation="vertical">

<TextView
android:id="@+id/text_view_collapsed_1"
style="@style/TextAppearance.Compat.Notification.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Message!"
android:textColor="@color/white" />

<TextView
android:id="@+id/text_view_collapsed_2"
style="@style/TextAppearance.Compat.Notification.Info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Expand to show!" />

</LinearLayout>
notification_expanded.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="256dp"
android:orientation="vertical">

<TextView
android:id="@+id/text_view_expanded"
style="@style/TextAppearance.Compat.Notification"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_red_light"
android:padding="8dp"
android:text="This is a custom notification"
android:textAlignment="center"
android:textColor="#FFF"
android:layout_gravity="center_horizontal" />

<ImageView
android:id="@+id/image_view_expanded"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/ic_launcher_background" />

</LinearLayout>

NotificationReceiver.java
package com.example.e7;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

import androidx.core.app.NotificationManagerCompat;

public class NotificationReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Image clicked", Toast.LENGTH_SHORT).show();

NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(context);
notificationManager.cancel(1);
}
}

AndroidManifest.xml
<receiver android:name=".NotificationReceiver" />
Ex 9

MainActivity.java
package com.example.exercise8;

import android.os.CountDownTimer;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.Locale;

public class MainActivity extends AppCompatActivity {


private static final long START_TIME_IN_MILLIS = 600000;

private TextView mTextViewCountDown;


private Button mButtonStartPause;
private Button mButtonReset;

private CountDownTimer mCountDownTimer;

private boolean mTimerRunning;

private long mTimeLeftInMillis = START_TIME_IN_MILLIS;

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

mTextViewCountDown = findViewById(R.id.text_view_countdown);

mButtonStartPause = findViewById(R.id.button_start_pause);
mButtonReset = findViewById(R.id.button_reset);

mButtonStartPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mTimerRunning) {
pauseTimer();
} else {
startTimer();
}
}
});

mButtonReset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
resetTimer();
}
});
updateCountDownText();
}

private void startTimer() {


mCountDownTimer = new CountDownTimer(mTimeLeftInMillis, 1000) {
@Override
public void onTick(long millisUntilFinished) {
mTimeLeftInMillis = millisUntilFinished;
updateCountDownText();
}

@Override
public void onFinish() {
mTimerRunning = false;
mButtonStartPause.setText("Start");
mButtonStartPause.setVisibility(View.INVISIBLE);
mButtonReset.setVisibility(View.VISIBLE);
}
}.start();

mTimerRunning = true;
mButtonStartPause.setText("pause");
mButtonReset.setVisibility(View.INVISIBLE);
}

private void pauseTimer() {


mCountDownTimer.cancel();
mTimerRunning = false;
mButtonStartPause.setText("Start");
mButtonReset.setVisibility(View.VISIBLE);
}

private void resetTimer() {


mTimeLeftInMillis = START_TIME_IN_MILLIS;
updateCountDownText();
mButtonReset.setVisibility(View.INVISIBLE);
mButtonStartPause.setVisibility(View.VISIBLE);
}

private void updateCountDownText() {


int minutes = (int) (mTimeLeftInMillis / 1000) / 60;
int seconds = (int) (mTimeLeftInMillis / 1000) % 60;

String timeLeftFormatted = String.format(Locale.getDefault(), "%02d:%02d",


minutes, seconds);

mTextViewCountDown.setText(timeLeftFormatted);
}
}

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

<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />

<Button
android:id="@+id/btnCurrentCity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Current City" />
</LinearLayout>

Strings.xml
<resources>
<string name="app_name">MyCity</string>
<string name="gmap_key">YOUR API KEY</string>
</resources>

Styles.xml
<resources>

<!-- Base application theme. -->


<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>

</resources>

You might also like