0% found this document useful (0 votes)
0 views130 pages

Android

The document outlines the creation of several Android applications, including one that demonstrates the activity lifecycle, another that utilizes DatePicker and TimePicker dialogs, and an app that calculates the factorial of a number. It also describes an application that plays audio in the background and a project that changes the color and style of a text view upon button click. Each application includes XML layout files and corresponding Java code for functionality.

Uploaded by

AKNaved Shaikh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
0 views130 pages

Android

The document outlines the creation of several Android applications, including one that demonstrates the activity lifecycle, another that utilizes DatePicker and TimePicker dialogs, and an app that calculates the factorial of a number. It also describes an application that plays audio in the background and a project that changes the color and style of a text view upon button click. Each application includes XML layout files and corresponding Java code for functionality.

Uploaded by

AKNaved Shaikh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 130

SLIP 1

Q1. Create a Simple Application which shows the Life Cycle of Activity.

ANS :-

Activity_main.xml : -

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

<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android: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_constraintEnd_toEndOf="parent"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java :-
package com.example.activitylifecycle;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Toast.makeText(this, "onCreate is called", Toast.LENGTH_SHORT).show();

@Override

protected void onStart() {

super.onStart();

Toast.makeText(this, "OnStart is called", Toast.LENGTH_LONG).show();

@Override

protected void onDestroy() {

super.onDestroy();

Toast.makeText(this, "OnDestroy is called", Toast.LENGTH_LONG).show();

@Override

protected void onResume() {


super.onResume();

Toast.makeText(this, "onResume is called", Toast.LENGTH_LONG).show();

@Override

protected void onRestart() {

super.onRestart();

Toast.makeText(this, "onRestart is called", Toast.LENGTH_LONG).show();

@Override

protected void onStop() {

super.onStop();

Toast.makeText(this, "onStop is called", Toast.LENGTH_LONG).show();

Q2. Create an Android Application that demonstrate DatePicker and


DatePickerDailog.

ANS :-

Activity_main.xml :-

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context=".MainActivity">

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Date : "

android:textSize="30sp"

android:id="@+id/tvDate"

android:layout_marginLeft="20dp"

/>

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Time : "

android:textSize="30sp"

android:id="@+id/tvTime"

android:layout_marginLeft="20dp"

/>

<Button

android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:text="Select Date"

android:onClick="callDPD"

/>

<Button

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Select Time"

android:onClick="callTPD"/>

</LinearLayout>

MainActivity.java :-

package com.example.datepicker;

import androidx.appcompat.app.AppCompatActivity;

import android.app.DatePickerDialog;

import android.app.TimePickerDialog;

import android.os.Bundle;

import android.view.View;

import android.widget.DatePicker;

import android.widget.TextView;

import android.widget.TimePicker;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity {


TextView tvDate;

TextView tvTime;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

tvDate=findViewById(R.id.tvDate);

tvTime=findViewById(R.id.tvTime);

public void callDPD(View view) {

Calendar cldr=Calendar.getInstance();

int day=cldr.get(Calendar.DAY_OF_MONTH);

int month=cldr.get(Calendar.MONTH);

int year=cldr.get(Calendar.YEAR);

DatePickerDialog datePickerDialog=new DatePickerDialog(this, new


DatePickerDialog.OnDateSetListener() {

@Override

public void onDateSet(DatePicker view, int year, int month, int


dayOfMonth) {

tvDate.setText(dayOfMonth + "/" + (month+1) + "/" + year);

},year,month,day);

datePickerDialog.show();
}

public void callTPD(View view) {

Calendar cldr = Calendar.getInstance();

int hour = cldr.get(Calendar.HOUR);

int min = cldr.get(Calendar.MINUTE);

TimePickerDialog timePickerDialog=new TimePickerDialog(this, new


TimePickerDialog.OnTimeSetListener() {

@Override

public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

tvTime.setText(hourOfDay + ":" + minute);

},hour,min,false);

timePickerDialog.show();

}
SLIP 2

Q1. Create a Simple Application, which reads a positive number from the
user and display its factorial value in another activity.
ANS : -

activity_main.xml :-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/edt"
android:hint="Enter positive number"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculate Factorial"
android:id="@+id/btn"
/>
</LinearLayout>

MainActivity.java :-
package com.example.sppu;
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;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText editText;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=findViewById(R.id.edt);
button=findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String numstr=editText.getText().toString();
int num=Integer.parseInt(numstr);
if(num>0){
long fact=1;
for(int i=1;i<=num;i++){
fact*=i;
}
Intent intent=new Intent(MainActivity.this, MainActivity2.class);
intent.putExtra("factorial",fact);
startActivity(intent);
}
else {
Toast.makeText(MainActivity.this, "Number must be positive",
Toast.LENGTH_LONG).show();
}
}
});
}
}

Activity_main2.xml :-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Factorial will print here"
android:id="@+id/tv"
/>
</LinearLayout>

MainActivity2.java :-
package com.example.sppu;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity2 extends AppCompatActivity {
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
textView=findViewById(R.id.tv);
long fact=getIntent().getLongExtra("factorial",0);
textView.setText("Factorial : "+fact);
}
}

Q2. Create an Android application that plays an audio(song) in the


background. Audio will not be stopped even if you switch to another
activity. To stop the audio, you need to stop the service.
ANS :-
Music file in (res/raw/sample.mp3)
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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="30dp"
android:text="Audio Controller" />

<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="48dp"
android:text="start" />

<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_toRightOf="@+id/button1"
android:text="pause" />

<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_toRightOf="@+id/button2"
android:text="stop" />
<Button
android:id="@+id/button4"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button3"
android:layout_toRightOf="@+id/button3"
android:text="Call Second" />
</RelativeLayout>
MainActivity.java :-
package com.example.myapp;

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

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {


Button start, pause, stop, callSecond;
MediaPlayer mp;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
start = findViewById(R.id.button1);
pause = findViewById(R.id.button2);
stop = findViewById(R.id.button3);
callSecond=findViewById(R.id.button4);
mp = MediaPlayer.create(getBaseContext(), R.raw.sample);
// mp.start();
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp.start();
}

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

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

}
});
callSecond.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this, MainActivity2.class);
startActivity(intent);
}
});

}
}

activity_main2.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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I am on Second activity"
android:layout_gravity="center_horizontal"
android:textSize="30sp"
android:layout_marginTop="50dp"
/>
</LinearLayout>

MainActivity2.java :-
package com.example.myapp;

import android.os.Bundle;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity2 extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main2);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main),
(v, insets) -> {
Insets systemBars =
insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right,
systemBars.bottom);
return insets;
});
}
}
SLIP 3

Q1. Create an Android Application that will change color of the College
Name on click of Push Button and change the font size, font style of text
view using xml.
ANS :-
activity_main.xml :-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv"
android:text="Dr. D. Y. Patil ACS College of Akurdi"
android:textSize="20dp"
android:textStyle="normal"
android:textColor="@android:color/black"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Push"
android:id="@+id/btn"
/>
</LinearLayout>

MainActivity.java :-
package com.example.sppu;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
TextView textView;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=findViewById(R.id.tv);
button=findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView.setTextAppearance(R.style.LargeText);
}
});
}
}

styles.xml (in res/values/):-


<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="LargeText">
<item name="android:textSize">32dp</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">#ffffff</item>
</style>
</resources>

Q2. Create an Android Application to find the factorial of a number and


Display the Result on Alert Box.
ANS :-
Activity_main.xml :-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/edt"
android:hint="Enter positive number"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculate Factorial"
android:id="@+id/btn"
/>

</LinearLayout>
MainActivity.java :-
package com.example.sppu;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


EditText editText;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=findViewById(R.id.edt);
button=findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String numstr=editText.getText().toString();
int num=Integer.parseInt(numstr);
if(num>0){
long fact=1;
for(int i=2;i<=num;i++){
fact*=i;
}
AlertDialog.Builder alertDialog = new
AlertDialog.Builder(MainActivity.this);
alertDialog.setTitle("Factorial");
alertDialog.setMessage("Factorial is : "+fact);
alertDialog.setPositiveButton("OK", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
}
else {
Toast.makeText(MainActivity.this, "Number must be positive",
Toast.LENGTH_LONG).show();
}
}
});
}
}
SLIP 4
Q1. Create a Simple Application, that performs Arithmetic Operations.
(Use constraint layout)
ANS :-
Activity_main.xml :-
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<EditText
android:id="@+id/number1EditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:ems="10"
android:hint="Enter number 1"
android:inputType="numberDecimal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<EditText
android:id="@+id/number2EditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:ems="10"
android:hint="Enter number 2"
android:inputType="numberDecimal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/number1EditText" />

<Button
android:id="@+id/addButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="Add"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
android:onClick="performAddition"
app:layout_constraintTop_toBottomOf="@+id/number2EditText" />

<Button
android:id="@+id/subtractButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Subtract"
android:onClick="performSubtraction"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/addButton" />

<Button
android:id="@+id/multiplyButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Multiply"
android:onClick="performMultiplication"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/subtractButton" />

<Button
android:id="@+id/divideButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Divide"
android:onClick="performDivision"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/multiplyButton" />

<TextView
android:id="@+id/resultTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="Result: "
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/divideButton" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java :-
package com.example.sppu;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {

private EditText number1EditText;


private EditText number2EditText;
private TextView resultTextView;

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

number1EditText = findViewById(R.id.number1EditText);
number2EditText = findViewById(R.id.number2EditText);
resultTextView = findViewById(R.id.resultTextView);
}

public void performAddition(View view) {


double number1 =
Double.parseDouble(number1EditText.getText().toString());
double number2 =
Double.parseDouble(number2EditText.getText().toString());
double result = number1 + number2;
resultTextView.setText("Result: " + result);
}

public void performSubtraction(View view) {


double number1 =
Double.parseDouble(number1EditText.getText().toString());
double number2 =
Double.parseDouble(number2EditText.getText().toString());
double result = number1 - number2;
resultTextView.setText("Result: " + result);
}

public void performMultiplication(View view) {


double number1 =
Double.parseDouble(number1EditText.getText().toString());
double number2 =
Double.parseDouble(number2EditText.getText().toString());
double result = number1 * number2;
resultTextView.setText("Result: " + result);
}

public void performDivision(View view) {


double number1 =
Double.parseDouble(number1EditText.getText().toString());
double number2 =
Double.parseDouble(number2EditText.getText().toString());
if (number2 != 0) {
double result = number1 / number2;
resultTextView.setText("Result: " + result);
} else {
resultTextView.setText("Result: Cannot divide by zero");
}
}
}

Q2. Create an android Application for performing the following operation on


the table Customer (id, name, address, phno). (use SQLite database)
i) Insert New Customer Details.
ii) Show All the Customer Details on Toast Message .
ANS :-
Activity_main.xml :-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<EditText
android:id="@+id/edtId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="50dp"
android:drawablePadding="10dp"
android:hint="Enter ID"
android:inputType="number" />

<EditText
android:id="@+id/etCN"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:drawablePadding="10dp"
android:hint="Enter Name"
android:inputType="text"
android:textSize="15sp" />

<EditText
android:id="@+id/etAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:drawablePadding="10dp"
android:hint="Enter Address"
android:inputType="text"
android:textSize="15sp" />

<EditText
android:id="@+id/etPh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawablePadding="5dp"
android:layout_marginLeft="20dp"
android:hint="Enter Phone number"
android:inputType="phone"
android:textSize="15sp" />

<Button
android:id="@+id/btnAddRec"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:text="Add Record"
android:textSize="20sp" />

<Button
android:id="@+id/btnShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:text="Show Record"
android:textSize="20sp" />

</LinearLayout>

MainActivity.java :-
package com.example.db;

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

public class MainActivity extends AppCompatActivity {


Button b1,b2;
EditText et1,et2,et3,et4;
MyDBHelper myDBHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = findViewById(R.id.btnAddRec);
b2 = findViewById(R.id.btnShow);
et1 = findViewById(R.id.edtId);
et2 = findViewById(R.id.etCN);
et3 = findViewById(R.id.etAddress);
et4 = findViewById(R.id.etPh);

myDBHelper = new MyDBHelper(this);

b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int id = Integer.parseInt(et1.getText().toString());
String cn = et2.getText().toString();
String address = et3.getText().toString();
String ph = et4.getText().toString();
boolean result = myDBHelper.addRecord(id, cn, address, ph);
if (result)
Toast.makeText(MainActivity.this, "Record added successfully",
Toast.LENGTH_SHORT).show();
else
Toast.makeText(MainActivity.this, "Error",
Toast.LENGTH_SHORT).show();
}
});

b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String record="";

Cursor resultSet=myDBHelper.showDetails();
if(resultSet.getCount()==0)
Toast.makeText(getApplicationContext(), "No record found",
Toast.LENGTH_LONG).show();
else {
record = resultSet.getInt(0) + resultSet.getString(1) + " " +
resultSet.getString(2)+ " "+
resultSet.getString(3);
while(resultSet.moveToNext()) {
record = record + "\n" +resultSet.getInt(0) + resultSet.getString(1)
+ " " + resultSet.getString(2)+ " "+
resultSet.getString(3);
}
}
Toast.makeText(MainActivity.this, record, Toast.LENGTH_LONG).show();

}
});
}
}

MyDBHelper :-
package com.example.db;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.content.Context;
import android.util.Log;

public class MyDBHelper extends SQLiteOpenHelper {


private static Context context;
private static final String database="mydatabase";
SQLiteDatabase sqLiteDatabase;

MyDBHelper(Context context) {
super(context, database, null, 1);
Log.i("TAG", "Database created");
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table customer(id integer primary key, " +
"cname varchar(30) not null,address varchar(30) not null," +
" phone varchar(10) not null )");
Log.i("TAG", "table created");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}

boolean addRecord(int id, String cn, String address, String ph) {


sqLiteDatabase = getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("id",id);
contentValues.put("cname", cn);
contentValues.put("address", address);
contentValues.put("phone", ph);

long i = sqLiteDatabase.insert("customer", null, contentValues);


if (i > 0)
return true;
else
return false;
}

Cursor showDetails() {
sqLiteDatabase = getReadableDatabase();
Cursor cursor = sqLiteDatabase.rawQuery("select * from customer", null);
if(cursor!=null)
{
cursor.moveToFirst();
}
return cursor;
}
}
SLIP 5
Q1. Create an Android Application to accept two numbers and find power
and Average. Display the result on the next activity on Button click.
ANS :-
activity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<EditText
android:id="@+id/edt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter first number"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
/>

<EditText
android:id="@+id/edt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter first number"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
/>

<Button
android:id="@+id/btnCalculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculate"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"/>

</LinearLayout>

MainActivity.java :-
package com.example.myapplication;

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 editTextNumber1;
EditText editTextNumber2;
Button btnCalculate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextNumber1 = findViewById(R.id.edt1);
editTextNumber2 = findViewById(R.id.edt2);
btnCalculate = findViewById(R.id.btnCalculate);

btnCalculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double num1 =
Double.parseDouble(editTextNumber1.getText().toString());
double num2 =
Double.parseDouble(editTextNumber2.getText().toString());

double power = Math.pow(num1, num2);


double average = (num1 + num2) / 2;

Intent intent = new Intent(MainActivity.this, MainActivity2.class);


intent.putExtra("POWER", power);
intent.putExtra("AVERAGE", average);
startActivity(intent);
}
});
}
}

Activity_main2.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:orientation="vertical"
tools:context=".MainActivity2">

<TextView
android:id="@+id/textViewPower"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Power: "
android:layout_marginTop="30dp"
android:layout_centerHorizontal="true"/>

<TextView
android:id="@+id/textViewAverage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Average: "
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"/>

</LinearLayout>

MainActivity2.java :-
package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;

public class MainActivity2 extends AppCompatActivity {


TextView textViewPower;
TextView textViewAverage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
textViewPower = findViewById(R.id.textViewPower);
textViewAverage = findViewById(R.id.textViewAverage);

double power = getIntent().getDoubleExtra("POWER", 0);


double average = getIntent().getDoubleExtra("AVERAGE", 0);

textViewPower.setText("Power: " + power);


textViewAverage.setText("Average: " + average);
}
}
Q2. Create an Android application that creates a custom Alert Dialog
containing Friends Name and onClick of Friend Name Button greet
accordingly.
ANS :- INCOMPLETE
activity_main.xml :-

MainActivity.java :-
SLIP 6
Q1. Create a Simple Application Which Send ―Hello! message from one
activity to another with help of Button (Use Intent).
ANS :-
activity_main.xml :-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculate"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"/>

</LinearLayout>

MainActivity.java :-
package com.example.myapplication;

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 {

Button btn1;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

btn1 = findViewById(R.id.btn1);

btn1.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

Intent intent = new Intent(MainActivity.this, MainActivity2.class);

intent.putExtra("MESSAGE", "Hello!");

startActivity(intent);

});

activity_main2.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:orientation="vertical"
tools:context=".MainActivity2">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_centerHorizontal="true"/>
</LinearLayout>

MainActivity2.java :-
package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.widget.TextView;

public class MainActivity2 extends AppCompatActivity {

TextView textView;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main2);

textView = findViewById(R.id.textView);

String message = getIntent().getStringExtra("MESSAGE");

textView.setText(message);

}
Q2. Create an Android application to perform following operations on table
Student (Sid ,Sname ,phno). Use autoincrement for Sid and Perform following
Operations.
a) Add Student and display its information.
b) Delete Student
ANS :-
Activity_main.xml :-

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context=".MainActivity">

<EditText

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/sname"

android:hint="Enter Student Name"

android:layout_marginTop="30dp"

android:inputType="text"

/>
<EditText

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/phno"

android:hint="Enter Phone Number"

android:layout_marginTop="30dp"

android:inputType="phone"

/>

<Button

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/insert"

android:text="Insert and Show"

android:layout_marginTop="30dp"

/>

<Button

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/delete"

android:text="Delete"

android:layout_marginTop="30dp"
/>

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/textView"

android:text="Students details will be shown here!"

android:layout_marginTop="20dp"

/>

</LinearLayout>

MainActivity :-

package com.example.mydb;

import androidx.appcompat.app.AppCompatActivity;

import android.database.Cursor;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

import android.widget.Toast;
public class MainActivity extends AppCompatActivity {

MyDBHelpher myDBHelpher;

EditText sname;

EditText phno;

Button add;

Button delete;

TextView textView;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

sname = findViewById(R.id.sname);

phno = findViewById(R.id.phno);

add = findViewById(R.id.insert);

delete = findViewById(R.id.delete);

textView = findViewById(R.id.textView);

myDBHelpher = new MyDBHelpher(this);

add.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

String name = sname.getText().toString();

String number = phno.getText().toString();


boolean result = myDBHelpher.addRecord(name,number);

if (result) {

Toast.makeText(MainActivity.this, "Record added successfully",


Toast.LENGTH_LONG).show();

String record="";

Cursor resultSet=myDBHelpher.showDetails();

if(resultSet.getCount()==0)

Toast.makeText(getApplicationContext(), "No record found",


Toast.LENGTH_LONG).show();

else {

record = resultSet.getInt(0) + resultSet.getString(1) + " " +


resultSet.getString(2);

while(resultSet.moveToNext()) {

record = record + "\n" +resultSet.getInt(0) +


resultSet.getString(1) + " " + resultSet.getString(2);

textView.setText(record);

}else

Toast.makeText(MainActivity.this, "Error",
Toast.LENGTH_LONG).show();

});

}
}

MyDBHelpher :-

package com.example.mydb;

import android.content.ContentValues;

import android.content.Context;

import android.database.Cursor;

import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteOpenHelper;

import android.util.Log;

public class MyDBHelpher extends SQLiteOpenHelper{

SQLiteDatabase sqLiteDatabase;

private static final String database = "mydb";

MyDBHelpher(Context context){

super(context,database,null,1);

Log.i("TAG","Database created");

@Override

public void onCreate(SQLiteDatabase db) {

db.execSQL("create table student(sid integer primary key


autoincrement,sname text not null,phno varchar(10) not null)");

Log.i("TAG","Table created");
}

@Override

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

boolean addRecord(String sname,String phno){

sqLiteDatabase = getWritableDatabase();

ContentValues contentValues = new ContentValues();

contentValues.put("sname",sname);

contentValues.put("phno",phno);

long i = sqLiteDatabase.insert("student",null,contentValues);

return i > 0;

Cursor showDetails(){

sqLiteDatabase = getReadableDatabase();

Cursor cursor =sqLiteDatabase.rawQuery("select * from student",null);

if(cursor!=null){

cursor.moveToFirst();

return cursor;

}
SLIP 7
Q1. Create an Android Application that Demonstrate Radio Button.
ANS :-
activity_main.xml :-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio Buttons"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio Button1"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio Button2"
/>
</RadioGroup>
</LinearLayout>

MainActivity.java :-
package com.example.myapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

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

Q2. Create an Android application to demonstrate phone call using Implicit


Intent.
ANS :-
activity_main.xml :-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:id="@+id/phno"
android:hint="Enter Phone Number"
/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Call"
android:id="@+id/call"
/>
</LinearLayout>

MainActivity.java :-
package com.example.myapp;

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;

public class MainActivity extends AppCompatActivity {

EditText editText;
Button button;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

editText = findViewById(R.id.phno);

button = findViewById(R.id.call);

button.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

String telephone = editText.getText().toString();

Intent intent = new Intent(Intent.ACTION_DIAL);

intent.setData(Uri.parse("tel:" + telephone));

startActivity(intent);

});

}
SLIP 8

Q1. Create an Android App with Login Screen. On successful login, gives
message go to next Activity (Without Using Database& use Table Layout).

ANS :-

activity_main.xml :-

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

tools:context=".MainActivity">

<EditText

android:layout_marginTop="20dp"

android:inputType="text"

android:id="@+id/userName"

android:hint="Enter Username"

/>

<EditText

android:layout_marginTop="20dp"

android:inputType="textPassword"

android:id="@+id/password"
android:hint="Enter Password"

/>

<Button

android:layout_marginTop="20dp"

android:layout_marginLeft="20dp"

android:text="Submit"

android:id="@+id/submit"

/>

</TableLayout>

MainActivity.java :-

package com.example.myapp;

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.Toast;
public class MainActivity extends AppCompatActivity {

EditText userName;

EditText password;

Button button;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

userName = findViewById(R.id.userName);

password = findViewById(R.id.password);

button = findViewById(R.id.submit);

button.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

String user = userName.getText().toString();

String pass = password.getText().toString();

if(user.equals("admin") && pass.equals("1234")){

Toast.makeText(MainActivity.this, "Go to next Activity",


Toast.LENGTH_LONG).show();

else {

Toast.makeText(MainActivity.this, "Username and password is not


correct", Toast.LENGTH_LONG).show();
}

});

Q2. Create application to send email with attachment

ANS :- INCOMPLETE
SLIP 9

Q1. Write an Android application to accept two numbers from the user, and
display them, but reject input if both numbers are greater than 10 and asks
for two new numbers.

ANS :-

activity_main.xml :-

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context=".MainActivity">

<EditText

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="20dp"

android:layout_marginLeft="20dp"

android:inputType="number"

android:id="@+id/num1"

android:hint="Enter first number"

/>
<EditText

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="20dp"

android:layout_marginLeft="20dp"

android:inputType="number"

android:id="@+id/num2"

android:hint="Enter second number"

/>

<Button

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="20dp"

android:layout_marginLeft="20dp"

android:text="Submit"

android:id="@+id/submit"

/>

</LinearLayout>

MainActivity.java :-

package com.example.myapp;
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.Toast;

public class MainActivity extends AppCompatActivity {

EditText num1;

EditText num2;

Button button;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

num1 = findViewById(R.id.num1);

num2 = findViewById(R.id.num2);

button = findViewById(R.id.submit);

button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

int a = Integer.parseInt(num1.getText().toString());

int b = Integer.parseInt(num2.getText().toString());

if(a > 10 && b > 10){

Toast.makeText(MainActivity.this, "Enter number which are less than


10", Toast.LENGTH_SHORT).show();

else {

Toast.makeText(MainActivity.this, a+" "+b,


Toast.LENGTH_SHORT).show();

});

Q2. Create table Company (id, name, address, phno). Create Application for
Performing the following operation on the table.
a) Insert New Company details.
b) Show All Company details
ANS :-

activity_main.xml :-

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context=".MainActivity">

<EditText

android:id="@+id/edtId"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginLeft="20dp"

android:layout_marginTop="50dp"

android:drawablePadding="10dp"

android:hint="Enter ID"

android:inputType="number" />

<EditText

android:id="@+id/etCN"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginLeft="20dp"

android:drawablePadding="10dp"

android:hint="Enter Name"

android:inputType="text"
android:textSize="15sp" />

<EditText

android:id="@+id/etAddress"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginLeft="20dp"

android:drawablePadding="10dp"

android:hint="Enter Address"

android:inputType="text"

android:textSize="15sp" />

<EditText

android:id="@+id/etPh"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:drawablePadding="5dp"

android:layout_marginLeft="20dp"

android:hint="Enter Phone number"

android:inputType="phone"

android:textSize="15sp" />

<Button

android:id="@+id/btnAddRec"

android:layout_width="wrap_content"

android:layout_height="wrap_content"
android:layout_gravity="center"

android:layout_marginLeft="20dp"

android:layout_marginTop="20dp"

android:text="Add Record"

android:textSize="20sp" />

<Button

android:id="@+id/btnShow"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center"

android:layout_marginLeft="20dp"

android:layout_marginTop="20dp"

android:text="Show Record"

android:textSize="20sp" />

</LinearLayout>

MainActivity.java :-

package com.example.db;

import androidx.appcompat.app.AppCompatActivity;

import android.database.Cursor;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;
import android.widget.EditText;

import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

Button b1,b2;

EditText et1,et2,et3,et4;

MyDBHelper myDBHelper;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

b1 = findViewById(R.id.btnAddRec);

b2 = findViewById(R.id.btnShow);

et1 = findViewById(R.id.edtId);

et2 = findViewById(R.id.etCN);

et3 = findViewById(R.id.etAddress);

et4 = findViewById(R.id.etPh);

myDBHelper = new MyDBHelper(this);

b1.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

int id = Integer.parseInt(et1.getText().toString());
String cn = et2.getText().toString();

String address = et3.getText().toString();

String ph = et4.getText().toString();

boolean result = myDBHelper.addRecord(id, cn, address, ph);

if (result)

Toast.makeText(MainActivity.this, "Record added successfully",


Toast.LENGTH_SHORT).show();

else

Toast.makeText(MainActivity.this, "Error",
Toast.LENGTH_SHORT).show();

});

b2.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

String record="";

Cursor resultSet=myDBHelper.showDetails();

if(resultSet.getCount()==0)

Toast.makeText(getApplicationContext(), "No record found",


Toast.LENGTH_LONG).show();

else {

record = resultSet.getInt(0) + resultSet.getString(1) + " " +


resultSet.getString(2)+ " "+
resultSet.getString(3);

while(resultSet.moveToNext()) {

record = record + "\n" +resultSet.getInt(0) +" "+


resultSet.getString(1) + " " + resultSet.getString(2)+ " "+

resultSet.getString(3);

Toast.makeText(MainActivity.this, record, Toast.LENGTH_LONG).show();

});

MyDBHelper.java :-

package com.example.db;

import android.content.ContentValues;

import android.database.Cursor;

import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteOpenHelper;

import android.content.Context;

import android.util.Log;
public class MyDBHelper extends SQLiteOpenHelper {

private static Context context;

private static final String database="mydatabase";

SQLiteDatabase sqLiteDatabase;

MyDBHelper(Context context) {

super(context, database, null, 1);

Log.i("TAG", "Database created");

@Override

public void onCreate(SQLiteDatabase db) {

db.execSQL("create table customer(id integer primary key, " +

"cname varchar(30) not null,address varchar(30) not null," +

" phone varchar(10) not null )");

Log.i("TAG", "table created");

@Override

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

boolean addRecord(int id, String cn, String address, String ph) {

sqLiteDatabase = getWritableDatabase();
ContentValues contentValues = new ContentValues();

contentValues.put("id",id);

contentValues.put("cname", cn);

contentValues.put("address", address);

contentValues.put("phone", ph);

long i = sqLiteDatabase.insert("customer", null, contentValues);

if (i > 0)

return true;

else

return false;

Cursor showDetails() {

sqLiteDatabase = getReadableDatabase();

Cursor cursor = sqLiteDatabase.rawQuery("select * from customer", null);

if(cursor!=null)

cursor.moveToFirst();

return cursor;

}
SLIP 10

Q1. Create an Android Application that Demonstrate Switch and Toggle


Button.

ANS :-

activity_main.xml :-

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context=".MainActivity">

<ToggleButton

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/togglebtn"

/>

<Switch

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Switch"

/>
</LinearLayout>

MainActivity.java :-

package com.example.myapp;

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.Toast;

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

}
Q2. Demonstrate Array Adapter using List View to display list of fruits

ANS :-

Activity_main.xml :-

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context=".MainActivity">

<ListView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/listView"

/>

</LinearLayout>

MainActivity.java :-

package com.example.myapp;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;

import android.net.Uri;

import android.os.Bundle;

import android.view.View;

import android.widget.ArrayAdapter;

import android.widget.Button;

import android.widget.EditText;

import android.widget.ListView;

import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

String fruits[] = {"Apples","Banana","Orange","Mango"};

ArrayList<String> arrayList = new ArrayList<>();

for (String fruit:fruits){

arrayList.add(fruit);

}
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1,arrayList);

ListView listView = findViewById(R.id.listView);

listView.setAdapter(adapter);

}
SLIP 11

Q.1 Create android application to change Font Size, Color and Font Family of
String.
ANS :-

activity_main.xml :-

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context=".MainActivity">

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/tv"

android:text="This is a TextView"

/>

<Button

android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:id="@+id/btn"

android:text="Change"

/>

</LinearLayout>

MainActivity :-

package com.example.myapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;

import android.graphics.Color;

import android.graphics.Typeface;

import android.net.Uri;

import android.os.Bundle;

import android.view.View;

import android.widget.ArrayAdapter;

import android.widget.Button;

import android.widget.EditText;

import android.widget.ListView;

import android.widget.TextView;

import android.widget.Toast;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

TextView textView;

Button button;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

textView=findViewById(R.id.tv);

button=findViewById(R.id.btn);

button.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

textView.setTextSize(40);

textView.setTextColor(Color.BLUE);

textView.setTypeface(null, Typeface.ITALIC);

});

}
Q.2 Create First Activity to accept information like Student First Name,
Middle Name, Last Name, Date of birth, Address, Email ID and display all
information on Second Activity when user click on the Submit button.
ANS :-

