Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

SDPD Solutions

Download as pdf or txt
Download as pdf or txt
You are on page 1of 43

Ramaiah Institute of Technology

(Autonomous Institute, Affiliated to VTU)


Department of Computer Science & Engineering
SDPD LAB SOLUTIONS
(M.Tech CSE) (Autonomous Syllabus)

1. Design an android application for “Registration Form”. The main UI will have
UserName, Password(Length > 8), Confirm Password, Mobile Number (Length > 10),
Email Id(Validate Email ID). Display appropriate Toast Message for wrong inputs. If
the details are correct then display the UserName, Password (Only last 3 characters to
be displayed, rest characters as *), Mobile Number and Email ID.

Solution:

activity_main.xml

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


<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.rit.ibmsap.registrationform.MainActivity"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="33dp"
android:layout_marginEnd="13dp"
android:layout_marginStart="13dp"
android:text="Register"
android:textAlignment="center"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toTopOf="@+id/editText"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="34dp"
tools:layout_constraintLeft_creator="1"
android:layout_marginBottom="27dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="42dp"
android:ems="10"
android:hint="UserName"
android:inputType="text|textPersonName"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toTopOf="@+id/editText2"
tools:layout_constraintLeft_creator="1"
android:layout_marginBottom="27dp"
app:layout_constraintLeft_toLeftOf="@+id/textView" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Password"
android:inputType="textPassword|textWebPassword"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toTopOf="@+id/editText3"
app:layout_constraintRight_toRightOf="@+id/editText"
tools:layout_constraintLeft_creator="1"
android:layout_marginBottom="27dp"
app:layout_constraintLeft_toLeftOf="@+id/editText" />
<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Confirm Password"
android:inputType="textPassword|textWebPassword"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toTopOf="@+id/editText4"
app:layout_constraintRight_toRightOf="@+id/editText2"
tools:layout_constraintLeft_creator="1"
android:layout_marginBottom="33dp"
app:layout_constraintLeft_toLeftOf="@+id/editText2" />
<EditText
android:id="@+id/editText4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Contact Number"
android:inputType="phone"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toTopOf="@+id/editText5"
app:layout_constraintRight_toRightOf="@+id/editText3"
tools:layout_constraintLeft_creator="1"
android:layout_marginBottom="26dp"
app:layout_constraintLeft_toLeftOf="@+id/editText3" />
<EditText
android:id="@+id/editText5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="email id"
android:inputType="textEmailAddress|textWebEmailAddress"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintRight_toRightOf="@+id/editText4"
tools:layout_constraintLeft_creator="1"
android:layout_marginBottom="23dp"
app:layout_constraintLeft_toLeftOf="@+id/editText4" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="48dp"
android:onClick="verify_reg"
android:text="SUBMIT"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="@+id/editText5"
android:layout_marginBottom="18dp" />
</android.support.constraint.ConstraintLayout>

MainActivity.java

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText uname, pwd, cpwd, phno, email;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public static boolean isValidEmailAddress(String mail) {
String emailPattern = "[a-zA-Z0-9._-]+@[a-z]+\\.[a-z]+";
return mail.matches(emailPattern);
}
public void verify_reg(View view) {
uname = (EditText)findViewById(R.id.editText);
pwd = (EditText)findViewById(R.id.editText2);
cpwd = (EditText)findViewById(R.id.editText3);
phno = (EditText)findViewById(R.id.editText4);
email = (EditText)findViewById(R.id.editText5);
if(uname.getText().toString().isEmpty() || pwd.getText().toString().isEmpty() ||
cpwd.getText().toString().isEmpty() || phno.getText().toString().isEmpty() ||
email.getText().toString().isEmpty()) {
Toast.makeText(this,"Please Enter all the details...",Toast.LENGTH_LONG).show();
}
else if(pwd.getText().toString().length()<=8) {
Toast.makeText(this,"Password should be more than 8
characters...",Toast.LENGTH_LONG).show();
}
else if(!pwd.getText().toString().equals(cpwd.getText().toString())) {
Toast.makeText(this,"Passwords do not match..!!",Toast.LENGTH_LONG).show();
}
else if(phno.getText().toString().length() < 10) {
Toast.makeText(this,"Invalid Mobile Number..!!",Toast.LENGTH_LONG).show();
}
else if(!isValidEmailAddress(email.getText().toString())) {
Toast.makeText(this,"Invalid Email ID",Toast.LENGTH_LONG).show();
}
else {
Intent obj = new Intent(this,SecondActivity.class);
obj.putExtra("Uname",uname.getText().toString());
obj.putExtra("Pwd", pwd.getText().toString());
obj.putExtra("Mobile", phno.getText().toString());
obj.putExtra("Email", email.getText().toString());
startActivity(obj);
}
}
}
activity_second.xml

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


<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.rit.ibmsap.registrationform.SecondActivity"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="TextView"
android:layout_marginStart="13dp"
android:layout_marginEnd="13dp"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toTopOf="@+id/textView9"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="64dp"
tools:layout_constraintLeft_creator="1"
android:layout_marginBottom="65dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView3"
android:layout_width="0dp"
android:layout_height="28dp"
android:text="TextView"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
app:layout_constraintRight_toRightOf="@+id/textView2"
android:layout_marginTop="21dp"
app:layout_constraintTop_toBottomOf="@+id/textView2"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="@+id/textView2" />
<TextView
android:id="@+id/textView9"
android:layout_width="0dp"
android:layout_height="31dp"
android:text="TextView"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="158dp"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView10"
android:layout_width="0dp"
android:layout_height="33dp"
android:text="TextView"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
app:layout_constraintRight_toRightOf="@+id/textView3"
android:layout_marginTop="63dp"
app:layout_constraintTop_toBottomOf="@+id/textView3"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="@+id/textView3" />
</android.support.constraint.ConstraintLayout>

SecondActivity.java

