MAD Programs

You might also like

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

Course :Mobile Application Development (MAD)

List of Assignment to be uploaded on Teams Folder

 Design Registration Activity by using Linear Layout.

<?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"
android:orientation="vertical"
android:gravity="center"
android:background="#8BC34A"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Registration "
android:textStyle="bold"
android:textSize="40dp" />

<EditText
android:layout_width="324dp"
android:layout_height="66dp"
android:text="Username" />

<EditText
android:layout_width="318dp"
android:layout_height="63dp"
android:text="Password" />

<EditText
android:layout_width="324dp"
android:layout_height="66dp"
android:text="Phone No" />

<EditText
android:layout_width="324dp"
android:layout_height="66dp"
android:text="Email Id" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:text="REGISTER"/>

</LinearLayout>
Output

 Design Registration Activity by using Absolute Layout.

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


<AbsoluteLayout
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"
android:orientation="vertical"
android:gravity="center"
android:background="#89A16C"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_x="60dp"
android:layout_y="78dp"
android:text="Registration "
android:textColor="@color/black"
android:textSize="40dp"
android:textStyle="bold" />
<EditText
android:layout_width="324dp"
android:layout_height="66dp"
android:layout_x="16dp"
android:layout_y="174dp"
android:text="Username" />

<EditText
android:layout_width="318dp"
android:layout_height="63dp"
android:layout_x="20dp"
android:layout_y="254dp"
android:text="Password" />

<EditText
android:layout_width="324dp"
android:layout_height="66dp"
android:layout_x="22dp"
android:layout_y="339dp"
android:text="Phone No" />

<EditText
android:layout_width="324dp"
android:layout_height="66dp"
android:layout_x="26dp"
android:layout_y="421dp"
android:text="Email Id" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="40dp"
android:layout_y="528dp"
android:text="REGISTER"
android:textColor="@color/black" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="220dp"
android:layout_y="521dp"
android:text="Login"
android:textColor="@color/black" />

</AbsoluteLayout>

Output:
 Design Registration Activity by using Relative Layout.

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


<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#C6C084"
tools:context=".MainActivity">

<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Registration"
android:textColor="#151514"
android:textSize="40dp"
android:layout_centerInParent="true"
android:layout_alignParentTop="true"/>

<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
android:layout_below="@+id/text1"/>

<EditText
android:id="@+id/phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Phone Number"
android:layout_below="@+id/name"/>

<EditText
android:id="@+id/gmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Gmail"
android:layout_below="@+id/phone"/>

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

<Button
android:id="@+id/reg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:text="Register"
android:layout_marginTop="67dp"
android:layout_marginStart="50dp"
android:layout_marginLeft="50dp"
android:layout_below="@+id/password"/>

<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:text="Login"
android:layout_below="@+id/password"
android:layout_marginTop="65dp"
android:layout_marginStart="90dp"
android:layout_marginLeft="90dp"
android:layout_toEndOf="@+id/reg"
android:layout_toRightOf="@+id/reg"/>

</RelativeLayout>

Output:
 Design Registration Activity by using Table Layout.

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


<TableLayout
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"
android:background="#AADA73"
tools:context=".MainActivity">

<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Registration"
android:textColor="#151514"
android:textSize="40dp"
android:layout_centerInParent="true"
android:layout_alignParentTop="true"/>

<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
android:layout_below="@+id/text1"/>
<EditText
android:id="@+id/phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Phone Number"
android:layout_below="@+id/name"/>

<EditText
android:id="@+id/gmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Gmail"
android:layout_below="@+id/phone"/>

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

<Button
android:id="@+id/reg"
android:layout_width="385dp"
android:layout_height="wrap_content"
android:layout_column="2"
android:layout_below="@+id/password"
android:layout_marginStart="5dp"
android:layout_marginLeft="50dp"
android:layout_marginTop="67dp"
android:text="Register"
android:textColor="@color/black"/>

</TableLayout>

Output:
 Design Login Activity and display entered username and password using Toast.
 Design and develop the program to perform addition of two numbers.

XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#858058"
tools:context=".MainActivity"
tools:layout_editor_absoluteY="81dp">

<!-- Text view for result view-->


<TextView
android:id="@+id/textView_answer"
android:layout_width="100dp"
android:layout_height="25dp"
android:layout_marginLeft="130dp"
android:layout_marginTop="350dp"
android:text="0"
android:textSize="20dp"
android:textStyle="bold" />
<!--take the input first number-->
<EditText
android:id="@+id/editText_first_no"
android:layout_width="150dp"
android:layout_height="40dp"
android:layout_marginLeft="200dp"
android:layout_marginTop="80dp"
android:inputType="number" />
<!-- for message input first number-->
<TextView
android:id="@+id/textView_first_no"
android:layout_width="150dp"
android:layout_height="25dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="80dp"
android:text="First number"
android:textSize="20dp" />

<!--view message -->

<!-- take input for second number -->

<TextView
android:id="@+id/textView_second_no"
android:layout_width="150dp"
android:layout_height="25dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="150dp"
android:text="Second number"
android:textSize="20dp" />

<EditText
android:id="@+id/editText_second_no"
android:layout_width="150dp"
android:layout_height="40dp"
android:layout_marginLeft="200dp"
android:layout_marginTop="150dp"
android:inputType="number"
tools:ignore="MissingConstraints" />

<!-- button for run add logic and view result -->

<Button
android:id="@+id/add_button"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_marginLeft="110dp"
android:layout_marginTop="250dp"
android:text="ADD" />

</RelativeLayout>

Java file
package com.example.a19202b0021;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

EditText number1;
EditText number2;
Button Add_button;
TextView result;
int ans=0;

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

// by ID we can use each component which id is assign in xml file


number1=(EditText) findViewById(R.id.editText_first_no);
number2=(EditText) findViewById(R.id.editText_second_no);
Add_button=(Button) findViewById(R.id.add_button);
result = (TextView) findViewById(R.id.textView_answer);

// Add_button add clicklistener


Add_button.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

// num1 or num2 double type


// get data which is in edittext, convert it to string
// using parse Double convert it to Double type
double num1 = Double.parseDouble(number1.getText().toString());
double num2 = Double.parseDouble(number2.getText().toString());
// add both number and store it to sum
double sum = num1 + num2;
// set it ot result textview
result.setText(Double.toString(sum));
}
});
}
}

Output
 Write a program to create a toggle button to display ON / OFF Bluetooth on the
display screen.

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


<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
tools:context=".MainActivity">

<TextView
android:id="@+id/text1"
android:layout_width="200dp"
android:layout_height="80dp"
android:layout_alignParentTop="true"
android:gravity="center"
android:text="Toggle"
android:textSize="30dp"
tools:ignore="MissingConstraints" />

<ToggleButton
android:id="@+id/t1"
android:layout_width="157dp"
android:layout_height="58dp"
android:layout_below="@+id/text1"
android:layout_centerHorizontal="true"
android:background="@color/purple_700"
tools:ignore="MissingConstraints"
android:gravity="center"/>

</androidx.constraintlayout.widget.ConstraintLayout>

 Write a program to create a simple calculator

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


<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relative1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

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

<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/button4"
android:layout_alignRight="@+id/button4"
android:layout_below="@+id/edt1"
android:layout_marginTop="94dp"
android:text="1" />

<Button
android:id="@+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button1"
android:layout_toLeftOf="@+id/button3"
android:layout_toStartOf="@+id/button3"
android:text="2" />

<Button
android:id="@+id/button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button2"
android:layout_centerHorizontal="true"
android:text="3" />

<Button
android:id="@+id/button4"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_toLeftOf="@+id/button2"
android:text="4" />

<Button
android:id="@+id/button5"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button4"
android:layout_alignLeft="@+id/button2"
android:layout_alignStart="@+id/button2"
android:text="5" />

<Button
android:id="@+id/button6"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button3"
android:layout_alignStart="@+id/button3"
android:layout_below="@+id/button3"
android:text="6" />

<Button
android:id="@+id/button7"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button4"
android:layout_toLeftOf="@+id/button2"
android:text="7" />

<Button
android:id="@+id/button8"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button5"
android:layout_alignStart="@+id/button5"
android:layout_below="@+id/button5"
android:text="8" />

