Android App: Activity Lifecycle Demo
Android App: Activity Lifecycle Demo
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="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
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>
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
Log.d([Link](this), "");
Log.d([Link](this), "onCreate");
}
@Override
protected void onStart(){
[Link]();
Log.d([Link](this), "onStart");
}
@Override
protected void onPause(){
[Link]();
Log.d([Link](this), "onPause");
}
@Override
protected void onRestart(){
[Link]();
Log.d([Link](this), "onRestart");
}
@Override
protected void onResume(){
Pramod Navle.
SYCS24
[Link]();
Log.d([Link](this),"onResume");
}
@Override
protected void onStop(){
[Link]();
Log.d([Link](this), "onStop");
}
@Override
protected void onDestroy(){
[Link]();
Log.d([Link](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"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="[Link]">
<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/[Link]"
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" />
</[Link]>
Activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="[Link]">
<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/[Link]"
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" />
</[Link]>
Pramod Navle.
SYCS24
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
mMessageEditText = findViewById([Link].editText_main);
mReplyHeadTextView = findViewById([Link].text_header_reply);
mReplyTextView = findViewById([Link].text_message_reply);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
[Link](requestCode, resultCode, data);
if (requestCode == TEXT_REQUEST) {
if (resultCode == RESULT_OK) {
String reply = [Link](SecondActivity.EXTRA_REPLY);
[Link]([Link]);
[Link](reply);
[Link]([Link]);
Pramod Navle.
SYCS24
}
}
}
}
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_second);
mReply = findViewById([Link].editText_second);
Practical 6
Aim: Create an android app that demonstrates the use of Keyboards, Input Controls, Alerts, and
Pickers
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int
i, long l) {
String spinnerLabel = [Link](i).toString();
displayToast(spinnerLabel);
}
Pramod Navle.
SYCS24
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
activity_order.xml
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
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" />
</[Link]>
[Link]
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
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" />
</[Link]>
Output:
Pramod Navle.
SYCS24
Practical No: 7
Aim: Create an android app that demonstrates the use of an Options Menu
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
[Link](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([Link].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 [Link].
int id = [Link]();
//noinspection SimplifiableIfStatement
if (id == [Link].action_order) {
return true;
}
return [Link](item);
}
public void displayToast(String message) {
[Link](getApplicationContext(), message,
Toast.LENGTH_SHORT).show();
}
Pramod Navle.
SYCS24
public void showDonutOrder(View view) {
//displayToast(getString([Link].donut_order_message));
mOrderMessage = getString([Link].donut_order_message);
displayToast(mOrderMessage);
}
public void showIceCreamOrder(View view) {
//displayToast(getString([Link].ice_cream_order_message));
mOrderMessage = getString([Link].ice_cream_order_message);
displayToast(mOrderMessage);
}
public void showFroyoOrder(View view) {
//displayToast(getString([Link].froyo_order_message));
mOrderMessage = getString([Link].froyo_order_message);
displayToast(mOrderMessage);
}
}
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<[Link]
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/[Link]">
<[Link]
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/[Link]" />
</[Link]>
<[Link]
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" />
</[Link]>
Menu_main.xml
<menu xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
tools:context="[Link]">
<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"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
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" />
</[Link]>
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
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int
i, long l) {
String spinnerLabel = [Link](i).toString();
displayToast(spinnerLabel);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
Pramod Navle.
SYCS24
}
Activity_order.xml
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
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" />
</[Link]>
[Link]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="[Link]
package="[Link]">
<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/[Link]">
<intent-filter>
<action android:name="[Link]" />
android:parentActivityName=".MainActivity">
<meta-data android:name="[Link].PARENT_ACTIVITY"
android:value=".MainActivity"/>
</activity>
</application>
</manifest>
Pramod Navle.
SYCS24
[Link]
<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"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
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" />
</[Link]>
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
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) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
IntentFilter filter = new IntentFilter();
[Link](Intent.ACTION_POWER_DISCONNECTED);
[Link](Intent.ACTION_POWER_CONNECTED);
[Link](mReceiver, filter);
[Link](this)
.registerReceiver(mReceiver,
new IntentFilter(ACTION_CUSTOM_BROADCAST));
}
protected void onDestroy() {
//Unregister the receiver
[Link](mReceiver);
[Link]();
Pramod Navle.
SYCS24
[Link](this)
.unregisterReceiver(mReceiver);
}
public void sendCustomBroadcast(View view) {
Intent customBroadcastIntent = new Intent(ACTION_CUSTOM_BROADCAST);
[Link](this).sendBroadcast(customBroadcastIntent);
}}
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
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 = [Link]();
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;
}
[Link](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
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
button_notify = findViewById([Link]);
button_notify.setOnClickListener(new [Link]() {
@Override
public void onClick(View view) {
sendNotification();
}
});
createNotificationChannel();
}
public void sendNotification() {
if ([Link].SDK_INT >=
[Link].VERSION_CODES.O) {
// Create a NotificationChannel
NotificationChannel notificationChannel = new NotificationChannel(PRIMARY_CHANNEL_ID,
"Mascot Notification", NotificationManager
.IMPORTANCE_HIGH);
[Link](true);
[Link]([Link]);
[Link](true);
[Link]("Notification from Mascot");
[Link](notificationChannel);
}
}
private [Link] getNotificationBuilder(){
Intent notificationIntent = new Intent(this, [Link]);
PendingIntent notificationPendingIntent = [Link](this,
NOTIFICATION_ID, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
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" />
</[Link]>
Output:
Pramod Navle.
SYCS24
B)ALARM MANAGER:
*activity_main.xml*:
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
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" />
</[Link]>
*[Link]*:
package [Link].alarm_manager;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
"primary_notification_channel";
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
ToggleButton alarmToggle = findViewById([Link].alarm_Toggle);
[Link](
new [Link]() {
@Override
public void onCheckedChanged(CompoundButton compoundButton,
boolean isChecked) {
String toastMessage;
if(isChecked){
//Set the toast message for the "on" case.
deliverNotification([Link]);
toastMessage = "Stand Up Alarm On!";
} else {
//Set the toast message for the "off" case.
[Link]();
toastMessage = "Stand Up Alarm Off!";
}
mNotificationManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
createNotificationChannel();
"Stand up notification",
NotificationManager.IMPORTANCE_HIGH);
[Link](true);
[Link]([Link]);
[Link](true);
[Link]
("Notifies every 15 minutes to stand up and walk");
[Link](notificationChannel);
}
}
[Link](NOTIFICATION_ID, [Link]());
}
}
Output: