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

Android Final

The document describes an Android app that demonstrates communication between two activities. The main activity allows the user to enter text and launch a second activity. The second activity displays the text from the first activity and allows entering a reply. It returns the reply to the main activity using startActivityForResult. This demonstrates passing data between activities and handling the result in the launching activity.

Uploaded by

Pramod Navle
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
0% found this document useful (0 votes)
122 views

Android Final

The document describes an Android app that demonstrates communication between two activities. The main activity allows the user to enter text and launch a second activity. The second activity displays the text from the first activity and allows entering a reply. It returns the reply to the main activity using startActivityForResult. This demonstrates passing data between activities and handling the result in the launching activity.

Uploaded by

Pramod Navle
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
You are on page 1/ 36

Pramod Navle.

SYCS24

Practical No: 4
Aim: Create an android app that demonstrates Activity Lifecycle and Instance State
Activity Lifecycle
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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">
android:orientation="vertical"
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"sssss
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
MainActivity.java
package com.example.activitylifecycle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(String.valueOf(this), "");
Log.d(String.valueOf(this), "onCreate");
}
@Override
protected void onStart(){
super.onStart();
Log.d(String.valueOf(this), "onStart");
}
@Override
protected void onPause(){
super.onPause();
Log.d(String.valueOf(this), "onPause");
}
@Override
protected void onRestart(){
super.onRestart();
Log.d(String.valueOf(this), "onRestart");
}
@Override
protected void onResume(){
Pramod Navle.
SYCS24

super.onResume();
Log.d(String.valueOf(this),"onResume");
}
@Override
protected void onStop(){
super.onStop();
Log.d(String.valueOf(this), "onStop");
}
@Override
protected void onDestroy(){
super.onDestroy();
Log.d(String.valueOf(this), "onDestroy");
}
}
Output:
onStart()

onPause()

onRestart()

onResume()
Pramod Navle.
SYCS24

onStop()

onDestroy()
Pramod Navle.
SYCS24

Practical No: 5
Two Activities.
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.twoactivities.MainActivity">

<TextView
android:id="@+id/text_header_reply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="16dp"
android:text="@string/text_header_reply"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textStyle="bold"
android:visibility="invisible"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/text_message_reply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:visibility="invisible"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/text_header_reply" />

<Button
android:id="@+id/button_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginRight="16dp"
android:text="@string/button_main"
android:onClick="launchSecondActivity"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent" />

<EditText
android:id="@+id/editText_main"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:ems="10"
android:hint="@string/editText_main"
android:inputType="textLongMessage"
app:layout_constraintBottom_toBottomOf="parent"
Pramod Navle.
SYCS24
app:layout_constraintEnd_toStartOf="@+id/button_main"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>

Activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.twoactivities.SecondActivity">

<TextView
android:id="@+id/text_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="16dp"
android:text="@string/text_header"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/text_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/text_header" />

<Button
android:id="@+id/button_second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginRight="16dp"
android:text="@string/button_second"
android:onClick="returnReply"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent" />

<EditText
android:id="@+id/editText_second"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:ems="10"
android:hint="@string/editText_second"
android:inputType="textLongMessage"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/button_second"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
Pramod Navle.
SYCS24

Mainactivity.java
package com.example.android.twoactivities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

private static final String LOG_TAG = MainActivity.class.getSimpleName();

public static final String EXTRA_MESSAGE


= "com.example.android.twoactivities.extra.MESSAGE";

public static final int TEXT_REQUEST = 1;

private EditText mMessageEditText;

private TextView mReplyHeadTextView;

private TextView mReplyTextView;

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

mMessageEditText = findViewById(R.id.editText_main);
mReplyHeadTextView = findViewById(R.id.text_header_reply);
mReplyTextView = findViewById(R.id.text_message_reply);
}

public void launchSecondActivity(View view) {


Log.d(LOG_TAG, "Button clicked!");
Intent intent = new Intent(this, SecondActivity.class);
String message = mMessageEditText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivityForResult(intent, TEXT_REQUEST);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == TEXT_REQUEST) {
if (resultCode == RESULT_OK) {
String reply = data.getStringExtra(SecondActivity.EXTRA_REPLY);

mReplyHeadTextView.setVisibility(View.VISIBLE);

mReplyTextView.setText(reply);
mReplyTextView.setVisibility(View.VISIBLE);
Pramod Navle.
SYCS24
}
}
}
}

SecondActivity.java
package com.example.android.twoactivities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class SecondActivity extends AppCompatActivity {

public static final String EXTRA_REPLY =


"com.example.android.twoactivities.extra.REPLY";

private EditText mReply;

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

mReply = findViewById(R.id.editText_second);

Intent intent = getIntent();


String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

TextView textView = findViewById(R.id.text_message);


textView.setText(message);
}

public void returnReply(View view) {

String reply = mReply.getText().toString();

Intent replyIntent = new Intent();


replyIntent.putExtra(EXTRA_REPLY, reply);
setResult(RESULT_OK, replyIntent);
finish();
}
}
Output:
Pramod Navle.
SYCS24
Pramod Navle.
SYCS24

Practical 6
Aim: Create an android app that demonstrates the use of Keyboards, Input Controls, Alerts, and
Pickers

Orderactivity.java
package com.example.dell.droidcafe;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class OrderActivity extends AppCompatActivity implements


AdapterView.OnItemSelectedListener {

private static final String TAG = MainActivity.class.getSimpleName();


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = (TextView) findViewById(R.id.order_textview);
textView.setText(message);
// Create the spinner.
Spinner spinner = (Spinner) findViewById(R.id.label_spinner);
if (spinner != null) {
spinner.setOnItemSelectedListener(this);
// Create ArrayAdapter using the string array and default spinner
layout.
ArrayAdapter<CharSequence> adapter =
ArrayAdapter.createFromResource(this,
R.array.labels_array,
android.R.layout.simple_spinner_dropdown_item);
// Specify the layout to use when the list of choices appears.
adapter.setDropDownViewResource
(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner.
if (spinner != null) {
spinner.setAdapter(adapter);
}
}
EditText editText =(EditText) findViewById(R.id.phone_text);
if (editText != null)
editText.setOnEditorActionListener(new
TextView.OnEditorActionListener() {
// If view is found, set the listener for editText.
@Override
public boolean onEditorAction(TextView textView, int actionId,
KeyEvent keyEvent) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_SEND) {
dialNumber();
Pramod Navle.
SYCS24
handled = true;
}
return handled;
}
});
}
private void dialNumber() {
// Find the editText_main view.
EditText editText = (EditText) findViewById(R.id.phone_text);
String phoneNum = null;
// If the editText field is not null,
// concatenate "tel: " with the phone number string.
if (editText != null) phoneNum = "tel:" +
editText.getText().toString();
// Optional: Log the concatenated phone number for dialing.

Log.d(TAG, "dialNumber: " + phoneNum);


// Specify the intent.
Intent intent = new Intent(Intent.ACTION_DIAL);
// Set the data for the intent as the phone number.
intent.setData(Uri.parse(phoneNum));
// If the intent resolves to a package (app),
// start the activity with the intent.
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
} else {
Log.d("ImplicitIntents", "Can't handle this!");
}
}
public void displayToast(String message) {
Toast.makeText(getApplicationContext(), message,
Toast.LENGTH_SHORT).show();
}

public void onRadioButtonClicked(View view) {


// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked.
switch (view.getId()) {
case R.id.sameday:
if (checked)
// Same day service
displayToast(getString(R.string.same_day_messenger_service));
break;
case R.id.nextday:
if (checked)
// Next day delivery
displayToast(getString(R.string.next_day_ground_delivery));
break;
case R.id.pickup:
if (checked)
// Pick up
displayToast(getString(R.string.pick_up));
break;
default:
// Do nothing.
break;
}
}

@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int
i, long l) {
String spinnerLabel = adapterView.getItemAtPosition(i).toString();
displayToast(spinnerLabel);
}
Pramod Navle.
SYCS24
@Override
public void onNothingSelected(AdapterView<?> adapterView) {

}
activity_order.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".OrderActivity">

<TextView
android:id="@+id/order_textview"
android:layout_width="368dp"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="20dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView

android:id="@+id/name_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="32dp"
android:text="Name :"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/order_textview"/>

<EditText
android:id="@+id/name_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:ems="10"
android:inputType="textPersonName"
android:hint="@string/enter_name_hint"
app:layout_constraintStart_toEndOf="@id/name_label"
app:layout_constraintBaseline_toBaselineOf="@id/name_label"
/>

<TextView
android:id="@+id/address_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="24dp"
android:text="Address :"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/name_label" />

<EditText
android:id="@+id/address_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
Pramod Navle.
SYCS24
android:layout_marginStart="8dp"
android:ems="10"
android:hint="@string/enter_address_hint"
android:inputType="textMultiLine"
app:layout_constraintBaseline_toBaselineOf="@+id/address_label"
app:layout_constraintStart_toEndOf="@+id/address_label" />

<TextView
android:id="@+id/phone_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="24dp"
android:text="Phone No :"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/address_text" />

<EditText
android:id="@+id/phone_text"
android:layout_width="134dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:ems="10"
android:hint="@string/enter_phone_hint"
android:inputType="phone"
android:imeOptions="actionSend"
app:layout_constraintBaseline_toBaselineOf="@+id/phone_label"
app:layout_constraintStart_toEndOf="@+id/phone_label" />

<TextView
android:id="@+id/note_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="24dp"
android:text="Note :"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/phone_label" />

<EditText
android:id="@+id/note_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:ems="10"
android:hint="@string/enter_note_hint"
android:inputType="textCapSentences|textMultiLine"
app:layout_constraintBaseline_toBaselineOf="@+id/note_label"
app:layout_constraintStart_toEndOf="@+id/note_label" />

<TextView
android:id="@+id/delivery_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="24dp"
android:text="@string/choose_delivery_method"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/note_text" />

<RadioGroup
android:layout_width="wrap_content"
Pramod Navle.
SYCS24
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginStart="24dp"
android:orientation="vertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/delivery_label">

<RadioButton
android:id="@+id/sameday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked"
android:text="same day messenger service" />

<RadioButton
android:id="@+id/nextday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked"
android:text="next day ground delivery" />

<RadioButton
android:id="@+id/pickup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked"
android:text="pick up" />
</RadioGroup>

<Spinner
android:id="@+id/label_spinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="24dp"
android:layout_marginRight="24dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/phone_text"
app:layout_constraintTop_toBottomOf="@+id/address_text" />
</android.support.constraint.ConstraintLayout>
Strings.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".OrderActivity">

<TextView
android:id="@+id/order_textview"
android:layout_width="368dp"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="20dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView

android:id="@+id/name_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Pramod Navle.
SYCS24
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="32dp"
android:text="Name :"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/order_textview"/>

<EditText
android:id="@+id/name_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:ems="10"
android:inputType="textPersonName"
android:hint="@string/enter_name_hint"
app:layout_constraintStart_toEndOf="@id/name_label"
app:layout_constraintBaseline_toBaselineOf="@id/name_label"
/>

<TextView
android:id="@+id/address_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="24dp"
android:text="Address :"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/name_label" />

<EditText
android:id="@+id/address_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:ems="10"
android:hint="@string/enter_address_hint"
android:inputType="textMultiLine"
app:layout_constraintBaseline_toBaselineOf="@+id/address_label"
app:layout_constraintStart_toEndOf="@+id/address_label" />

<TextView
android:id="@+id/phone_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="24dp"
android:text="Phone No :"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/address_text" />

<EditText
android:id="@+id/phone_text"
android:layout_width="134dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:ems="10"
android:hint="@string/enter_phone_hint"
android:inputType="phone"
android:imeOptions="actionSend"
app:layout_constraintBaseline_toBaselineOf="@+id/phone_label"
app:layout_constraintStart_toEndOf="@+id/phone_label" />

<TextView
Pramod Navle.
SYCS24
android:id="@+id/note_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="24dp"
android:text="Note :"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/phone_label" />

<EditText
android:id="@+id/note_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:ems="10"
android:hint="@string/enter_note_hint"
android:inputType="textCapSentences|textMultiLine"
app:layout_constraintBaseline_toBaselineOf="@+id/note_label"
app:layout_constraintStart_toEndOf="@+id/note_label" />