<Button
android:id="@+id/button9"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button6"
android:layout_alignStart="@+id/button6"
android:layout_below="@+id/button6"
android:text="9" />

<Button
android:id="@+id/buttonadd"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/edt1"
android:layout_alignRight="@+id/edt1"
android:layout_alignTop="@+id/button3"
android:layout_marginLeft="46dp"
android:layout_marginStart="46dp"
android:layout_toRightOf="@+id/button3"
android:text="+" />

<Button
android:id="@+id/buttonsub"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/buttonadd"
android:layout_alignLeft="@+id/buttonadd"
android:layout_alignRight="@+id/buttonadd"
android:layout_alignStart="@+id/buttonadd"
android:layout_below="@+id/buttonadd"
android:text="-" />

<Button
android:id="@+id/buttonmul"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/buttonsub"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignStart="@+id/buttonsub"
android:layout_below="@+id/buttonsub"
android:text="*" />

<Button
android:id="@+id/button10"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button7"
android:layout_toLeftOf="@+id/button2"
android:text="." />

<Button
android:id="@+id/button0"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button8"
android:layout_alignStart="@+id/button8"
android:layout_below="@+id/button8"
android:text="0" />

<Button
android:id="@+id/buttonC"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button9"
android:layout_alignStart="@+id/button9"
android:layout_below="@+id/button9"
android:text="C" />

<Button
android:id="@+id/buttondiv"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/buttonmul"
android:layout_alignLeft="@+id/buttonmul"
android:layout_alignRight="@+id/buttonmul"
android:layout_alignStart="@+id/buttonmul"
android:layout_below="@+id/buttonmul"
android:text="/" />

<Button
android:id="@+id/buttoneql"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/buttondiv"
android:layout_alignLeft="@+id/button10"
android:layout_alignRight="@+id/buttondiv"
android:layout_alignStart="@+id/button10"
android:layout_below="@+id/button0"
android:layout_marginTop="37dp"
android:text="=" />

</RelativeLayout>

Java File
package com.example.a19202b0021;

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

public class MainActivity extends AppCompatActivity {

Button button0, button1, button2, button3, button4, button5,


button6,
button7, button8, button9, buttonAdd, buttonSub,
buttonDivision,
buttonMul, button10, buttonC, buttonEqual;
EditText crunchifyEditText;

float mValueOne, mValueTwo;

boolean crunchifyAddition, mSubtract, crunchifyMultiplication,


crunchifyDivision;

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

button0 = (Button) findViewById(R.id.button0);


button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
button5 = (Button) findViewById(R.id.button5);
button6 = (Button) findViewById(R.id.button6);
button7 = (Button) findViewById(R.id.button7);
button8 = (Button) findViewById(R.id.button8);
button9 = (Button) findViewById(R.id.button9);
button10 = (Button) findViewById(R.id.button10);
buttonAdd = (Button) findViewById(R.id.buttonadd);
buttonSub = (Button) findViewById(R.id.buttonsub);
buttonMul = (Button) findViewById(R.id.buttonmul);
buttonDivision = (Button) findViewById(R.id.buttondiv);
buttonC = (Button) findViewById(R.id.buttonC);
buttonEqual = (Button) findViewById(R.id.buttoneql);
crunchifyEditText = (EditText) findViewById(R.id.edt1);

button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
crunchifyEditText.setText(crunchifyEditText.getText()
+ "1");
}
});

button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
crunchifyEditText.setText(crunchifyEditText.getText()
+ "2");
}
});

button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
crunchifyEditText.setText(crunchifyEditText.getText()
+ "3");
}
});

button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
crunchifyEditText.setText(crunchifyEditText.getText()
+ "4");
}
});

button5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
crunchifyEditText.setText(crunchifyEditText.getText()
+ "5");
}
});

button6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
crunchifyEditText.setText(crunchifyEditText.getText()
+ "6");
}
});

button7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
crunchifyEditText.setText(crunchifyEditText.getText()
+ "7");
}
});

button8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
crunchifyEditText.setText(crunchifyEditText.getText()
+ "8");
}
});

button9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
crunchifyEditText.setText(crunchifyEditText.getText()
+ "9");
}
});

button0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
crunchifyEditText.setText(crunchifyEditText.getText()
+ "0");
}
});
buttonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