package com.rit.ibmsap.registrationform;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Bundle obj = getIntent().getExtras();
String str1 = obj.getString("Uname");
String str2 = obj.getString("Pwd");
String str3 = obj.getString("Mobile");
String str4 = obj.getString("Email");
String final_str = "";
for(int i=0;i<str2.length()-3;i++) {
final_str+='*';
}
final_str+=str2.substring(str2.length()-3);
TextView tv = (TextView)findViewById(R.id.textView2);
TextView tv1 = (TextView)findViewById(R.id.textView3);
TextView tv2 = (TextView)findViewById(R.id.textView9);
TextView tv3 = (TextView)findViewById(R.id.textView10);
tv.setText("UserName: "+str1);
tv1.setText("Password: "+final_str);
tv2.setText("Mobile Number: "+str3);
tv3.setText("Email ID: "+str4);
}
}

2 a) Design an Android Application to test the Login Page. If the user fails to enter correct
username or password three times, then the application should close automatically after
displaying a Toast message.

Solution:

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.win81.logincounter.MainActivity">

<EditText
android:id="@+id/editText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="17dp"
android:layout_marginLeft="17dp"
android:layout_marginRight="17dp"
android:layout_marginStart="17dp"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="Enter Username"
android:inputType="textPersonName"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1" />

<EditText
android:id="@+id/editText2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="Enter Password"
android:inputType="textPersonName"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
app:layout_constraintHorizontal_bias="0.0" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="7dp"
android:onClick="onSubmit"
android:text="Button"
app:layout_constraintLeft_toLeftOf="@+id/editText2"
app:layout_constraintRight_toRightOf="@+id/editText2"
app:layout_constraintTop_toBottomOf="@+id/editText2"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1" />
</android.support.constraint.ConstraintLayout>

MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


EditText txt1,txt2;
String name,password;
int counter=0;

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

public void onSubmit(View view){


txt1=(EditText)findViewById(R.id.editText);
name=txt1.getText().toString();
txt2=(EditText)findViewById(R.id.editText2);
password=txt2.getText().toString();
if (name.equals("Bharath") && password.equals("admin")) {
Toast.makeText(this, "Login Successfull", Toast.LENGTH_SHORT).show();
}
else if (name.equals("")||password.equals("")){
Toast.makeText(this, "Enter credentials", Toast.LENGTH_SHORT).show();
counter++;
}
else{
Toast.makeText(this, "Invalid credentials", Toast.LENGTH_SHORT).show();
counter++;
}
if(counter>=3){
Toast.makeText(this, "Helde tane Sayi Nan Magnen iga",
Toast.LENGTH_SHORT).show();
finish();
}
else{
Toast.makeText(this, "After "+(3-counter)+" more wrong attempts app will
close", Toast.LENGTH_SHORT).show();
}
}
}

2 b) Design an Android Application to test the life cycle of an Activity.

Solution:

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.win81.lifecycle.MainActivity">

<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter Name"
android:inputType="textPersonName"
android:layout_marginStart="16dp"
app:layout_constraintBaseline_toBaselineOf="@+id/button"
tools:layout_constraintBaseline_creator="1"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="16dp" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:onClick="onSubmit"
android:text="Submit"
app:layout_constraintLeft_toRightOf="@+id/editText"
tools:layout_constraintTop_creator="1"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
MainActivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
String tag = "Life Cycle";
EditText txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(tag, "In onCreate() function");
}
public void display(View view)
{
String name;
txt = (EditText)findViewById(R.id.editText);
name = txt.getText().toString();
String str="Good Afternoon : " + name;
Toast.makeText(getApplicationContext(), str, Toast.LENGTH_LONG).show();
}
public void onStart()
{
super.onStart();
Log.d(tag, "In onStart() function");
}
public void onResume()
{
super.onResume();
Log.d(tag, "In onResume() function");
}
public void onPause(){
super.onPause();
Log.d(tag, "In onPause() function");
}
public void onRestart()
{
super.onRestart();
Log.d(tag, "In onRestart() function");
}
public void onDestroy() {
super.onDestroy();
Log.d(tag, "In onDestroy() function");
}
}

3 a) Design an Android application to create a Vote Eligibility app. The MainActivity UI will
have Name, Gender (Male & Female) & a Button to next page. Collect the Birtdate using Date
Picker Widget in the Second Screen. Once user selects the Birthdate and clicks submit button.
Pass this value to main page and display whether the user is eligible to not.

3 b) Design an Android application to demonstrate the working of RadioGroup and Radio


Buttons.

Solution:

activity_main.xml

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


<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.krishna.voteeligibilityapp.MainActivity"
tools:layout_editor_absoluteY="81dp">

<EditText
android:id="@+id/editText"
android:layout_width="262dp"
android:layout_height="42dp"
android:layout_marginBottom="178dp"
android:layout_marginEnd="67dp"
android:layout_marginStart="67dp"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="Name"
android:inputType="textPersonName"
app:layout_constraintBottom_toTopOf="@+id/textView3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="128dp"
android:layout_height="94dp"
android:layout_marginEnd="64dp"
app:layout_constraintBottom_toTopOf="@+id/textView3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView3"
app:layout_constraintTop_toBottomOf="@+id/textView4">

<RadioButton
android:id="@+id/radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Male" />

<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Female" />
</RadioGroup>

<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_marginBottom="43dp"
android:layout_marginEnd="17dp"
android:layout_marginStart="3dp"
android:layout_marginTop="55dp"
android:text="Gender"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="@+id/radioGroup"
app:layout_constraintEnd_toStartOf="@+id/radioGroup"
app:layout_constraintStart_toStartOf="@+id/textView4"
app:layout_constraintTop_toTopOf="@+id/textView4" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="17dp"
android:onClick="goToDOBPick"
android:text="Date of Birth"
app:layout_constraintBottom_toTopOf="@+id/textView2"
app:layout_constraintEnd_toStartOf="@+id/radioGroup"
app:layout_constraintStart_toStartOf="@+id/radioGroup"
app:layout_constraintTop_toBottomOf="@+id/textView3" />

<TextView
android:id="@+id/textView2"
android:layout_width="161dp"
android:layout_height="0dp"
android:layout_marginBottom="152dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.372"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />

<TextView
android:id="@+id/textView3"
android:layout_width="101dp"
android:layout_height="31dp"
android:layout_marginBottom="244dp"
android:layout_marginStart="36dp"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText" />

<TextView
android:id="@+id/textView4"
android:layout_width="249dp"
android:layout_height="52dp"
android:layout_marginBottom="15dp"
app:layout_constraintBottom_toTopOf="@+id/radioGroup"
app:layout_constraintEnd_toStartOf="@+id/radioGroup"
app:layout_constraintStart_toStartOf="@+id/radioGroup"
app:layout_constraintTop_toBottomOf="@+id/editText" />

</android.support.constraint.ConstraintLayout>

MainActivity.java

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

public class MainActivity extends AppCompatActivity {


EditText name;
TextView tv, tv2, tv3;
RadioGroup rgp;
RadioButton btn;
String date = "", gender = "", msg = "",vname = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = (EditText)findViewById(R.id.editText);
tv = (TextView)findViewById(R.id.textView2);
tv2 = (TextView)findViewById(R.id.textView3);
tv3 = (TextView)findViewById(R.id.textView4);
if(getIntent().getStringExtra("msg") !=null){
date = getIntent().getStringExtra("date");
vname = getIntent().getStringExtra("name");
gender = getIntent().getStringExtra("gender");
tv3.setText(vname);
tv2.setText(gender);
tv.setText("DOB: "+date);
msg = getIntent().getStringExtra("msg");
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
}

public void goToDOBPick(View view) {


if(name.getText().toString().isEmpty()) {
Toast.makeText(this,"Name should not be Empty",Toast.LENGTH_LONG).show();
}
else {
rgp = (RadioGroup)findViewById(R.id.radioGroup);
btn = (RadioButton)findViewById(rgp.getCheckedRadioButtonId());
gender = btn.getText().toString();

Intent birthDate = new Intent(this,BirthDateActivity.class);


birthDate.putExtra("name", name.getText().toString());
birthDate.putExtra("gender", gender);
startActivity(birthDate);
}
}
}

activity_birth_date.xml

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


<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.krishna.voteeligibilityapp.BirthDateActivity">

<DatePicker
android:id="@+id/datePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="17dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:onClick="onClicking"
android:text="Date of Birth"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>

BirthDateActivity.java

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

public class BirthDateActivity extends AppCompatActivity {

DatePicker datePicker;
String vname = "", gender = "", date, msg = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_birth_date);
datePicker = (DatePicker) findViewById(R.id.datePicker);
Bundle fromMain = getIntent().getExtras();
vname = fromMain.getString("name");
gender = fromMain.getString("gender");
}

public void onClicking(View view) {


Intent backMain = new Intent(this, MainActivity.class);
date =
datePicker.getDayOfMonth()+"/"+(datePicker.getMonth()+1)+"/"+datePicker.getYear();
if(datePicker.getYear()<2000) {
msg = "Eligible to Vote..";
}
else {
msg = "Not Eligible to Vote..!!";
}
backMain.putExtra("msg",msg);
backMain.putExtra("date",date);
backMain.putExtra("name",vname);
backMain.putExtra("gender",gender);
startActivity(backMain);
}
}

4. Design an Android application to calculate the number of hours, minutes and seconds
between 2 times. The main UI will have 3 TextViews and 2 buttons. Using a Second screen
collect times and display the difference in the third text view. Difference should be displayed
as
 Total Number of Hours between 2 times
 Total Number of Minutes between 2 times

Solution:
activity_main. xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.krishna.timediffreqd.MainActivity">

<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="29dp"
android:layout_marginStart="27dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintEnd_toStartOf="@+id/button"
app:layout_constraintHorizontal_bias="0.113"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.086" />

<TextView
android:id="@+id/textView2"
android:layout_width="203dp"
android:layout_height="30dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="28dp"
app:layout_constraintEnd_toEndOf="@+id/textView"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/textView"
app:layout_constraintTop_toBottomOf="@+id/textView" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="220dp"
android:layout_marginEnd="16dp"
android:layout_marginTop="67dp"
android:onClick="timeSelect"
android:text="Select Time"
app:layout_constraintBottom_toTopOf="@+id/textView3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="calcDiff"
android:text="Calculate Difference"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/textView3"
android:layout_width="226dp"
android:layout_height="38dp"
android:layout_marginBottom="8dp"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />
</android.support.constraint.ConstraintLayout>

MainActivity. Java
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


TextView tv1, tv2, tv3;
//int c = 0;
Button st, cd;
String t1,t2, diff = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (TextView)findViewById(R.id.textView);
tv2 = (TextView)findViewById(R.id.textView2);
tv3 = (TextView)findViewById(R.id.textView3);
st = (Button)findViewById(R.id.button);
cd = (Button)findViewById(R.id.button2);

if(getIntent().getStringExtra("time1")!=null &&
getIntent().getStringExtra("time2")!=null) {
t1 = getIntent().getStringExtra("time1");
t2 = getIntent().getStringExtra("time2");
diff = getIntent().getStringExtra("diff");
tv1.setText(t1);
tv2.setText(t2);
}
}

public void timeSelect(View view) {


Intent gts = new Intent(this, TimePickActivity.class);
startActivity(gts);
}

public void calcDiff(View view) {


tv3.setText(diff);
}
}

activity_time_pick.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.krishna.timediffreqd.TimePickActivity"
tools:layout_editor_absoluteY="81dp">

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onSubmit"
android:text="Submit"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginTop="49dp"
android:onClick="setTime1"
android:text="Time1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginTop="21dp"
android:onClick="setTime2"
android:text="Time2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button4" />

<TextView
android:id="@+id/textView4"
android:layout_width="228dp"
android:layout_height="45dp"
android:layout_marginEnd="36dp"
android:layout_marginTop="48dp"
android:textSize="24sp"
app:layout_constraintEnd_toStartOf="@+id/button4"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/textView5"
android:layout_width="230dp"
android:layout_height="47dp"
android:layout_marginEnd="32dp"
android:layout_marginTop="24dp"
android:textSize="24sp"
app:layout_constraintEnd_toStartOf="@+id/button5"
app:layout_constraintTop_toBottomOf="@+id/textView4" />
</android.support.constraint.ConstraintLayout>

