SDPD Solutions
SDPD Solutions
SDPD Solutions
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
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
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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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.
Solution:
activity_main.xml
<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;
activity_birth_date.xml
<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;
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");
}
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;
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);
}
}
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;
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();
}
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;
@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 boolean onOptionsItemSelected(MenuItem item) {
return MenuChoice(item);
}
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;
@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);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
return MenuChoice1(item);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return MenuChoice(item);
}
Solution:
activity_main.xml
<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>
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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
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
<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;
}
}
Solution:
activity_main.xml
<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;
@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;
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:
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);
}
}
<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;
@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
<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;
}
}
Solution:
activity_main.xml
<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;
@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:
<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>
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;
@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);
}
}
Step 6: Run the code. Enter the phone number (highlighted 4 digit number in AVD screenshot
as) and a msg. and press send.