if (crunchifyEditText == null) {
crunchifyEditText.setText("");
} else {
mValueOne =
Float.parseFloat(crunchifyEditText.getText() + "");
crunchifyAddition = true;
crunchifyEditText.setText(null);
}
}
});

buttonSub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mValueOne =
Float.parseFloat(crunchifyEditText.getText() + "");
mSubtract = true;
crunchifyEditText.setText(null);
}
});

buttonMul.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mValueOne =
Float.parseFloat(crunchifyEditText.getText() + "");
crunchifyMultiplication = true;
crunchifyEditText.setText(null);
}
});

buttonDivision.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
mValueOne =
Float.parseFloat(crunchifyEditText.getText() + "");
crunchifyDivision = true;
crunchifyEditText.setText(null);
}
});

buttonEqual.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mValueTwo =
Float.parseFloat(crunchifyEditText.getText() + "");

if (crunchifyAddition == true) {
crunchifyEditText.setText(mValueOne + mValueTwo +
"");
crunchifyAddition = false;
}

if (mSubtract == true) {
crunchifyEditText.setText(mValueOne - mValueTwo +
"");
mSubtract = false;
}

if (crunchifyMultiplication == true) {
crunchifyEditText.setText(mValueOne * mValueTwo +
"");
crunchifyMultiplication = false;
}

if (crunchifyDivision == true) {
crunchifyEditText.setText(mValueOne / mValueTwo +
"");
crunchifyDivision = false;
}
}
});

buttonC.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
crunchifyEditText.setText("");
}
});

button10.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
crunchifyEditText.setText(crunchifyEditText.getText()
+ ".");
}
});
}
}

Output :
.

 Write a program to show five checkboxes and toast selected checkboxes.

XML File
<?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"
android:orientation="vertical"
android:padding="10dp"
tools:context=".MainActivity">

<CheckBox
android:id="@+id/cb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android"
android:textSize="25dp"
android:layout_gravity="center"
android:padding="10dp"
android:checked="false"/>

<CheckBox
android:id="@+id/cb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pythpn"
android:textSize="25dp"
android:layout_gravity="center"
android:padding="10dp"
android:checked="false"/>
<CheckBox
android:id="@+id/cb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PHP"
android:textSize="25dp"
android:layout_gravity="center"
android:padding="10dp"
android:checked="false"/>
<CheckBox
android:id="@+id/cb4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C"
android:textSize="25dp"
android:layout_gravity="center"
android:padding="10dp"
android:checked="false"/>
<CheckBox
android:id="@+id/cb5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Java"
android:textSize="25dp"
android:layout_gravity="center"
android:padding="10dp"
android:checked="false"/>

<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:textSize="35dp"
android:layout_gravity="center"
android:padding="10dp"/>
</LinearLayout>

Java File
package com.example.a19202b0021;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;
import android.widget.Button;

import android.widget.CheckBox;

import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

CheckBox cb1, cb2, cb3, cb4, cb5;

Button b;

StringBuffer sb;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

sb = new StringBuffer("You Selected");

b = findViewById(R.id.btn);

cb1 = findViewById(R.id.cb1);

cb2 = findViewById(R.id.cb2);

cb3 = findViewById(R.id.cb3);

cb4 = findViewById(R.id.cb4);

cb5 = findViewById(R.id.cb5);

b.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

if (cb1.isChecked())

sb.append("Android");

if (cb2.isChecked())

sb.append("Python");
if (cb3.isChecked())

sb.append("PHP");

if (cb4.isChecked())

sb.append("Java");

if (cb5.isChecked())

sb.append("C");

Toast.makeText(getApplicationContext(), sb,
Toast.LENGTH_SHORT).show();

});

Output :
 Write a program to use the radio Buttons.

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

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select your Subject ?"
android:textStyle="bold"
android:layout_marginLeft="10dp"
android:textSize="20dp"/>

<!-- add RadioGroup which contain the many RadioButton-->


<RadioGroup
android:layout_marginTop="50dp"
android:id="@+id/groupradio"
android:layout_marginLeft="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<!-- In RadioGroup create the 1 Radio Button-->


<!-- like this we will add some more Radio Button-->
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/radia_id1"
android:text="DBMS"
android:textSize="20dp"/>