TimePickActivity.java
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;

public class TimePickActivity extends AppCompatActivity {

Boolean isTime1;
TextView tv4, tv5;
Button s, b4, b5;
//int counter;
String t1="", t2="", diff="";
long time1, time2, differ, dh, dm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_time_pick);
//tp = (TimePicker)findViewById(R.id.timePicker);
//tp.setIs24HourView(true);
s = (Button)findViewById(R.id.button3);
b4 = (Button)findViewById(R.id.button4);
b5 = (Button)findViewById(R.id.button5);
tv4 = (TextView)findViewById(R.id.textView4);
tv5 = (TextView)findViewById(R.id.textView5);
}
public void setTime1(View view) {
isTime1 = true;
dispTime();
}

private void dispTime() {


TimePickerDialog tdp;
tdp = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int h, int m) {
if(isTime1) {
t1 = h+":"+m;
time1 = h*60+m;
tv4.setText(t1);
}
else {
t2 = h+":"+m;
time2 = h*60+m;
tv5.setText(t2);
}
}
},0, 0, true);
tdp.show();
}

public void setTime2(View view) {


isTime1 = false;
dispTime();
}

public void onSubmit(View view) {


if(time2 > time1) differ = time2 - time1;
else differ = time1 - time2;
dh = differ/60;
dm = differ%60;
diff = "Difference is: "+dh+" hours "+dm+" minutes";
Intent backMain = new Intent(this, MainActivity.class);
backMain.putExtra("time1", t1);
backMain.putExtra("time2", t2);
backMain.putExtra("diff", diff);
startActivity(backMain);
}
}

5 a) Design an Android application to demonstrate the working of OptionMenu. Provide the


menu item for Display and Edit a Contact.

Solution:

MainActivity.java
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button)findViewById(R.id.button);
btn.setOnCreateContextMenuListener(this);
}

private void CreateMenu1(Menu menu) {


MenuItem item1 = menu.add(0,0,0,"Display All Contacts");
MenuItem item2 = menu.add(0,1,1,"Fast Dial");
}

public boolean onCreateOptionsMenu(Menu menu) {


super.onCreateOptionsMenu(menu);
CreateMenu(menu);
return super.onCreateOptionsMenu(menu);
}

private void CreateMenu(Menu menu) {


MenuItem m1 = menu.add(0,0,0,"Display Contact");
MenuItem m2 = menu.add(0,1,1,"Edit Contact");
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
return MenuChoice(item);
}

private boolean MenuChoice(MenuItem item) {


switch (item.getItemId()) {
case 0:
Intent i1 = new Intent(Intent.ACTION_VIEW);
i1.setData(Uri.parse("content://contacts/people/1"));
startActivity(i1);
return true;
case 1:
Intent i2 = new Intent(Intent.ACTION_EDIT);
i2.setData(Uri.parse("content://contacts/people/1"));
startActivity(i2);
return true;
}
return false;
}
}

6 a) Design an Android application to demonstrate the working of OptionMenu and


ContextMenu. Provide the menu item for FastDial and DisplayAll Contacts button.

Solution:

MainActivity.java
package com.example.krishna.optionmenuexample;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button)findViewById(R.id.button);
btn.setOnCreateContextMenuListener(this);
}

@Override
public void onCreateContextMenu(ContextMenu menu, View view,
ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu,view,menuInfo);
CreateMenu1(menu);
}

private void CreateMenu1(Menu menu) {


MenuItem item1 = menu.add(0,0,0,"Display All Contacts");
MenuItem item2 = menu.add(0,1,1,"Fast Dial");
}

@Override
public boolean onContextItemSelected(MenuItem item) {
return MenuChoice1(item);
}

private boolean MenuChoice1(MenuItem item) {


switch (item.getItemId()) {
case 0:
Intent i1 = new Intent(Intent.ACTION_VIEW);
i1.setData(Uri.parse("content://contacts/people/"));
startActivity(i1);
return true;
case 1:
Intent i2 = new Intent(Intent.ACTION_DIAL);
i2.setData(Uri.parse("tel: +91-9663016536"));
startActivity(i2);
return true;
}
return false;
}

public boolean onCreateOptionsMenu(Menu menu) {


super.onCreateOptionsMenu(menu);
CreateMenu(menu);
return super.onCreateOptionsMenu(menu);
}

private void CreateMenu(Menu menu) {


MenuItem m1 = menu.add(0,0,0,"Display Contact");
MenuItem m2 = menu.add(0,1,1,"Edit Contact");
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
return MenuChoice(item);
}

private boolean MenuChoice(MenuItem item) {


switch (item.getItemId()) {
case 0:
Intent i1 = new Intent(Intent.ACTION_VIEW);
i1.setData(Uri.parse("content://contacts/people/1"));
startActivity(i1);
return true;
case 1:
Intent i2 = new Intent(Intent.ACTION_EDIT);
i2.setData(Uri.parse("content://contacts/people/1"));
startActivity(i2);
return true;
}
return false;
}
}
6 b) Design an Android application to demonstrate the working of RatingBar widget

Solution:

activity_main.xml

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


<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.win81.ratingbar.MainActivity">

<Spinner
android:id="@+id/spinner"
android:layout_width="0dp"
android:layout_height="50dp"
android:entries="@array/Actors"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
android:layout_marginStart="17dp"
android:layout_marginEnd="17dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="64dp"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginLeft="17dp"
android:layout_marginRight="17dp" />

<RatingBar
android:id="@+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/spinner"
tools:layout_constraintRight_creator="1"
app:layout_constraintRight_toRightOf="parent"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:onClick="onSubmit"
android:text="Submit"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/ratingBar"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1" />
</android.support.constraint.ConstraintLayout>

Add the below entries in strings.xml


<resources>
<string name="app_name">RatingBar</string>
<string-array name="Actors">
<item>Puneeth Rajkumar</item>
<item>Hucha Venkat</item>
<item>Upendra</item>
<item>Kashinath</item>
</string-array>
</resources>
MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RatingBar;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