activity_main.xml :-

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context=".MainActivity">

<EditText

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/firstName"

android:inputType="text"

android:hint="Enter First Name"

/>

<EditText

android:layout_width="wrap_content"

android:layout_height="wrap_content"
android:id="@+id/middleName"

android:inputType="text"

android:hint="Enter Middle Name"

/>

<EditText

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/lastName"

android:inputType="text"

android:hint="Enter Last Name"

/>

<EditText

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/dob"

android:inputType="date"

android:hint="Enter DOB"

/>

<EditText

android:layout_width="wrap_content"

android:layout_height="wrap_content"
android:id="@+id/addr"

android:inputType="text"

android:hint="Enter Address"

/>

<EditText

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/emailId"

android:inputType="textEmailAddress"

android:hint="Enter Email ID"

/>

<Button

android:id="@+id/btn1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Submit"

android:layout_marginTop="20dp"

/>

</LinearLayout>

MainActivity.java :-
package com.example.myapplication;

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 {

Button btn1;

EditText firstName;

EditText middleName;

EditText lastName;

EditText dob;

EditText addr;

EditText email;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

btn1 = findViewById(R.id.btn1);

firstName = findViewById(R.id.firstName);
middleName = findViewById(R.id.middleName);

lastName = findViewById(R.id.lastName);

dob = findViewById(R.id.dob);

addr = findViewById(R.id.addr);

email = findViewById(R.id.emailId);

btn1.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

Intent intent = new Intent(MainActivity.this, MainActivity2.class);

String fname=firstName.getText().toString();

String lname = lastName.getText().toString();

String mname = middleName.getText().toString();

String dob1 = dob.getText().toString();

String add = addr.getText().toString();

String emailId = email.getText().toString();

intent.putExtra("firstname", fname);

intent.putExtra("lastname",lname);

intent.putExtra("middlename",mname);

intent.putExtra("dob",dob1);

intent.putExtra("addr",add);

intent.putExtra("email",emailId);

startActivity(intent);
}

});