<TextView
android:id="@+id/delivery_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="24dp"
android:text="@string/choose_delivery_method"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/note_text" />

<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginStart="24dp"
android:orientation="vertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/delivery_label">

<RadioButton
android:id="@+id/sameday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked"
android:text="same day messenger service" />

<RadioButton
android:id="@+id/nextday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked"
android:text="next day ground delivery" />

<RadioButton
android:id="@+id/pickup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked"
android:text="pick up" />
</RadioGroup>

<Spinner
android:id="@+id/label_spinner"
android:layout_width="0dp"
Pramod Navle.
SYCS24
android:layout_height="wrap_content"
android:layout_marginEnd="24dp"
android:layout_marginRight="24dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/phone_text"
app:layout_constraintTop_toBottomOf="@+id/address_text" />
</android.support.constraint.ConstraintLayout>
Output:
Pramod Navle.
SYCS24

Practical No: 7
Aim: Create an android app that demonstrates the use of an Options Menu

MainActivity.java
package com.example.dell.droidcafe;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

public String mOrderMessage;


public static final String EXTRA_MESSAGE =
"com.example.dell.droidcafe.MainActivity.extra.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);


fab.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent =
new Intent(MainActivity.this, OrderActivity.class);

intent.putExtra(EXTRA_MESSAGE, mOrderMessage);
startActivity(intent);
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_order) {
return true;
}

return super.onOptionsItemSelected(item);
}
public void displayToast(String message) {
Toast.makeText(getApplicationContext(), message,
Toast.LENGTH_SHORT).show();
}
Pramod Navle.
SYCS24
public void showDonutOrder(View view) {
//displayToast(getString(R.string.donut_order_message));
mOrderMessage = getString(R.string.donut_order_message);
displayToast(mOrderMessage);
}
public void showIceCreamOrder(View view) {
//displayToast(getString(R.string.ice_cream_order_message));
mOrderMessage = getString(R.string.ice_cream_order_message);
displayToast(mOrderMessage);
}
public void showFroyoOrder(View view) {
//displayToast(getString(R.string.froyo_order_message));
mOrderMessage = getString(R.string.froyo_order_message);
displayToast(mOrderMessage);
}
}

Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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">

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@drawable/ic_shopping" />

</android.support.design.widget.CoordinatorLayout>

Menu_main.xml
<menu 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"
tools:context="com.example.dell.droidcafe.MainActivity">
<item
android:id="@+id/action_order"
android:orderInCategory="10"
android:title="@string/action_order_message"
app:showAsAction="never" />
Pramod Navle.
SYCS24
<item
android:id="@+id/action_status"
android:orderInCategory="20"
android:title="@string/action_status_message"
app:showAsAction="never" />
<item
android:id="@+id/action_favourites"
android:orderInCategory="30"
android:title="@string/action_favorites_message"
app:showAsAction="never" />
<item
android:id="@+id/action_contact"
android:orderInCategory="100"
android:title="@string/action_contact_message"
app:showAsAction="never" />
</menu>
Content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="@layout/activity_main">

<TextView
android:id="@+id/textintro"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/droid_desserts"
android:textStyle="bold"
android:textSize="24sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>

<ImageView
android:id="@+id/Donut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginTop="24dp"
android:contentDescription="@string/donut"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textintro"
app:srcCompat="@drawable/donut_circle"
android:onClick="showDonutOrder"/>

<ImageView
android:id="@+id/ice_creame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginTop="24dp"
android:contentDescription="@string/ice_cream_sandwiches"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/Donut"
app:srcCompat="@drawable/icecream_circle"
android:onClick="showIceCreamOrder"/>

<ImageView
android:id="@+id/froyo"
android:layout_width="wrap_content"
Pramod Navle.
SYCS24
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginTop="24dp"
android:contentDescription="@string/froyo"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/ice_creame"
app:srcCompat="@drawable/froyo_circle"
android:onClick="showFroyoOrder"/>

<TextView
android:id="@+id/donut_description"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="24dp"
android:text="@string/donut"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintLeft_toRightOf="@+id/Donut"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@+id/Donut" />