Spinner spinner;
RatingBar rating;

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

public void onSubmit(View v){


spinner=(Spinner)findViewById(R.id.spinner);
rating=(RatingBar)findViewById(R.id.ratingBar);
String rate= String.valueOf(rating.getRating());
Toast.makeText(this, "Selected Actor is :
"+spinner.getSelectedItem().toString()+"\n"+"Rating is: "+rate,
Toast.LENGTH_SHORT).show();
}
}

7 a) Design an Android application to demonstrate the working of a Calculator. Create Add,


Subtract, Multiply and Divide buttons. Collect Numbers from 2 EditText and display Result
in a TextView

Solution:

MainActivity.java

package com.example.ibmsap.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast
;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void calculate(View view) {
EditText num1, num2;
float a, b;
num1 = (EditText) findViewById(R.id.editText);
num2 = (EditText) findViewById(R.id.editText3);
String str1, str2;
str1 = num1.getText()
.toString();
str2 = num2.getText().toString();
if (str1.isEmpty()|| str2.isEmpty()) {
Toast.makeText(getApplicationContext(), "Please enter the numbers",
Toast.LENGTH_LONG).show();
}
else {
a = Float.parseFloat(num1.getText().toString());
b = Float.parseFloat(num2.getText().toString());
TextView txt;
txt = (TextView) findViewById(R.id.textView2);
switch (view.getId()) {
case R.id.button:
txt.setText("Result : " + (a + b));
break;
case R.id.button2:
txt.setText("Result : " + (a
-
b));
break;
case R.id.button3:
txt.setText("Result : " + (a * b));
break;
case R.id.button4:
if (b != 0)
txt.setText("Result : " + (a / b));
else
txt.setText("Divide by Zero Error");
break;
}
}
}
}

activity.xml

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


<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ibmsap.myapplication.MainActivity">
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number"
android:hint="Enter the first Number"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintLeft_creator="1"
app:layout_constraintHorizontal_bias="0.502"
android:layout_marginTop="32dp"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="Simple Calculator"
app:layout_constraintTop_toTopOf="parent"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintTop_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
tools:layout_constraintRight_creator="1" />
<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number"
android:hint="Enter the second Number"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
android:layout_marginTop="49dp"
app:layout_constraintTop_toBottomOf="@+id/editText"
tools:layout_constraintLeft_creator="1" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:onClick="calculate"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toBottomOf="@+id/button2"
app:layout_constraintRight_toRightOf="@+id/button3"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="@+id/button3"
app:layout_constraintTop_toTopOf="@+id/button2" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:onClick="calculate"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
android:layout_marginEnd="58dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="42dp"
app:layout_constraintTop_toBottomOf="@+id/editText3" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="*"
android:onClick="calculate"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintTop_creator="1"
android:layout_marginStart="46dp"
android:layout_marginTop="32dp"
app:layout_constraintTop_toBottomOf="@+id/button"
app:layout_constraintLeft_toLeftOf="parent" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="/"
android:onClick="calculate"
app:layout_constraintBottom_toBottomOf="@+id/button3"
app:layout_constraintLeft_toLeftOf="@+id/button2"
app:layout_constraintRight_toRightOf="@+id/button2"
app:layout_constraintTop_toTopOf
="@+id/button3"
tools:layout_constraintBottom_creator="1"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Result"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toBottomOf="parent"
tools:layout_constraintLeft_creator="1"
android:layout_marginBottom="52dp" />
</android.support.constraint.ConstraintLayout>

8 a) Design an Android application to create a Student Table with USN (Primary Key),
NAME, SEM & CGPA. User will have facility to insert the rows in the table. Display student
details when searched by USN.

Solution:

activity_main.xml

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

<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.rit.ibmsap.vehicletable.MainActivity"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="29dp"
android:ems="10"
android:hint="USN"
android:inputType="text"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="22dp"
android:ems="10"
android:hint="Name"
android:inputType="textPersonName"
app:layout_constraintLeft_toLeftOf="@+id/editText"
app:layout_constraintRight_toRightOf="@+id/editText"
app:layout_constraintTop_toBottomOf="@+id/editText"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1" />
<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="21dp"
android:ems="10"
android:hint="SEm"
android:inputType="number"
app:layout_constraintLeft_toLeftOf="@+id/editText2"
app:layout_constraintRight_toRightOf="@+id/editText2"
app:layout_constraintTop_toBottomOf="@+id/editText2"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1" />
<EditText
android:id="@+id/editText4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="31dp"
android:ems="10"
android:hint="CGPA"
android:inputType="numberDecimal"
app:layout_constraintLeft_toLeftOf="@+id/editText3"
app:layout_constraintRight_toRightOf="@+id/editText3"
app:layout_constraintTop_toBottomOf="@+id/editText3"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:onClick="insertDetails"
android:text="Insert"
tools:layout_constraintTop_creator="1"
android:layout_marginTop="84dp"
app:layout_constraintTop_toBottomOf="@+id/editText4"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="16dp" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="search"
android:text="SEARCH"
app:layout_constraintBaseline_toBaselineOf="@+id/button"
app:layout_constraintHorizontal_bias="0.945"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
tools:layout_constraintBaseline_creator="1"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1" />
</android.support.constraint.ConstraintLayout>

MainActivity.java