<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/radia_id2"
android:text="C/C++ Programming"
android:textSize="20dp"/>

<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/radia_id3"
android:text="Data Structure"
android:textSize="20dp"/>

</RadioGroup>

<!-- add button For Submit the Selected item-->


<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:id="@+id/submit"
android:textStyle="bold"
android:textSize="20dp"
android:layout_marginTop="200dp"
android:layout_marginLeft="180dp"
/>

<!-- add clear button for clear the selected item-->


<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear"
android:id="@+id/clear"
android:textSize="20dp"
android:textStyle="bold"
android:layout_marginTop="200dp"
android:layout_marginLeft="20dp"
/>

<RadioButton
android:id="@+id/radia_id4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Algorithms"
android:textSize="20dp" />

</RelativeLayout>

Java File
package com.example.a19202b0021;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

// Define the object for Radio Group,


// Submit and Clear buttons
private RadioGroup radioGroup;
Button submit, clear;

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

// Bind the components to their respective objects


// by assigning their IDs
// with the help of findViewById() method
submit = (Button)findViewById(R.id.submit);
clear = (Button)findViewById(R.id.clear);
radioGroup = (RadioGroup)findViewById(R.id.groupradio);
// Uncheck or reset the radio buttons initially
radioGroup.clearCheck();

// Add the Listener to the RadioGroup


radioGroup.setOnCheckedChangeListener(
new RadioGroup
.OnCheckedChangeListener() {
@Override

// The flow will come here when


// any of the radio buttons in the radioGroup
// has been clicked

// Check which radio button has been clicked


public void onCheckedChanged(RadioGroup group,
int checkedId)
{

// Get the selected Radio Button


RadioButton
radioButton
= (RadioButton)group
.findViewById(checkedId);
}
});

// Add the Listener to the Submit Button


submit.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v)
{

// When submit button is clicked,


// Ge the Radio Button which is set
// If no Radio Button is set, -1 will be returned
int selectedId =
radioGroup.getCheckedRadioButtonId();
if (selectedId == -1) {
Toast.makeText(MainActivity.this,
"No answer has been selected",
Toast.LENGTH_SHORT)
.show();
}
else {

RadioButton radioButton
= (RadioButton)radioGroup
.findViewById(selectedId);

// Now display the value of selected item


// by the Toast message
Toast.makeText(MainActivity.this,
radioButton.getText(),
Toast.LENGTH_SHORT)
.show();
}
}
});

// Add the Listener to the Submit Button


clear.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v)
{

// Clear RadioGroup
// i.e. reset all the Radio Buttons
radioGroup.clearCheck();
}
});
}
}

Output:
 Write a program to display different name of cities in List View and display toast
message when particular city is selected.

XML.File

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

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="fill_parent"
/>
</RelativeLayout>

Mylist.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:padding="2dp"
android:textColor="#4d4d4d"
/>

String.xml
<resources>
<string name="app_name">ListView</string>
<string-array name="array_technology">
<item>Mumbai</item>
<item>Delhi</item>
<item>Bangalore </item>
<item>Hyderabad</item>
<item>Ahmedabad</item>
<item>Chennai</item>
<item>Kolkata</item>
<item>Surat</item>
<item>Pune</item>
<item>Jaipur</item>
<item>Lucknow</item>
<item>Kanpur</item>
</string-array>
</resources>

Java File
package com.example.a19202b0021;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


ListView listView;
TextView textView;
String[] listItem;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=(ListView)findViewById(R.id.listView);
textView=(TextView)findViewById(R.id.textView);
listItem =
getResources().getStringArray(R.array.array_technology);
final ArrayAdapter<String> adapter = new
ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
android.R.id.text1, listItem);
listView.setAdapter(adapter);

listView.setOnItemClickListener(new
AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View
view, int position, long l) {
// TODO Auto-generated method stub
String value=adapter.getItem(position);

Toast.makeText(getApplicationContext(),value,Toast.LENGTH_SHORT).show
();

}
});
}
}

Output :
 Write a program to add buttons in Grid View.

XML File:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="40dp"
tools:context=".MainActivity">