<TextView
android:id="@+id/ice_cream_description"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="24dp"
android:text="@string/ice_cream_sandwiches"
app:layout_constraintLeft_toRightOf="@+id/ice_creame"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@+id/ice_creame" />

<TextView
android:id="@+id/froyo_description"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="24dp"
android:text="@string/froyo"
app:layout_constraintLeft_toRightOf="@+id/froyo"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@+id/froyo" />

</android.support.constraint.ConstraintLayout>
Strings_main.xml
<resources>
<string name="app_name">DroidCafe</string>
<string name="action_settings">Settings</string>
<string name="donut">Donuts are glazed and sprinkled with candy.</string>
<string name="froyo">FroYo is premium self-serve frozen yogurt.</string>
<string name="ice_cream_sandwiches">Ice cream sandwiches have chocolate wafers
and vanilla filling.</string>
<string name="droid_desserts">Droid Desserts</string>
<string name="donut_order_message">You ordered a donut.</string>
<string name="ice_cream_order_message">You ordered an ice cream
sandwich.</string>
<string name="froyo_order_message">You ordered a FroYo.</string>
<string name="enter_name_hint">enter name...</string>
<string name="pick_up">pick_up</string>
<string name="next_day_ground_delivery">next day ground delivery</string>
<string name="same_day_messenger_service">same day messenger service</string>
<string name="choose_delivery_method">choose delivery method</string>
<string name="enter_note_hint">enter note...</string>
<string name="note_label_text">note_label_text</string>
<string name="enter_phone_hint">enter phone...</string>
Pramod Navle.
SYCS24
<string name="phone_label_string">phone_label_string</string>
<string name="enter_address_hint">enter address...</string>
<string name="address_label_text">address_label_text</string>
<string name="action_order_message">Order.</string>
<string name="action_status_message">Status.</string>
<string name="action_favorites_message">Favorites.</string>
<string name="action_contact_message">Contact.</string>
<string-array name="labels_array">
<item>Home</item>
<item>Work</item>
<item>Mobile</item>
<item>Other</item>
</string-array>

</resources>
Output:
Pramod Navle.
SYCS24

Practical No: 8
Create an android app that demonstrate Screen Navigation Using the App Bar and Tabs
OrderActivity.java
package com.example.dell.droidcafe;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class OrderActivity extends AppCompatActivity implements


AdapterView.OnItemSelectedListener {

private static final String TAG = MainActivity.class.getSimpleName();


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = (TextView) findViewById(R.id.order_textview);
textView.setText(message);
// Create the spinner.
Spinner spinner = (Spinner) findViewById(R.id.label_spinner);
if (spinner != null) {
spinner.setOnItemSelectedListener(this);
// Create ArrayAdapter using the string array and default spinner
layout.
ArrayAdapter<CharSequence> adapter =
ArrayAdapter.createFromResource(this,
R.array.labels_array,
android.R.layout.simple_spinner_dropdown_item);
// Specify the layout to use when the list of choices appears.
adapter.setDropDownViewResource
(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner.
if (spinner != null) {
spinner.setAdapter(adapter);
}
}
EditText editText =(EditText) findViewById(R.id.phone_text);
if (editText != null)
editText.setOnEditorActionListener(new
TextView.OnEditorActionListener() {
// If view is found, set the listener for editText.
@Override
public boolean onEditorAction(TextView textView, int actionId,
KeyEvent keyEvent) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_SEND) {
dialNumber();
handled = true;
}
return handled;
}
Pramod Navle.
SYCS24
});
}
private void dialNumber() {
// Find the editText_main view.
EditText editText = (EditText) findViewById(R.id.phone_text);
String phoneNum = null;
// If the editText field is not null,
// concatenate "tel: " with the phone number string.
if (editText != null) phoneNum = "tel:" +
editText.getText().toString();
// Optional: Log the concatenated phone number for dialing.

Log.d(TAG, "dialNumber: " + phoneNum);


// Specify the intent.
Intent intent = new Intent(Intent.ACTION_DIAL);
// Set the data for the intent as the phone number.
intent.setData(Uri.parse(phoneNum));
// If the intent resolves to a package (app),
// start the activity with the intent.
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
} else {
Log.d("ImplicitIntents", "Can't handle this!");
}
}
public void displayToast(String message) {
Toast.makeText(getApplicationContext(), message,
Toast.LENGTH_SHORT).show();
}