import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText txt1, txt2, txt3, txt4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt1 = (EditText)findViewById(R.id.editText);
txt2 = (EditText)findViewById(R.id.editText2);
txt3 = (EditText)findViewById(R.id.editText3);
txt4 = (EditText)findViewById(R.id.editText4);
}
public void insertDetails(View view) {
DBAdapter db = new DBAdapter(this);
db.open();
String usn = txt1.getText().toString();
String name = txt2.getText().toString();
Integer sem = Integer.parseInt(txt3.getText().toString());
Double cgpa = Double.parseDouble(txt4.getText().toString());
long id = db.insertVehicleDetails(usn,name,sem,cgpa);
if(id!=0) {
Toast.makeText(this,"Data Inserted Successfully..",Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(this,"Data Insertion Failed..!!",Toast.LENGTH_LONG).show();
}
db.close();
txt1.setText("");
txt2.setText("");
txt3.setText("");
txt4.setText("");
txt4.clearFocus();
}
public void search(View view)
{
EditText txt =(EditText)findViewById(R.id.editText);
String usn = txt.getText().toString();;
DBAdapter db = new DBAdapter(this);
db.open();
Cursor c = db.getContact(usn);
if (c.moveToFirst())
display(c);
else
Toast.makeText(this, "No contact found", Toast.LENGTH_LONG).show();
db.close();
}
public void display(Cursor c) {
Toast.makeText(this,"USN: "+c.getString(0)+"\n"+
"Name: "+c.getString(1)+"\n"+
"Sem: "+c.getString(2)+"\n"+
"CGPA: "+c.getString(3),
Toast.LENGTH_LONG).show();
}
}

DBAdapter.java

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBAdapter {
static final String USN = "usn";
static final String NAME = "name";
static final String SEM = "sem";
static final String CGPA = "cgpa";
static final String TAG = "DBAdapter";
static final String DATABASE_NAME = "StudentDB";
static final String DATABASE_TABLE = "student_table";
static final int DATABASE_VERSION = 1;
static final String DATABASE_CREATE =
"create table student_table (usn text primary key, "
+ "name text not null, sem integer not null, cgpa double not null);";
final Context context;
DatabaseHelper DBHelper;
SQLiteDatabase db;
public DBAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
try {
db.execSQL(DATABASE_CREATE);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS contacts");
onCreate(db);
}
}
public DBAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}
public void close()
{
DBHelper.close();
}
public long insertVehicleDetails(String usn, String name, Integer sem, Double cgpa)
{
ContentValues initialValues = new ContentValues();
initialValues.put(USN,usn);
initialValues.put(NAME, name);
initialValues.put(SEM, sem);
initialValues.put(CGPA, cgpa);
return db.insert(DATABASE_TABLE, null, initialValues);
}
public Cursor getContact(String usn) throws SQLException
{
Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {USN,
NAME, SEM, CGPA}, USN + "= '"+ usn+"'", null,null, null, null, null);
if (mCursor != null)
{
mCursor.moveToFirst();
}
return mCursor;
}
}

8 b) Design an Android application to demonstrate the working of Implicit Intents by


displaying all the contact details

Solution:

activity_main.xml

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


<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.win81.implicitintent.MainActivity">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Display Contacts"
android:onClick="displayContacts"
tools:layout_constraintTop_creator="1"
android:layout_marginStart="27dp"
android:layout_marginTop="12dp"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginLeft="27dp" />

</android.support.constraint.ConstraintLayout>

MainActivity.java
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void displayContacts(View v){
Intent i= new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("content://contacts/people"));
startActivity(i);

}
}
9 Design an Android Application to play a mp3 file. Provide, Start, Pause and Stop buttons

Solution:

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.win81.audioplayer.MainActivity">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:onClick="onclickStart"
android:text="Start"
app:layout_constraintLeft_toLeftOf="parent"
tools:layout_constraintBaseline_creator="1"
tools:layout_constraintLeft_creator="1"
app:layout_constraintBaseline_toBaselineOf="@+id/button2" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="160dp"
android:onClick="onclickPause"
android:text="Pause"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
tools:layout_constraintBottom_creator="1"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1" />

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:onClick="onclickStop"
android:text="Stop"
app:layout_constraintBaseline_toBaselineOf="@+id/button2"
app:layout_constraintRight_toRightOf="parent"
tools:layout_constraintBaseline_creator="1"
tools:layout_constraintRight_creator="1" />

<SeekBar
android:id="@+id/seekBar2"
android:layout_width="0dp"
android:layout_height="wrap_content"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginStart="9dp"
android:layout_marginEnd="9dp"
app:layout_constraintRight_toRightOf="parent"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginLeft="9dp"
android:layout_marginRight="9dp" />
</android.support.constraint.ConstraintLayout>

MainActivity.java
package com.example.win81.audioplayer;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity{


MediaPlayer mediaplayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaplayer = MediaPlayer.create(getApplicationContext(),R.raw.test);
}

public void onclickStart(View v){


mediaplayer.start();
}
public void onclickPause(View v){
mediaplayer.pause();
}
public void onclickStop(View v){
mediaplayer.stop();
}
}

10 a) Design an Android application to create a Employee table with EMP_ID (Primary Key),
EMP_NAME, PROJECT & SALARY. User will have facility to insert the rows in the table.
Display the Employee detail who has maximum salary.

Solution:

XML File:

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


<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.rit.ibmsap.maxsalaryprogram.MainActivity">
<EditText
android:id="@+id/id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Emp Id"
android:inputType="number"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
app:layout_constraintRight_toRightOf="@+id/name"
android:layout_marginTop="32dp"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="@+id/name"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Emp Name"
android:inputType="textPersonName"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
app:layout_constraintRight_toRightOf="@+id/editText3"
android:layout_marginTop="32dp"
app:layout_constraintTop_toBottomOf="@+id/id"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="@+id/editText3" />
<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Project"
android:inputType="textPersonName"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
app:layout_constraintRight_toRightOf="@+id/salary"
android:layout_marginTop="49dp"
app:layout_constraintTop_toBottomOf="@+id/name"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="@+id/salary" />
<EditText
android:id="@+id/salary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Salary"
android:inputType="textPersonName|number"
tools:layout_constraintTop_creator="1"
android:layout_marginStart="74dp"
android:layout_marginTop="35dp"
app:layout_constraintTop_toBottomOf="@+id/editText3"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="166dp" />
<Button
android:id="@+id/insert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="35dp"
android:onClick="insertEmp"
android:text="insert"
app:layout_constraintBottom_toBottomOf="@+id/maxsal"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="@+id/maxsal"
tools:layout_constraintBottom_creator="1"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintTop_creator="1" />
<Button
android:id="@+id/maxsal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="64dp"
android:layout_marginEnd="53dp"
android:onClick="displayMaxSal"
android:text="Max Salary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
tools:layout_constraintBottom_creator="1"
tools:layout_constraintRight_creator="1" />
</android.support.constraint.ConstraintLayout>
MainActivity.java:
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
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);
}
public void insertEmp(View view){
DBAdapter db=new DBAdapter(this);
db.open();
EditText id,name,proj,sal;
String strname,strproj;
Integer intId,intSal;
id =(EditText)findViewById(R.id.id);
name =(EditText)findViewById(R.id.name);
proj =(EditText)findViewById(R.id.editText3);
sal =(EditText)findViewById(R.id.salary);
intId =Integer.parseInt(id.getText().toString());
strname=name.getText().toString();
strproj=name.getText().toString();
intSal =Integer.parseInt(sal.getText().toString());
if(strname.equals("")|| strproj.equals("")){
Toast.makeText(this,"Please enter details",Toast.LENGTH_LONG).show();
}
else {
long retid = db.insertEmp(intId, strname,strproj,intSal);
if(retid!=0){
Toast.makeText(this,"Emp inserted
successfully",Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(this,"Error occured.Emp insertion
unsuccessful",Toast.LENGTH_LONG).show();
}
}
db.close();
id.setText("");
name.setText("");
proj.setText("");
sal.setText("");
id.clearFocus();
}
public void displayMaxSal(View view)
{
DBAdapter db=new DBAdapter(this);
db.open();
Cursor obj=db.getMaxSal();
if(obj.moveToFirst())
{
do{
display(obj);
}while (obj.moveToNext());
}
db.close();
}
public void display(Cursor obj)
{
Toast.makeText(this,
"sal: " + obj.getString(0) + "\n" +
"Name: " + obj.getString(1) + "\n" +
"ID: " + obj.getString(2),
Toast.LENGTH_LONG).show();
}
}

DBAdapter:
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.util.Log;
public class DBAdapter {
static final String KEY_ROWID = "_id";
static final String KEY_ID = "id";
static final String KEY_NAME = "name";
static final String KEY_PROJ = "project";
static final String KEY_SAL = "sal";
static final String TAG = "DBAdapter";
static final String DATABASE_NAME = "MyDB2";
static final String DATABASE_TABLE = "empdb";
static final int DATABASE_VERSION = 1;
static final String DATABASE_CREATE =
"create table empdb (_id integer primary key autoincrement, "
+ "id integer not null,name text not null, project text not null,sal
integer not null);";
final Context context;
DatabaseHelper DBHelper;
SQLiteDatabase db;
public DBAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
try {
db.execSQL(DATABASE_CREATE);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS contacts");
onCreate(db);
}
}
//---opens the database---
public DBAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}
//---closes the database---
public void close()
{
DBHelper.close();
}
//---insert a contact into the database---
public long insertEmp(Integer id,String name, String project,Integer sal)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_ID, id);
initialValues.put(KEY_NAME, name);
initialValues.put(KEY_PROJ, project);
initialValues.put(KEY_SAL, sal);
return db.insert(DATABASE_TABLE, null, initialValues);
}
//---retrieves all the contacts---
public Cursor getMaxSal()
{
return db.query(DATABASE_TABLE, new String[] {"MAX("+KEY_SAL+")", KEY_NAME,
KEY_ID}, null, null, null, null, null);
}
}

10 b) Design an Android application to demonstrate the working of Implicit Intents by editing


a contact detail.
Solution:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.win81.implicitintent.MainActivity">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Display Contacts"
android:onClick="displayContacts"
tools:layout_constraintTop_creator="1"
android:layout_marginStart="27dp"
android:layout_marginTop="12dp"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginLeft="27dp" />

</android.support.constraint.ConstraintLayout>

MainActivity.java
package com.example.win81.implicitintent;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void displayContacts(View v){
Intent i= new Intent(Intent.ACTION_EDIT);
i.setData(Uri.parse("content://contacts/people/1"));
startActivity(i);

}
}

11 a) Design an Android application to create a Vehicle Table with Number (Primary Key),
VEHICLE_NAME, OWNER and MAKE_YEAR. User will have facility to insert the rows in
the table. Provide the facility for updating the values. Display all the data.

activity_main.xml

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

<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.rit.ibmsap.vehicletable.MainActivity"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter Vehicle Number"
android:inputType="number"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="29dp"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter Vehicle Name"
android:inputType="textPersonName"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
app:layout_constraintRight_toRightOf="@+id/editText"
android:layout_marginTop="22dp"
app:layout_constraintTop_toBottomOf="@+id/editText"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="@+id/editText" />
<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter Owner Name"
android:inputType="textPersonName"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
app:layout_constraintRight_toRightOf="@+id/editText2"
android:layout_marginTop="21dp"
app:layout_constraintTop_toBottomOf="@+id/editText2"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="@+id/editText2" />
<EditText
android:id="@+id/editText4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter Make Year"
android:inputType="number"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
app:layout_constraintRight_toRightOf="@+id/editText3"
android:layout_marginTop="31dp"
app:layout_constraintTop_toBottomOf="@+id/editText3"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="@+id/editText3" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:onClick="insertDetails"
android:text="Insert"
tools:layout_constraintTop_creator="1"
android:layout_marginTop="84dp"
app:layout_constraintTop_toBottomOf="@+id/editText4"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="16dp" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="displayDetails"
android:text="Display"
tools:layout_constraintRight_creator="1"
tools:layout_constraintLeft_creator="1"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBaseline_toBaselineOf="@+id/button"
tools:layout_constraintBaseline_creator="1"
app:layout_constraintLeft_toLeftOf="parent" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="updateEntry"
android:text="Update"
tools:layout_constraintRight_creator="1"
android:layout_marginEnd="16dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginRight="16dp"
app:layout_constraintBaseline_toBaselineOf="@+id/button2"
tools:layout_constraintBaseline_creator="1" />
</android.support.constraint.ConstraintLayout>
MainActivity.java