<GridView
android:id="@+id/g"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#A383DD"
android:dividerHeight="1dp"
android:numColumns="2"/>

</RelativeLayout>

Mylist.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:id="@+id/textview"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:padding="4dp"
android:textColor="#0000FF" />

Java file :
package com.example.a19202b0021;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


GridView gridView;
TextView t1;
String[] city={"Bangalore", "Mumbai", "Kolkata", "Ahmedabad",
"Jaipur", "Lucknow", "Kanpur" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

gridView=findViewById(R.id.g);
t1=findViewById(R.id.textview);

ArrayAdapter adapter=new
ArrayAdapter(this,R.layout.mylist,R.id.textview,city);

gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new
AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
String element="City Is: "+adapter.getItem(position);

Toast.makeText(getApplicationContext(),element,Toast.LENGTH_LONG).sho
w();
}
});

}
}

Output:
 Write a program to create a HelloWorld Activity using all lifecycles methods to
display messages using Log.d.

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

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

package com.example.a19202b0021;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("Life cycle","Create method");
}

@Override
protected void onStart() {
super.onStart();
Log.d("Life cycle","start method");
}

@Override
protected void onResume() {
super.onResume();
Log.d("Life cycle","Resume method");
}
@Override
protected void onPause(){
super.onPause();
Log.d("Life cycle","pause method");
}
@Override
protected void onStop(){
super.onStop();
Log.d("Life cycle","stop method");
}
@Override
public void onDestroy(){
super.onDestroy();
Log.d("Life cycle","destroy method");
}

@Override
public void onRestart(){
super.onResort();
Log.d("Life cycle","Restart method");
}
}

 Write a program to create a text field and a button “Navigate”. When you enter
“www.google.com” and press navigate button it should open google page.

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#9F8B71"
tools:context=".MainActivity">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Explicit Intent Example"
android:id="@+id/b1" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/b1"
android:text="Implicit Intent Example"
android:id="@+id/b2"/>
</RelativeLayout>

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

xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is Second Activity"
android:id="@+id/textView" />

</RelativeLayout>

Java file
package com.example.a19202b0021;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity


{
Button b1,b2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button)findViewById(R.id.b1);
b2 = (Button) findViewById(R.id.b2);

b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
Intent in = new Intent(getBaseContext(),
MainActivity2.class);
startActivity(in);
}
});

b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Intent intent = new Intent(Intent.ACTION_VIEW);


intent.setData(Uri.parse("https://www.google.com"));
startActivity(intent);
}
});
}}

SecondActivity.java
package com.example.a19202b0021;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.Toast;

public class MainActivity2 extends AppCompatActivity {


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

Toast.makeText(getApplicationContext(), "We are moved to


second Activity",Toast.LENGTH_LONG).show();
}}

Output :
 Write a program to create button “Start Dialer”. When u click on this button it
should open the phone dialer.

XML File
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="40dp"
tools:context=".MainActivity">
<Button
android:id="@+id/b1"
android:text="Dialpad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>

Java file
package com.example.a19202b0021;

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

public class MainActivity extends AppCompatActivity {


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

b=findViewById(R.id.b1);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent(Intent.ACTION_DIAL);
startActivity(i);

}
});
}
}

Output :
 Write a program to create two screens. First screen will take one number input
from user. After click on Factorial button, second screen will open and it should
display factorial of the same number. Also specify which type of intent you will
use in this case.

MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="40dp"
android:background="@color/purple_200"
tools:context=".MainActivity">
<EditText
android:id="@+id/e1"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textAlignment="center"/>
<Button
android:id="@+id/b1"
android:text="Factorial"
android:layout_below="@+id/e1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"/>
</RelativeLayout>

MainActivity.java
package com.example.a19202b0021;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {


EditText e;
Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b=findViewById(R.id.b1);
e=findViewById(R.id.e1);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String str=String.valueOf(e.getText());
Intent i=new
Intent(MainActivity.this,MainActivity2.class);
i.putExtra("msg_key",str);
startActivity(i);
}
});
}
}

MainAtivity2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:id="@+id/f1"
tools:context=".MainActivity2">

<TextView
android:id="@+id/t1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textAlignment="center"
android:textSize="100px"
android:textStyle="bold"/>
<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/t1"
android:layout_centerHorizontal="true"
android:text="BACK"/>
</RelativeLayout>