public void onRadioButtonClicked(View view) {


// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked.
switch (view.getId()) {
case R.id.sameday:
if (checked)
// Same day service
displayToast(getString(R.string.same_day_messenger_service));
break;
case R.id.nextday:
if (checked)
// Next day delivery
displayToast(getString(R.string.next_day_ground_delivery));
break;
case R.id.pickup:
if (checked)
// Pick up
displayToast(getString(R.string.pick_up));
break;
default:
// Do nothing.
break;
}
}

@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int
i, long l) {
String spinnerLabel = adapterView.getItemAtPosition(i).toString();
displayToast(spinnerLabel);
}

@Override
public void onNothingSelected(AdapterView<?> adapterView) {

}
Pramod Navle.
SYCS24

}
Activity_order.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".OrderActivity">

<TextView
android:id="@+id/order_textview"
android:layout_width="368dp"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="20dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView

android:id="@+id/name_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="32dp"
android:text="Name :"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/order_textview"/>

<EditText
android:id="@+id/name_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:ems="10"
android:inputType="textPersonName"
android:hint="@string/enter_name_hint"
app:layout_constraintStart_toEndOf="@id/name_label"
app:layout_constraintBaseline_toBaselineOf="@id/name_label"
/>

<TextView
android:id="@+id/address_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="24dp"
android:text="Address :"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/name_label" />

<EditText
android:id="@+id/address_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:ems="10"
android:hint="@string/enter_address_hint"
android:inputType="textMultiLine"
app:layout_constraintBaseline_toBaselineOf="@+id/address_label"
Pramod Navle.
SYCS24
app:layout_constraintStart_toEndOf="@+id/address_label" />

<TextView
android:id="@+id/phone_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="24dp"
android:text="Phone No :"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/address_text" />

<EditText
android:id="@+id/phone_text"
android:layout_width="134dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:ems="10"
android:hint="@string/enter_phone_hint"
android:inputType="phone"
android:imeOptions="actionSend"
app:layout_constraintBaseline_toBaselineOf="@+id/phone_label"
app:layout_constraintStart_toEndOf="@+id/phone_label" />

<TextView
android:id="@+id/note_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="24dp"
android:text="Note :"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/phone_label" />

<EditText
android:id="@+id/note_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:ems="10"
android:hint="@string/enter_note_hint"
android:inputType="textCapSentences|textMultiLine"
app:layout_constraintBaseline_toBaselineOf="@+id/note_label"
app:layout_constraintStart_toEndOf="@+id/note_label" />

<TextView
android:id="@+id/delivery_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="24dp"
android:text="@string/choose_delivery_method"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/note_text" />

<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginStart="24dp"
android:orientation="vertical"
app:layout_constraintStart_toStartOf="parent"
Pramod Navle.
SYCS24
app:layout_constraintTop_toBottomOf="@id/delivery_label">

<RadioButton
android:id="@+id/sameday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked"
android:text="same day messenger service" />

<RadioButton
android:id="@+id/nextday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked"
android:text="next day ground delivery" />

<RadioButton
android:id="@+id/pickup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked"
android:text="pick up" />
</RadioGroup>

<Spinner
android:id="@+id/label_spinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="24dp"
android:layout_marginRight="24dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/phone_text"
app:layout_constraintTop_toBottomOf="@+id/address_text" />
</android.support.constraint.ConstraintLayout>
AndroiMAnifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dell.droidcafe">

<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/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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


</intent-filter>
</activity>
<activity android:name="com.example.dell.droidcafe.OrderActivity"

android:parentActivityName=".MainActivity">
<meta-data android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity"/>
</activity>
</application>