import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText txt1, txt2, txt3, txt4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt1 = (EditText)findViewById(R.id.editText);
txt2 = (EditText)findViewById(R.id.editText2);
txt3 = (EditText)findViewById(R.id.editText3);
txt4 = (EditText)findViewById(R.id.editText4);
}
public void insertDetails(View view) {
DBAdapter db = new DBAdapter(this);
db.open();
Integer num = Integer.parseInt(txt1.getText().toString());
String name = txt2.getText().toString();
String owner = txt3.getText().toString();
Integer year = Integer.parseInt(txt4.getText().toString());
long id = db.insertVehicleDetails(num,name,owner,year);
if(id!=0) {
Toast.makeText(this,"Data Inserted Successfully..",Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(this,"Data Insertion Failed..!!",Toast.LENGTH_LONG).show();
}
db.close();
txt1.setText("");
txt2.setText("");
txt3.setText("");
txt4.setText("");
txt4.clearFocus();
}
public void displayDetails(View view) {
DBAdapter db = new DBAdapter(this);
db.open();
Cursor c = db.getAllVehicleDetails();
if(c.moveToFirst()) {
do {
display(c);
}while (c.moveToNext());
}
db.close();
}
public void display(Cursor c) {
Toast.makeText(this,"Vehicle Number: "+c.getString(0)+"\n"+
"Vehicle Name: "+c.getString(1)+"\n"+
"Vehicle Owner: "+c.getString(2)+"\n"+
"Make Year: "+c.getString(3),
Toast.LENGTH_LONG).show();
}
public void updateEntry(View view) {
Integer num = Integer.parseInt(txt1.getText().toString());
String name = txt2.getText().toString();
String owner = txt3.getText().toString();
Integer year = Integer.parseInt(txt4.getText().toString());
DBAdapter db = new DBAdapter(this);
db.open();
if (db.updateVehicleDetails(num,name,owner,year))
Toast.makeText(this,"Update Successful..",Toast.LENGTH_LONG).show();
else
Toast.makeText(this,"Update Unsuccessful",Toast.LENGTH_LONG).show();
db.close();
txt1.setText("");
txt2.setText("");
txt3.setText("");
txt4.setText("");
txt4.clearFocus();
}
}

DBAdapter.java

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBAdapter {
static final String NUMBER = "vehicle_id";
static final String VEHICLE_NAME = "vehicle_name";
static final String OWNER = "vehicle_owner";
static final String MAKE_YEAR = "make_year";
static final String TAG = "DBAdapter";
static final String DATABASE_NAME = "VehicleDB";
static final String DATABASE_TABLE = "vehicle_table";
static final int DATABASE_VERSION = 1;
static final String DATABASE_CREATE =
"create table vehicle_table (vehicle_id integer primary key, "
+ "vehicle_name text not null, vehicle_owner text not null, make_year
integer not null);";
final Context context;
DatabaseHelper DBHelper;
SQLiteDatabase db;
public DBAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
try {
db.execSQL(DATABASE_CREATE);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS contacts");
onCreate(db);
}
}
public DBAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}
public void close()
{
DBHelper.close();
}
public long insertVehicleDetails(Integer num, String name, String owner, Integer year)
{
ContentValues initialValues = new ContentValues();
initialValues.put(NUMBER,num);
initialValues.put(VEHICLE_NAME, name);
initialValues.put(OWNER, owner);
initialValues.put(MAKE_YEAR, year);
return db.insert(DATABASE_TABLE, null, initialValues);
}
public Cursor getAllVehicleDetails()
{
return db.query(DATABASE_TABLE, new String[] {NUMBER, VEHICLE_NAME,
OWNER, MAKE_YEAR}, null, null, null, null, null);
}
public boolean updateVehicleDetails(Integer num, String name, String owner, Integer year)
{
ContentValues args = new ContentValues();
args.put(VEHICLE_NAME, name);
args.put(OWNER, owner);
args.put(MAKE_YEAR, year);
return db.update(DATABASE_TABLE, args, NUMBER + "=" + num, null) > 0;
}
}

11 b) Design an Android application to demonstrate the working of Implicit Intents by


displaying your friend’s contact detail.

Solution:

activity_main.xml

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


<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.win81.implicitintent.MainActivity">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Display Contacts"
android:onClick="displayContacts"
tools:layout_constraintTop_creator="1"
android:layout_marginStart="27dp"
android:layout_marginTop="12dp"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginLeft="27dp" />

</android.support.constraint.ConstraintLayout>
MainActivity.java
package com.example.win81.implicitintent;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void displayContacts(View v){
Intent i= new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("content://contacts/people/1"));
startActivity(i);

}
}

12. Design an Android Application to send a text message from one AVD to another. Collect
Phone Number and Message from the User.

Solution:

Step1: create the below XML


<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.win81.sms.MainActivity">

<EditText
android:id="@+id/editText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter Message"
android:inputType="textPersonName"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/editText2"
tools:layout_constraintRight_creator="1"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintRight_toRightOf="parent"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp" />

<EditText
android:id="@+id/editText2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter Number"
android:inputType="textPersonName"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="16dp"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:onClick="sendSMS"
android:text="Send SMS"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1" />
</android.support.constraint.ConstraintLayout>

Step2: Write the below Java Code in MainActivity


package com.example.win81.sms;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


EditText txt1,txt2;
String msg,phoneno;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendSMS(View v){
txt1=(EditText)findViewById(R.id.editText2);
phoneno=txt1.getText().toString();
txt2=(EditText)findViewById(R.id.editText);
msg=txt2.getText().toString();
Toast.makeText(this, phoneno, Toast.LENGTH_SHORT).show();
SmsManager smsManager= SmsManager.getDefault();
smsManager.sendTextMessage(phoneno,null,msg,null,null);
}
}

Step3: Add <uses-permission android:name="android.permission.SEND_SMS"/>


In AndroidManifest.xml
Step4: Go to Gradle Scripts->build.gradle(Module:app) change the targetsdkversion to 21 or
22

Step 5: Go to Tools -> Android->AVD Manger


and run 2 AVDs simultaneously

Step 6: Run the code. Enter the phone number (highlighted 4 digit number in AVD screenshot
as) and a msg. and press send.

You might also like