MainActivity.java
package com.example.a19202b0021;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity2 extends AppCompatActivity {


TextView t;
Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
t=findViewById(R.id.t1);
Intent a=getIntent();
String str=a.getStringExtra("msg_key");
int num=Integer.parseInt(str);
int fact=1;
for(int i=1;i<=num;i++)
{
fact=fact*i;
}
String s=String.valueOf(fact);
t.setText(s);

b=findViewById(R.id.b1);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new
Intent(MainActivity2.this,MainActivity.class);
startActivity(i);
}
});
}
}

Output:

 Write a program to start a Music using service.


 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#DFEABB"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:text="Service"
android:textColor="@color/black"
android:textSize="35dp"
android:textStyle="bold" />

<Button
android:id="@+id/B1"
android:layout_width="190dp"
android:layout_height="60dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="190dp"
android:text="Start Service" />

<Button
android:id="@+id/B2"
android:layout_width="197dp"
android:layout_height="61dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="290dp"
android:text="Stop Service" />

</RelativeLayout>

 Java file

 package com.example.a19202b0021;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {


Button b1,b2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=findViewById(R.id.B1);
b2=findViewById(R.id.B2);

b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startService(new
Intent(getApplicationContext(),MyService.class));
}
});

b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
stopService(new
Intent(getApplicationContext(),MyService.class));
}
});

}
}

 Myservice

 package com.example.a19202b0021;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.widget.Toast;

public class MyService extends Service {


MediaPlayer mp;
public MyService() {
}

@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet
implemented");
}

@Override
public int onStartCommand(Intent intent, int flags, int
startId) {
mp=MediaPlayer.create(this,R.raw.song);
mp.setLooping(true);
mp.start();
Toast.makeText(getApplicationContext(), "Service
Started", Toast.LENGTH_SHORT).show();
return START_STICKY;
}

@Override
public void onDestroy() {
super.onDestroy();
mp.stop();
Toast.makeText(getApplicationContext(), "Service
Stopped", Toast.LENGTH_SHORT).show();
}
}

 Output

 Write a program to demonstrate all the system broadcast messages.

MainActivity.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"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">

</LinearLayout>
MainActivity.java
package com.example.a19202b0021;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {


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

@Override
protected void onStart() {
super.onStart();
IntentFilter intentFilter=new IntentFilter();
intentFilter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intentFilter.addAction(Intent.ACTION_POWER_CONNECTED);
intentFilter.addAction(Intent.ACTION_POWER_DISCONNECTED);
intentFilter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
this.registerReceiver(myReceiver,intentFilter);

@Override
protected void onStop() {
super.onStop();
this.unregisterReceiver(myReceiver);
}
}
MyReceiver.java
package com.example.a19202b0021;

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

public class MyReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

String actionstring=intent.getAction();

Toast.makeText(context,actionstring,Toast.LENGTH_LONG).show();
}
}

Output:
 Write a program to capture an image and display it using image view.

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="40dp"
android:background="#92DF95"
android:orientation="horizontal"
tools:context=".MainActivity">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="CAMERA"
android:id="@+id/text"
android:textSize="20dp"
android:gravity="center"/>

<ImageView
android:id="@+id/image"
android:layout_width="371dp"
android:layout_height="217dp"
android:layout_below="@+id/text"
android:layout_marginTop="81dp"
android:src="@drawable/image" />

<Button
android:id="@+id/photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/image"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="TAKE PHOTO" />

</RelativeLayout>

Java file

package com.example.a19202b0021;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {


Button b1;
ImageView imageView;
int CAMERA_REQUEST=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

b1=findViewById(R.id.photo);
imageView=findViewById(R.id.image);

b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);


startActivityForResult(i,CAMERA_REQUEST);
}
});

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

if (requestCode==CAMERA_REQUEST)
{
Bitmap image= (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(image);
}
}
}

Output:

 Write a program to rotate the image in clockwise/anticlockwise, Zoom IN/Zoom


OUT, Fade IN/Fade OUT by using the following GUI.

Animation (res>anim>xml)
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#9ECF75"
tools:context=".MainActivity">