</manifest>
Pramod Navle.
SYCS24
Strings.xml
<resources>
<string name="app_name">DroidCafe</string>
<string name="action_settings">Settings</string>
<string name="donut">Donuts are glazed and sprinkled with candy.</string>
<string name="froyo">FroYo is premium self-serve frozen yogurt.</string>
<string name="ice_cream_sandwiches">Ice cream sandwiches have chocolate wafers
and vanilla filling.</string>
<string name="droid_desserts">Droid Desserts</string>
<string name="donut_order_message">You ordered a donut.</string>
<string name="ice_cream_order_message">You ordered an ice cream
sandwich.</string>
<string name="froyo_order_message">You ordered a FroYo.</string>
<string name="enter_name_hint">enter name...</string>
<string name="pick_up">pick_up</string>
<string name="next_day_ground_delivery">next day ground delivery</string>
<string name="same_day_messenger_service">same day messenger service</string>
<string name="choose_delivery_method">choose delivery method</string>
<string name="enter_note_hint">enter note...</string>
<string name="note_label_text">note_label_text</string>
<string name="enter_phone_hint">enter phone...</string>
<string name="phone_label_string">phone_label_string</string>
<string name="enter_address_hint">enter address...</string>
<string name="address_label_text">address_label_text</string>
<string name="action_order_message">Order.</string>
<string name="action_status_message">Status.</string>
<string name="action_favorites_message">Favorites.</string>
<string name="action_contact_message">Contact.</string>
<string-array name="labels_array">
<item>Home</item>
<item>Work</item>
<item>Mobile</item>
<item>Other</item>
</string-array>

</resources>
Output:
Pramod Navle.
SYCS24

Practical No: 9
Aim: Create an android app to Connect to the Internet and use BroadcastReceiver.

Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id = "@+id/sendBroadcast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send Custom Broadcast"
android:onClick="sendCustomBroadcast"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Mainactivity.java
package com.example.powerreciever;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private MyReceiver mReceiver = new MyReceiver();
private static final String ACTION_CUSTOM_BROADCAST =
BuildConfig.APPLICATION_ID + ".ACTION_CUSTOM_BROADCAST";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_POWER_DISCONNECTED);
filter.addAction(Intent.ACTION_POWER_CONNECTED);
this.registerReceiver(mReceiver, filter);
LocalBroadcastManager.getInstance(this)
.registerReceiver(mReceiver,
new IntentFilter(ACTION_CUSTOM_BROADCAST));
}
protected void onDestroy() {
//Unregister the receiver
this.unregisterReceiver(mReceiver);
super.onDestroy();
Pramod Navle.
SYCS24

LocalBroadcastManager.getInstance(this)
.unregisterReceiver(mReceiver);
}
public void sendCustomBroadcast(View view) {
Intent customBroadcastIntent = new Intent(ACTION_CUSTOM_BROADCAST);
LocalBroadcastManager.getInstance(this).sendBroadcast(customBroadcastIntent);
}}
MyReceiver.java
package com.example.powerreciever;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class MyReceiver extends BroadcastReceiver {
private static final String ACTION_CUSTOM_BROADCAST =
BuildConfig.APPLICATION_ID + ".ACTION_CUSTOM_BROADCAST";
public void onReceive(Context context, Intent intent) {
String intentAction = intent.getAction();
if (intentAction != null) {
String toastMessage = "unknown intent action";
switch (intentAction){
case Intent.ACTION_POWER_CONNECTED:
toastMessage = "Power connected!";
break;
case Intent.ACTION_POWER_DISCONNECTED:
toastMessage = "Power disconnected!";
break;
case ACTION_CUSTOM_BROADCAST:
toastMessage = "Custom Broadcast Received";
break;
}
Toast.makeText(context, toastMessage, Toast.LENGTH_SHORT).show()
//Display the toast.
}
}
}
Pramod Navle.
SYCS24

Output:
Pramod Navle.
SYCS24

Practical No: 10
Aim: Create an android app to show Notifications and Alarm manager.

Practical 10-A
Notify me
MainActivity.java
package com.example.notifyme;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Color;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

private Button button_notify;


private static final String PRIMARY_CHANNEL_ID = "primary_notification_channel";
private NotificationManager mNotifyManager;
private static final int NOTIFICATION_ID = 0;

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