activity_main2.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:orientation="vertical"

tools:context=".MainActivity2">

<TextView

android:id="@+id/textView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginLeft="20dp"

android:layout_marginTop="30dp"

android:layout_centerHorizontal="true"/>

</LinearLayout>

MainActivity2.java :-

package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.widget.TextView;

public class MainActivity2 extends AppCompatActivity {

TextView textView;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main2);

textView = findViewById(R.id.textView);

String message = getIntent().getStringExtra("firstname");

message = message + "\n" + getIntent().getStringExtra("middlename");

message = message + "\n" + getIntent().getStringExtra("lastname");

message = message + "\n" + getIntent().getStringExtra("dob");

message = message + "\n" + getIntent().getStringExtra("addr");

message = message + "\n" + getIntent().getStringExtra("email");

textView.setText(message);

}
SLIP 12

Q1.Create a Simple Application Which Send ―Hi‖ message from one activity
to another with help of Button (Use Intent).

ANS :-

activity_main.xml :-

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context=".MainActivity">

<Button

android:id="@+id/btn1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Send"

android:layout_marginTop="20dp"

android:layout_centerHorizontal="true"/>

</LinearLayout>

MainActivity.java :-

package com.example.myapplication;
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 {

Button btn1;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

btn1 = findViewById(R.id.btn1);

btn1.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

Intent intent = new Intent(MainActivity.this, MainActivity2.class);

intent.putExtra("MESSAGE", "Hi!!");

startActivity(intent);

});

}
activity_main2.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:orientation="vertical"

tools:context=".MainActivity2">

<TextView

android:id="@+id/textView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginLeft="20dp"

android:layout_marginTop="30dp"

android:layout_centerHorizontal="true"/>

</LinearLayout>

MainActivity2.java :-

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.widget.TextView;
public class MainActivity2 extends AppCompatActivity {

TextView textView;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main2);

textView = findViewById(R.id.textView);

String message = getIntent().getStringExtra("MESSAGE");

textView.setText(message);

Q.2 Create an application to demonstrate date and time picker.

ANS :-

activity_main.xml :-

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

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context=".MainActivity">
<DatePicker

android:layout_width="wrap_content"

android:layout_height="wrap_content"

/>

<TimePicker

android:layout_width="wrap_content"

android:layout_height="wrap_content"

/>

</LinearLayout>

Mainctivity.java :-

package com.example.myapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;

import android.graphics.Color;

import android.graphics.Typeface;

import android.net.Uri;

import android.os.Bundle;

import android.view.View;

import android.widget.ArrayAdapter;

import android.widget.Button;

import android.widget.EditText;
import android.widget.ListView;

import android.widget.TextView;

import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

}
SLIP 13

Q1. Create following Vertical Scroll View Creation in Android.

ANS :-

activity_main.xml :-

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

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent">

<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="vertical">

<Button

android:id="@+id/button1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginLeft="30dp"

android:layout_marginTop="30dp"

android:text="Button 1" />

<Button
android:id="@+id/button2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginLeft="30dp"

android:layout_marginTop="30dp"

android:text="Button 2" />

<Button

android:id="@+id/button3"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginLeft="30dp"

android:layout_marginTop="30dp"

android:text="Button 3" />

<Button

android:id="@+id/button4"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginLeft="30dp"

android:layout_marginTop="30dp"

android:text="Button 4" />

<Button
android:id="@+id/button5"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginLeft="30dp"

android:layout_marginTop="30dp"

android:text="Button 5" />

<Button

android:id="@+id/button6"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginLeft="30dp"

android:layout_marginTop="30dp"

android:text="Button 6" />

<Button

android:id="@+id/button7"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginLeft="30dp"

android:layout_marginTop="30dp"

android:text="Button 7" />

<Button
android:id="@+id/button8"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginLeft="30dp"

android:layout_marginTop="30dp"

android:text="Button 8" />

<Button

android:id="@+id/button9"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginLeft="30dp"

android:layout_marginTop="30dp"

android:text="Button 9" />

<Button

android:id="@+id/button10"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginLeft="30dp"

android:layout_marginTop="30dp"

android:text="Button 10" />

<Button
android:id="@+id/button11"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginLeft="30dp"

android:layout_marginTop="30dp"

android:text="Button 11" />

</LinearLayout>

</ScrollView>

MainActivity.java :-

package com.example.myapp;

import android.os.Bundle;

import android.app.Activity;

public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Q2. Write an application to accept a teacher name from user and display the
names of students along with subjects to whom they are teaching.
Create table Student (sno , s_name,s_class,s_addr)
Teacher (tno, t_name, qualification, experience) Student-Teacher has Many to
Many relationship.

ANS :- INCOMPLETE
SLIP 14

Q1. Create a Simple Application which shows Life Cycle of Activity.


{Use log}.

ANS :-

activity_main.xml :-

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context=".MainActivity">

</LinearLayout>

MainActivity.java :-

package com.example.myapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;

import android.graphics.Color;

import android.graphics.Typeface;
import android.net.Uri;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.widget.ArrayAdapter;

import android.widget.Button;

import android.widget.EditText;

import android.widget.ListView;

import android.widget.TextView;

import android.widget.Toast;

import java.util.ArrayList;

import java.util.Locale;

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Log.i("TAG","onCreate method is called");

@Override