<ImageView
android:id="@+id/imageView"
android:layout_width="340dp"
android:layout_height="416dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:src="@drawable/image" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:onClick="blink"
android:text="Blink" />

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView"
android:layout_marginTop="20dp"
android:onClick="rotate"
android:text="Rotate" />

<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView"
android:layout_marginTop="75dp"
android:layout_marginRight="10dp"
android:onClick="fade"
android:text="Fade" />

<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView"
android:layout_centerHorizontal="true"
android:layout_marginTop="75dp"
android:layout_marginRight="10dp"
android:onClick="move"
android:text="Move" />

<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView"
android:layout_marginLeft="250dp"
android:layout_marginTop="20dp"
android:onClick="slide"
android:text="Slide" />

</RelativeLayout>

Java file
package com.example.anim;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

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

public void blink(View view){


ImageView image = (ImageView)findViewById(R.id.imageView);
Animation animation1 =
AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.blink);
image.startAnimation(animation1);
}

public void rotate(View view){


ImageView image = (ImageView)findViewById(R.id.imageView);
Animation animation1 =
AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.rotate);
image.startAnimation(animation1);
}

public void fade(View view){


ImageView image = (ImageView)findViewById(R.id.imageView);
Animation animation1 =
AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.fade);
image.startAnimation(animation1);
}

public void move(View view){


ImageView image = (ImageView)findViewById(R.id.imageView);
Animation animation1 =
AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.move);
image.startAnimation(animation1);
}

public void slide(View view){


ImageView image = (ImageView)findViewById(R.id.imageView);
Animation animation1 =
AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.slide);
image.startAnimation(animation1);
}
}
Slide.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true" >

<scale
android:duration="500"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:interpolator="@android:anim/linear_interpolator"
android:toXScale="1.0"
android:toYScale="0.0" />

</set>

Move .xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator"
android:fillAfter="true">

<translate
android:fromXDelta="0%p"
android:toXDelta="75%p"
android:duration="800" />

</set>

Fade.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

<alpha
android:fromAlpha="0"
android:toAlpha="1"
android:duration="2000" >
</alpha>

<alpha
android:startOffset="2000"
android:fromAlpha="1"
android:toAlpha="0"
android:duration="2000" >
</alpha>

</set>

Blink.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="0.0"
android:toAlpha="1.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:duration="600"
android:repeatMode="reverse"
android:repeatCount="infinite"/>
</set>

Rotate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

<rotate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="5000" >
</rotate>

<rotate
xmlns:android="http://schemas.android.com/apk/res/android"
android:startOffset="5000"
android:fromDegrees="360"
android:toDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="5000" >
</rotate>

</set>

Output :
Ratate
Blink

Fade
Move

 Write a program to display Mumbai in map using MapActivity.


 Example of Google Map
 Let's create an example of Google map integrating within our app. For
doing this we select Google Maps Activity.

 Copy the URL from google_map_api.xml file to generate Google map key.


 Paste the copied URL at the browser. It will open the following page.


 Click on Create API key to generate API key.


 After clicking on Create API key, it will generate our API key displaying the
following screen.

 Copy this generated API key in our google_map_api.xml file

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


<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
tools:context=".MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment"/>

Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.a19202b0021" >

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


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

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.19202B0021"
tools:targetApi="31" >

<!--
TODO: Before you run your application, you need a Google Maps API key.

To get one, follow the directions here:

https://developers.google.com/maps/documentation/android-sdk/get-api-key
Once you have your API key (it starts with "AIza"), define a new property in your
project's local.properties file (e.g. MAPS_API_KEY=Aiza...), and replace the
"YOUR_API_KEY" string in this file with "${MAPS_API_KEY}".
-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="YOUR_API_KEY" />

<activity
android:name=".MapsActivity"
android:exported="true"
android:label="@string/title_activity_maps" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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


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

</manifest>

Java
package com.example.a19202b0021;

import androidx.fragment.app.FragmentActivity;

import android.os.Bundle;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;

import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng mumbai = new LatLng(19.085442, 72.875694);
mMap.addMarker(new MarkerOptions().position(mumbai).title("Marker in Mumbai"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(mumbai, 10F));
}
}

You might also like