button_notify = findViewById(R.id.notify);
button_notify.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sendNotification();
}
});
createNotificationChannel();
}
public void sendNotification() {

NotificationCompat.Builder notifyBuilder = getNotificationBuilder();


mNotifyManager.notify(NOTIFICATION_ID, notifyBuilder.build());
}

public void createNotificationChannel()


{
mNotifyManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
getSystemService(NOTIFICATION_SERVICE);
Pramod Navle.
SYCS24

if (android.os.Build.VERSION.SDK_INT >=
android.os.Build.VERSION_CODES.O) {
// Create a NotificationChannel
NotificationChannel notificationChannel = new NotificationChannel(PRIMARY_CHANNEL_ID,
"Mascot Notification", NotificationManager
.IMPORTANCE_HIGH);

notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setDescription("Notification from Mascot");
mNotifyManager.createNotificationChannel(notificationChannel);

}
}
private NotificationCompat.Builder getNotificationBuilder(){
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent notificationPendingIntent = PendingIntent.getActivity(this,
NOTIFICATION_ID, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder notifyBuilder = new NotificationCompat.Builder(this,


PRIMARY_CHANNEL_ID)
.setContentTitle("You've been notified!")
.setContentText("This is your notification text.")
.setContentIntent(notificationPendingIntent)
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_android);
return notifyBuilder;
}
}

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

<Button
android:id="@+id/notify"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Notify Me!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
Pramod Navle.
SYCS24

app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>
Output:
Pramod Navle.
SYCS24

B)ALARM MANAGER:

*activity_main.xml*:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">

<ToggleButton
android:id="@+id/alarm_Toggle"
android:layout_width="match_parent"
android:layout_height="100dp"
android:gravity="center"
android:textOff="Alarm Off"
android:textOn="Alarm On"
android:textStyle="bold"
android:textSize="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

*MainActivity.java*:
package com.example.alarm_manager;

import android.app.AlarmManager;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.SystemClock;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity<isChecked> extends AppCompatActivity {


private NotificationManager mNotificationManager;
private static final int NOTIFICATION_ID = 0;
private static final String PRIMARY_CHANNEL_ID =
Pramod Navle.
SYCS24

"primary_notification_channel";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ToggleButton alarmToggle = findViewById(R.id.alarm_Toggle);

alarmToggle.setOnCheckedChangeListener(
new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton,
boolean isChecked) {
String toastMessage;
if(isChecked){
//Set the toast message for the "on" case.
deliverNotification(MainActivity.this);
toastMessage = "Stand Up Alarm On!";
} else {
//Set the toast message for the "off" case.
mNotificationManager.cancelAll();
toastMessage = "Stand Up Alarm Off!";
}

//Show a toast to say the alarm is turned on or off.


Toast.makeText(MainActivity.this, toastMessage,Toast.LENGTH_SHORT)
.show();
}
});

mNotificationManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);

createNotificationChannel();

public void createNotificationChannel() {

// Create a notification manager object.


mNotificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);

// Notification channels are only available in OREO and higher.


// So, add a check on SDK version.
if (android.os.Build.VERSION.SDK_INT >=
android.os.Build.VERSION_CODES.O) {

// Create the NotificationChannel with all the parameters.


NotificationChannel notificationChannel = new NotificationChannel
(PRIMARY_CHANNEL_ID,
Pramod Navle.
SYCS24

"Stand up notification",
NotificationManager.IMPORTANCE_HIGH);

notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setDescription
("Notifies every 15 minutes to stand up and walk");
mNotificationManager.createNotificationChannel(notificationChannel);
}
}

private void deliverNotification(Context context) {


Intent contentIntent = new Intent(context, MainActivity.class);
PendingIntent contentPendingIntent = PendingIntent.getActivity
(context, NOTIFICATION_ID, contentIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context,
PRIMARY_CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_stand_up)
.setContentTitle("Stand Up Alert")
.setContentText("You should stand up and walk around now!")
.setContentIntent(contentPendingIntent)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setAutoCancel(true)
.setDefaults(NotificationCompat.DEFAULT_ALL);

mNotificationManager.notify(NOTIFICATION_ID, builder.build());
}
}
Output:

You might also like