protected void onStart() {


super.onStart();

Log.i("TAG","onStart method is called");

@Override

protected void onPause() {

super.onPause();

Log.i("TAG","onPause method is called");

@Override

protected void onResume() {

super.onResume();

Log.i("TAG","onResume method is called");

@Override

protected void onStop() {

super.onStop();

Log.i("TAG","onStop method is called");

@Override

protected void onDestroy() {


super.onDestroy();

Log.i("TAG","onDestroy method is called");

@Override

protected void onRestart() {

super.onRestart();

Log.i("TAG","onRestart method is called");

Q2. Create an Android application to send email.

ANS :- INCOMPLETE

activity_main.xml :-

MainActivity.java :-
SLIP 15

Q1. Design following-add a border to an Android Layout.

ANS:-

activity_main.xml :-

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:padding="40dp"

android:background="@color/white">

<LinearLayout

android:layout_width="match_parent"

android:layout_height="match_parent"

android:gravity="center"

android:background="@android:color/holo_blue_light">

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textSize="50dp"

android:text="Hello World!"

/>

</LinearLayout>
</LinearLayout>

MainActivity.java :-

package com.example.myapp;

import android.os.Bundle;

import android.app.Activity;

public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Q2. Create simple application with Login Screen. On successful login, gives
message go to next Activity (Without Using Database).

ANS:-

activity_main.xml :-

<?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="@android:color/holo_blue_light"

tools:context=".MainActivity">

<EditText

android:layout_marginTop="20dp"

android:layout_marginLeft="50dp"

android:layout_marginRight="50dp"

android:inputType="text"

android:id="@+id/email"

android:hint="Email"

/>

<EditText

android:layout_marginTop="20dp"

android:layout_marginLeft="50dp"

android:layout_marginRight="50dp"

android:inputType="textPassword"

android:id="@+id/password"

android:hint="Password"

/>
<Button

android:layout_marginTop="20dp"

android:layout_marginLeft="50dp"

android:layout_marginRight="50dp"

android:text="Login"

android:id="@+id/login"

/>

<TextView

android:layout_marginTop="20dp"

android:layout_marginLeft="110dp"

android:text="Not a member?Sign up now"

/>

</TableLayout>

MainActivity.java :-

package com.example.mytestapp;

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.Toast;

public class MainActivity extends AppCompatActivity {

EditText userName;

EditText password;

Button button;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

userName = findViewById(R.id.email);

password = findViewById(R.id.password);

button = findViewById(R.id.login);

button.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

String user = userName.getText().toString();

String pass = password.getText().toString();


if(user.equals("admin@gmail.com") &&
pass.equals("1234")){

Toast.makeText(MainActivity.this, "Go to next Activity",


Toast.LENGTH_LONG).show();

else {

Toast.makeText(MainActivity.this, "Username and


password is not correct", Toast.LENGTH_LONG).show();

});

}
SLIP 16

Q1. Create an Android App, it reads the Students Details (Name, Surname,
Class, Gender, Hobbies, Marks) and display the all information in another
activity in table format on click of Submit button.

Ans :-

activity_main.xml :-

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

<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity">

<EditText

android:id="@+id/edtName"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="16dp"

android:ems="10"

android:hint="enter your Name"

android:inputType="text"

app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toTopOf="parent" />

<EditText

android:id="@+id/edtSurName"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="20dp"

android:ems="10"

android:hint="enter your SurName"

android:inputType="text"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintHorizontal_bias="0.502"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toBottomOf="@+id/edtName" />

<EditText

android:id="@+id/edtClass"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="20dp"

android:ems="10"

android:hint="enter your class"


android:inputType="text"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintHorizontal_bias="0.502"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toBottomOf="@+id/edtGender" />

<EditText

android:id="@+id/edtGender"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="16dp"

android:ems="10"

android:hint="enter your gender"

android:inputType="text"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintHorizontal_bias="0.502"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toBottomOf="@+id/edtSurName" />

<Button

android:id="@+id/button"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginBottom="88dp"
android:text="submit"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintStart_toStartOf="parent" />

<EditText

android:id="@+id/edtHobbies"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="20dp"

android:ems="10"

android:hint="enter your hobbies"

android:inputType="text"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintHorizontal_bias="0.502"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toBottomOf="@+id/edtClass" />

<EditText

android:id="@+id/edtMarks"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="20dp"

android:ems="10"
android:hint="enter your marks"

android:inputType="text"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintHorizontal_bias="0.502"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toBottomOf="@+id/edtHobbies" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java :-

package com.example.mytestapp;

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 edtName,edtSurName,edtClass,edtHobbies,edtMarks,edtGender;

Button button;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

edtName=findViewById(R.id.edtName);
edtSurName=findViewById(R.id.edtSurName);

edtClass=findViewById(R.id.edtClass);

edtHobbies=findViewById(R.id.edtHobbies);

edtMarks=findViewById(R.id.edtMarks);

edtGender=findViewById(R.id.edtGender);

button=findViewById(R.id.button);

button.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

Intent intent = new Intent(MainActivity.this, MainActivity2.class);

intent.putExtra("Name", edtName.getText().toString());

intent.putExtra("SurName", edtSurName.getText().toString());

intent.putExtra("Class",edtClass.getText().toString());

intent.putExtra("Hobbies",edtHobbies.getText().toString());

intent.p0utExtra("Marks",edtMarks.getText().toString());

intent.putExtra("Gender",edtGender.getText().toString());

startActivity(intent);

});

activity_main2.xml :-

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

tools:context=".MainActivity2"

tools:layout_editor_absoluteX="0dp"

tools:layout_editor_absoluteY="-52dp">

<TableRow>

<TextView android:text="Name" />

<TextView android:id="@+id/tvName"/>

</TableRow>

<TableRow>

<TextView android:text="Surname" />

<TextView android:id="@+id/tvSurName"/>

</TableRow>

<TableRow>

<TextView android:text="Class" />

<TextView android:id="@+id/tvClass"/>

</TableRow>

<TableRow>

<TextView android:text="Gender" />

<TextView android:id="@+id/tvGender" />


</TableRow>

<TableRow>

<TextView android:text="Marks" />

<TextView android:id="@+id/tvMarks"/>

</TableRow>

<TableRow>

<TextView android:text="Hobbies" />

<TextView android:id="@+id/tvHobbies"/>

</TableRow>

</TableLayout>

MainActivity2.java :-

package com.example.mytestapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.widget.TextView;

public class MainActivity2 extends AppCompatActivity {

TextView tvName,tvSurName,tvClass,tvGender,tvMarks,tvHobbies;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main2);

tvName=findViewById(R.id.tvName);

tvSurName=findViewById(R.id.tvSurName);

tvClass=findViewById(R.id.tvClass);

tvGender=findViewById(R.id.tvGender);

tvMarks=findViewById(R.id.tvMarks);

tvHobbies=findViewById(R.id.tvHobbies);

Bundle extras = getIntent().getExtras();

if (extras != null) {

tvName.setText(extras.getString("Name"));

tvSurName.setText(extras.getString("SurName"));

tvClass.setText(extras.getString("Class"));

tvGender.setText(extras.getString("Gender"));

tvHobbies.setText(extras.getString("Hobbies"));

tvMarks.setText(extras.getString("Marks"));

}
Q2. Create an Android Application that Demonstrate
TimePicker and display Selected Time on TextView.

ANS :-

activity_main.xml :-

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

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

>

<TimePicker

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/tp"

/>

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"
android:id="@+id/tv"

android:text="Selected Time :"

/>

</LinearLayout>

MainActivity.java :-

package com.example.myapp;

import android.os.Bundle;

import android.app.Activity;

import android.widget.DatePicker;

import android.widget.TextView;

import android.widget.TimePicker;

public class MainActivity extends Activity {

TimePicker timePicker;

TextView textView;

@Override

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

timePicker = findViewById(R.id.tp);

textView=findViewById(R.id.tv);

timePicker.setOnTimeChangedListener(new
TimePicker.OnTimeChangedListener() {

@Override

public void onTimeChanged(TimePicker view, int


hourOfDay, int minute) {

String str = hourOfDay+" : "+minute;

textView.setText("Selected Time : "+str);

});

}
SLIP 17

Q1. Write an android code to make phone call using Intent.

ANS :-

Activity_main.xml :-

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

>

<EditText

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:inputType="phone"

android:hint="Enter Phone Number"

android:id="@+id/phno"

/>

<Button

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Call"

android:id="@+id/btn"
/>

</LinearLayout>

MainActivity.java :-

package com.example.myapp;

import android.content.Intent;

import android.net.Uri;

import android.os.Bundle;

import android.app.Activity;

import android.view.View;

import android.widget.Button;

import android.widget.DatePicker;

import android.widget.EditText;

import android.widget.TextView;

import android.widget.TimePicker;

public class MainActivity extends Activity {

EditText editText;

Button button;

Intent intent;

@Override

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

editText=findViewById(R.id.phno);

button = findViewById(R.id.btn);

button.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

String telephone = editText.getText().toString();

intent = new Intent(Intent.ACTION_DIAL);

intent.setData(Uri.parse("tel:" + telephone));

startActivity(intent);

});

Q2. Create an android application that demonstrate Spinner.

ANS :-

Activity_maain.xml :-

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"
android:layout_height="match_parent"

tools:context=".MainActivity">

<Spinner

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/sp"

/>

</LinearLayout>

MainActivity.java :-

package com.example.myapp;

import android.content.Intent;

import android.net.Uri;

import android.os.Bundle;

import android.app.Activity;

import android.view.View;

import android.widget.AdapterView;

import android.widget.ArrayAdapter;

import android.widget.Button;

import android.widget.DatePicker;

import android.widget.EditText;
import android.widget.Spinner;

import android.widget.TextView;

import android.widget.TimePicker;

import android.widget.Toast;

public class MainActivity extends Activity {

Spinner spinner;

ArrayAdapter<String> arrayAdapter;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

spinner = findViewById(R.id.spinner);

String[] str = {"Number 1","Number 2","Number 3","Number 4","Number 5"};

arrayAdapter = new ArrayAdapter<>(this,


android.R.layout.simple_spinner_item,str);

arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropd
own_item);

spinner.setAdapter(arrayAdapter);

spinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {

String selectedItem = parent.getSelectedItem().toString();

Toast.makeText(MainActivity.this, "Selected Item "+selectedItem,


Toast.LENGTH_SHORT).show();

});

}
SLIP 18

Q1. Create an Android Application that Demonstrate Alert Dialog Box.

ANS :-

Activity_main.xml :-

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

>

<Button

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/btn"

android:text="Show Alert Dialog"

/>

</LinearLayout>

MainActivity.java :-

package com.example.myapp;

import android.app.AlertDialog;
import android.content.DialogInterface;

import android.content.Intent;

import android.net.Uri;

import android.os.Bundle;

import android.app.Activity;

import android.view.View;

import android.widget.ArrayAdapter;

import android.widget.Button;

import android.widget.DatePicker;

import android.widget.EditText;

import android.widget.Spinner;

import android.widget.TextView;

import android.widget.TimePicker;

public class MainActivity extends Activity {

Button button;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

button = findViewById(R.id.btn);

button.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {


AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

builder.setTitle("Alert Dialog");

builder.setMessage("This is example of Alert Dialog");

builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

dialog.dismiss();

});

builder.show();

});

Q2Create an Android Application to accept two numbers and find power and
Average. Display the result on the next activity using Context Menu.

ANS :- INCOMPLETE

activity_main.xml :-

MainActivity.java :-

activity_main2.xml :-

MainActivity2.java :-

context_menu.xml(res/menu/) :-
SLIP 19

Q1. Create an Android Application that on/off the bulb using Toggle Button

SLIP 20

Q1. Create Android Program to Change the Image on the Screen.

ANS :-(2 images in res/drawable image1.jpeg & image2.jpeg)

activity_main.xml :-

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

>

<ImageView

android:id="@+id/image"

android:layout_width="wrap_content"

android:layout_height="600dp"

android:src="@drawable/image1" />

<Button

android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:text="Change Image"

android:id="@+id/btn"

/>

</LinearLayout>

MainActivity.java :-

package com.example.myapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

ImageView imageView;

Button button;

private boolean isTrue=true;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

imageView = findViewById(R.id.image);

button = findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

if(isTrue){

imageView.setImageResource(R.drawable.image2);

isTrue=false;

else {

imageView.setImageResource(R.drawable.image1);

isTrue=true;

});

Q2. Demonstrate Array Adapter using List View to display list of Country.

ANS :-

activity_main.xml :-

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"
android:layout_height="match_parent"

android:orientation="vertical"

>

<ListView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/listView"

/>

</LinearLayout>

MainActivity.java :-

package com.example.myapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.widget.ArrayAdapter;

import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

String[] countries= {"India","Russia","Nepal","Bhutan","Japan"};

ArrayList<String> arrayList = new ArrayList<>();

for (String country:countries){

arrayList.add(country);

ArrayAdapter<String> adapter = new ArrayAdapter<>(this,


android.R.layout.simple_list_item_1,arrayList);

ListView listView = findViewById(R.id.listView);

listView.setAdapter(adapter);

You might also like