0% found this document useful (0 votes)
3 views42 pages

MAD Practical File

Uploaded by

keerasinghania7
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views42 pages

MAD Practical File

Uploaded by

keerasinghania7
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

INDEX

Sno List of Practical Signature

1 Create "hello world" application to display "hello world" in the middle


of the screen in the emulator as well as android phone

2
Create an android app to display various android lifecycle phases.

3 Create a calculator app that performs addition, subtraction, division


and multiplication operation on numbers.

4 Write an Android application to convert into different currencies, for


example, Rupees to dollar

5 Write an application to mark the daily route of travel in map.

6 Create a spinner application with strings taken from resource directory


res/values/[Link]. On changing the spinner value, the image will
change. Image is saved in the drawable directory.
7
Create an app that uses a radio button group which calculates discount
on a shopping bill amount. Use EditText to enter bill amount and
select one of three radio buttons to determine a discount for 10%,
15%, or 20%. The discount is calculated upon selection and displayed
in a TextView.
8 Create a login application to verify username and password. On
successful login, redirect to another activity that displays "Welcome
User" with a logout button. On logout click, a dialog should appear
with OK and Cancel. OK goes back to login, Cancel stays on same
activity.
9
Create an application to perform the operations of create, insert,
delete, view, and update using SQLite database.

10
Create an application to pick any image from the native application
gallery and display it on the screen.

11 Create an application to take picture using native application.


Question 1:
Create "hello world" application to display "hello world" in the middle of the screen in the emulator
as well as android phone

activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

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

</[Link]>

[Link]:
package [Link];

import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);

setContentView([Link].activity_main);

}
}
OUTPUT
Emulator Andriod phone
Question 2:

Create an android app to display various android lifecycle phases.

[Link]:
package [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {


private static final String TAG = "LifecyclePhase";
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
Log.d(TAG, "onCreate called");
}
@Override
protected void onStart() {
[Link]();
Log.d(TAG, "onStart called");
}
@Override
protected void onResume() {
[Link]();
Log.d(TAG, "onResume called");
}
@Override
protected void onPause() {
[Link]();
Log.d(TAG, "onPause called");
}
@Override
protected void onStop() {
[Link]();
Log.d(TAG, "onStop called");
}
@Override
protected void onRestart() {
[Link]();
Log.d(TAG, "onRestart called");
}
@Override
protected void onDestroy() {
[Link]();
Log.d(TAG, "onDestroy called");
}
}
OUTPUT
Question 3:
Create a calculator app that performs addition, subtraction, division and multiplication operation on
numbers.
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">

<!-- EditTexts for entering the two numbers -->


<EditText
android:id="@+id/etFirstNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter First Number"
android:inputType="numberDecimal"
android:padding="10dp"
android:textSize="16sp"/>

<EditText
android:id="@+id/etSecondNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Second Number"
android:inputType="numberDecimal"
android:padding="10dp"
android:textSize="16sp"
android:layout_below="@id/etFirstNumber"
android:layout_marginTop="20dp"/>

<!-- Buttons for the operations -->


<Button
android:id="@+id/btnAdd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add"
android:layout_below="@id/etSecondNumber"
android:layout_marginTop="20dp"/>

<Button
android:id="@+id/btnSubtract"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Subtract"
android:layout_below="@id/btnAdd"
android:layout_marginTop="10dp"/>

<Button
android:id="@+id/btnMultiply"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Multiply"
android:layout_below="@id/btnSubtract"
android:layout_marginTop="10dp"/>

<Button
android:id="@+id/btnDivide"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Divide"
android:layout_below="@id/btnMultiply"
android:layout_marginTop="10dp"/>

<!-- TextView to display the result -->


<TextView
android:id="@+id/tvResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/btnDivide"
android:layout_marginTop="20dp"
android:text="Result"
android:textSize="18sp"
android:textColor="#000000" />

</RelativeLayout>

[Link]:
package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

import [Link];

public class MainActivity extends AppCompatActivity {

EditText etFirstNumber, etSecondNumber;


Button btnAdd, btnSubtract, btnMultiply, btnDivide;
TextView tvResult;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
// Initializing the views
etFirstNumber = findViewById([Link]);
etSecondNumber = findViewById([Link]);
btnAdd = findViewById([Link]);
btnSubtract = findViewById([Link]);
btnMultiply = findViewById([Link]);
btnDivide = findViewById([Link]);
tvResult = findViewById([Link]);

// Add operation
[Link](new [Link]() {
@Override
public void onClick(View v) {
performOperation("add");
}
});

// Subtract operation
[Link](new [Link]() {
@Override
public void onClick(View v) {
performOperation("subtract");
}
});

// Multiply operation
[Link](new [Link]() {
@Override
public void onClick(View v) {
performOperation("multiply");
}
});

// Divide operation
[Link](new [Link]() {
@Override
public void onClick(View v) {
performOperation("divide");
}
});
}

// Method to perform the operations


private void performOperation(String operation) {
String firstNumStr = [Link]().toString();
String secondNumStr = [Link]().toString();

// Checking if both fields are not empty


if ([Link]() || [Link]()) {
[Link]([Link], "Please enter both numbers", Toast.LENGTH_SHORT).show();
return;
}

// Converting the input values to double


double firstNum = [Link](firstNumStr);
double secondNum = [Link](secondNumStr);
double result = 0;

// Performing the calculation based on the operation


switch (operation) {
case "add":
result = firstNum + secondNum;
break;
case "subtract":
result = firstNum - secondNum;
break;
case "multiply":
result = firstNum * secondNum;
break;
case "divide":
if (secondNum == 0) {
// Checking if the second number is 0 for division
[Link]([Link], "Cannot divide by zero", Toast.LENGTH_SHORT).show();
return;
} else {
result = firstNum / secondNum;
}
break;
}

// Displaying the result in the TextView


[Link]("Result: " + result);
}
}
OUTPUT
Addition Division

Subtraction Multiplication
Question 4:
Write an Android application to convert into different currencies for example, Rupees to dollar
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"
android:orientation="vertical">

<EditText
android:id="@+id/etRupees"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter amount in Rupees"
android:inputType="numberDecimal" />

<Button
android:id="@+id/btnDollar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Convert to Dollar"
android:layout_marginTop="20dp"/>

<Button
android:id="@+id/btnYen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Convert to Chinese Yen"
android:layout_marginTop="10dp"/>

<Button
android:id="@+id/btnPound"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Convert to Pound"
android:layout_marginTop="10dp"/>

<TextView
android:id="@+id/tvResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Result"
android:textSize="18sp"
android:textColor="#000000"
android:layout_marginTop="20dp"/>

</LinearLayout>
[Link]:
package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

import [Link];

public class MainActivity extends AppCompatActivity {

EditText etRupees;
Button btnDollar, btnYen, btnPound;
TextView tvResult;

// Conversion Rates (can be hardcoded)


final double DOLLAR_RATE = 0.012; // 1 Rupee = 0.012 USD
final double YEN_RATE = 0.087; // 1 Rupee = 0.087 CNY
final double POUND_RATE = 0.0096; // 1 Rupee = 0.0096 GBP

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

etRupees = findViewById([Link]);
btnDollar = findViewById([Link]);
btnYen = findViewById([Link]);
btnPound = findViewById([Link]);
tvResult = findViewById([Link]);

[Link](new [Link]() {
@Override
public void onClick(View v) {
convertCurrency(DOLLAR_RATE, "Dollar");
}
});

[Link](new [Link]() {
@Override
public void onClick(View v) {
convertCurrency(YEN_RATE, "Chinese Yen");
}
});

[Link](new [Link]() {
@Override
public void onClick(View v) {
convertCurrency(POUND_RATE, "Pound");
}
});
}

private void convertCurrency(double rate, String currencyName) {


String input = [Link]().toString();

if ([Link]()) {
[Link](this, "Please enter an amount", Toast.LENGTH_SHORT).show();
return;
}

double rupees = [Link](input);


double converted = rupees * rate;

[Link]([Link]("%.2f Rupees = %.2f %s", rupees, converted, currencyName));


}
}
OUTPUT
Question 5:
Write an application to mark the daily route of travel in map.
activity_main.xml:

[Link]:
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
String loc="Delhi";
EditText edtloc;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
[Link](this);
setContentView([Link].activity_main);
[Link](findViewById([Link]), (v, insets) -> {
Insets systemBars = [Link]([Link]());
[Link]([Link], [Link], [Link], [Link]);
return insets;
});
edtloc=(EditText) findViewById([Link]);
[Link](peekAvailableContext().getApplicationContext(),"SelectLocation",Toast.LENGTH_SHOR
T).show();
}
public void openMap(View v) {
loc = [Link]().toString();
Uri u = [Link]("[Link] + [Link]());
Intent i = new Intent(Intent.ACTION_VIEW, u);
[Link]().startActivity(i);
}
public void TravelPath(View view){
loc=[Link]().toString();
Uri uri= [Link]("[Link]
Intent i= new Intent(Intent.ACTION_VIEW,uri);
[Link]().startActivity(i); }
}

activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="43dp"
android:layout_marginEnd="43dp"
android:layout_marginBottom="19dp"
android:onClick="openMap"
android:text="Search Destination Location"
app:layout_constraintBottom_toTopOf="@+id/button2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edtLocation" />
<EditText
android:id="@+id/edtLocation"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="39dp"
android:layout_marginTop="198dp"
android:layout_marginEnd="39dp"
android:layout_marginBottom="36dp"
android:ems="10"
android:inputType="textPersonName"
android:onClick="TravelPath"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintEnd_toEndOf="parent"
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_marginStart="3dp"
android:layout_marginBottom="245dp"
android:text="Travel Path Delhi to Destination"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="@+id/button"
app:layout_constraintTop_toBottomOf="@+id/button" />
</[Link]>
OUTPUT
Question 6:
Create a spinner application with strings taken from resource directory res/values/[Link] and on
changing the spinner value, image will change. Image is saved in the drawable directory.
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:orientation="vertical"
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Spinner
android:id="@+id/mySpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<ImageView
android:id="@+id/imgDisplay"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginTop="20dp"
android:scaleType="centerInside"
android:src="@drawable/apple" />
</LinearLayout>

[Link]:
package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

import [Link];

public class MainActivity extends AppCompatActivity {

Spinner spinner;
ImageView imageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

spinner = findViewById([Link]);
imageView = findViewById([Link]);

ArrayAdapter<CharSequence> adapter = [Link](


this, [Link].fruits_array, [Link].simple_spinner_item);
[Link]([Link].simple_spinner_dropdown_item);
[Link](adapter);

[Link](new [Link]() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
[Link]([Link]);
break;
case 1:
[Link]([Link]);
break;
case 2:
[Link]([Link]);
break;
}
}

@Override
public void onNothingSelected(AdapterView<?> parent) {}
});
}
}

[Link]:

<resources>
<string name="app_name">Practical six</string>
<string-array name="fruits_array">
<item>Apple</item>
<item>Banana</item>
<item>Cherry</item>
</string-array>
</resources>
OUTPUT
Question 7:
Create an app that uses radio button group which calculates discount on shopping bill amount. Use
edit text to enter bill amount and select one of three radio buttons to determine a discount for 10, 15,
or 20 [Link] discount is calculated upon selection of one of the buttons and displayed in a
textview control.

activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">

<!-- EditText for entering bill amount -->


<EditText
android:id="@+id/etBillAmount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Bill Amount"
android:inputType="numberDecimal"
android:padding="10dp"
android:textSize="16sp"/>

<!-- RadioGroup containing three radio buttons -->


<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/etBillAmount"
android:orientation="vertical"
android:layout_marginTop="20dp">

<RadioButton
android:id="@+id/radio10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10% Discount" />

<RadioButton
android:id="@+id/radio15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="15% Discount" />

<RadioButton
android:id="@+id/radio20"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="20% Discount" />
</RadioGroup>

<!-- Button to calculate the discount -->


<Button
android:id="@+id/btnCalculate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Calculate Discount"
android:layout_below="@id/radioGroup"
android:layout_marginTop="20dp" />

<!-- TextView to display the result -->


<TextView
android:id="@+id/tvResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/btnCalculate"
android:layout_marginTop="20dp"
android:text=""
android:textSize="18sp"
android:textColor="#000000" />
</RelativeLayout>

[Link]:
package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

import [Link];

public class MainActivity extends AppCompatActivity {

EditText etBillAmount;
RadioGroup radioGroup;
Button btnCalculate;
TextView tvResult;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
// Initializing views
etBillAmount = findViewById([Link]);
radioGroup = findViewById([Link]);
btnCalculate = findViewById([Link]);
tvResult = findViewById([Link]);

[Link](new [Link]() {
@Override
public void onClick(View v) {
// Get the bill amount entered by the user
String billAmountStr = [Link]().toString();

if ([Link]()) {
// Show a toast message if the bill amount is not entered
[Link]([Link], "Please enter a valid bill amount",
Toast.LENGTH_SHORT).show();
} else {
// Parse the bill amount to a double
double billAmount = [Link](billAmountStr);

// Check which radio button is selected


int selectedRadioButtonId = [Link]();
double discountPercentage = 0;

if (selectedRadioButtonId == [Link].radio10) {
discountPercentage = 10;
} else if (selectedRadioButtonId == [Link].radio15) {
discountPercentage = 15;
} else if (selectedRadioButtonId == [Link].radio20) {
discountPercentage = 20;
} else {
// If no discount is selected, show a message
[Link]([Link], "Please select a discount",
Toast.LENGTH_SHORT).show();
return;
}

// Calculate the discount and the final amount


double discountAmount = billAmount * discountPercentage / 100;
double finalAmount = billAmount - discountAmount;

// Display the discount and final amount


[Link]([Link]("Discount: %.2f\nFinal Amount: %.2f", discountAmount,
finalAmount));
}
}
});
}
}
OUTPUT
Question 8:
Create a login application to verify username and password. On successful login, redirect to another
activity that has a textview to display "welcome user" with logout button. On click of logout button, a
dialog should appear with ok and cancel buttons. On click of oK button, go back to the login activity
and on click of cancel button, stay on the same activity.
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent">

<EditText
android:id="@+id/edtUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:hint="Username"
android:padding="10dp"
android:textSize="18sp" />

<EditText
android:id="@+id/edtPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/edtUsername"
android:layout_marginTop="20dp"
android:hint="Password"
android:padding="10dp"
android:textSize="18sp"
android:inputType="textPassword" />

<Button
android:id="@+id/btnLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:textSize="18sp"
android:layout_below="@id/edtPassword"
android:layout_marginTop="30dp"
android:layout_centerHorizontal="true"/>
</RelativeLayout>

[Link]:
package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

import [Link];

public class MainActivity extends AppCompatActivity {

EditText edtUsername, edtPassword;


Button btnLogin;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

edtUsername = findViewById([Link]);
edtPassword = findViewById([Link]);
btnLogin = findViewById([Link]);

[Link](new [Link]() {
@Override
public void onClick(View v) {
String username = [Link]().toString();
String password = [Link]().toString();

if ([Link]("admin") && [Link]("shivam")) {

Intent intent = new Intent([Link], [Link]);


startActivity(intent);
} else {

[Link]([Link], "Invalid username or password",


Toast.LENGTH_SHORT).show();
}
}
});
}
}

activity_welcome.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/tvWelcome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome User"
android:textSize="24sp"
android:layout_centerHorizontal="true"
android:layout_marginTop="150dp" />

<Button
android:id="@+id/btnLogout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Logout"
android:textSize="18sp"
android:layout_below="@id/tvWelcome"
android:layout_marginTop="30dp"
android:layout_centerHorizontal="true"/>
</RelativeLayout>

[Link]:
package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

import [Link];
import [Link];

public class WelcomeActivity extends AppCompatActivity {

Button btnLogout;
TextView tvWelcome;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_welcome);

tvWelcome = findViewById([Link]);
btnLogout = findViewById([Link]);

// Setting the text dynamically on welcome activity


[Link]("Welcome User");

[Link](new [Link]() {
@Override
public void onClick(View v) {
// Show confirmation dialog on logout
new [Link]([Link])
.setMessage("Are you sure you want to logout?")
.setCancelable(false)
.setPositiveButton("OK", new [Link]() {
public void onClick(DialogInterface dialog, int id) {
// On click of OK, go back to Login activity
Intent intent = new Intent([Link], [Link]);
startActivity(intent);
finish(); // Close current activity
}
})
.setNegativeButton("Cancel", null) // Stay on same activity if Cancel
.show();
}
});
}
}
OUTPUT
Question 9:
Create an application to perform the operations of create, insert, delete, view and update, using sqlite
database.
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp">

<EditText
android:id="@+id/editName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Name"
android:inputType="textPersonName" />

<EditText
android:id="@+id/editEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Email"
android:inputType="textEmailAddress" />

<EditText
android:id="@+id/editId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter ID for update/delete"
android:inputType="number" />

<Button
android:id="@+id/btnInsert"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Insert" />

<Button
android:id="@+id/btnUpdate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Update" />

<Button
android:id="@+id/btnDelete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Delete" />
<Button
android:id="@+id/btnView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="View All" />
</LinearLayout>

[Link]:
package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {

EditText editName, editEmail, editId;


Button btnInsert, btnUpdate, btnDelete, btnView;
DBHelper DB;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

editName = findViewById([Link]);
editEmail = findViewById([Link]);
editId = findViewById([Link]);

btnInsert = findViewById([Link]);
btnUpdate = findViewById([Link]);
btnDelete = findViewById([Link]);
btnView = findViewById([Link]);

DB = new DBHelper(this);

[Link](view -> {
String name = [Link]().toString();
String email = [Link]().toString();

if ([Link]() || [Link]()) {
[Link]([Link], "Please fill all fields", Toast.LENGTH_SHORT).show();
return;
}

Boolean checkInsert = [Link](name, email);


if (checkInsert) {
[Link]([Link], "New Entry Inserted", Toast.LENGTH_SHORT).show();
[Link]("");
[Link]("");
} else {
[Link]([Link], "Insertion Failed", Toast.LENGTH_SHORT).show();
}
});

[Link](view -> {
String id = [Link]().toString();
String name = [Link]().toString();
String email = [Link]().toString();

if ([Link]() || [Link]() || [Link]()) {


[Link]([Link], "Please fill all fields", Toast.LENGTH_SHORT).show();
return;
}

Boolean checkUpdate = [Link](id, name, email);


if (checkUpdate) {
[Link]([Link], "Entry Updated", Toast.LENGTH_SHORT).show();
} else {
[Link]([Link], "Update Failed", Toast.LENGTH_SHORT).show();
}
});

[Link](view -> {
String id = [Link]().toString();
if ([Link]()) {
[Link]([Link], "Please enter ID", Toast.LENGTH_SHORT).show();
return;
}

Boolean checkDelete = [Link](id);


if (checkDelete) {
[Link]([Link], "Entry Deleted", Toast.LENGTH_SHORT).show();
} else {
[Link]([Link], "Deletion Failed", Toast.LENGTH_SHORT).show();
}
});

[Link](view -> {
Cursor res = [Link]();
if ([Link]() == 0) {
[Link]([Link], "No records to display", Toast.LENGTH_SHORT).show();
return;
}

StringBuilder buffer = new StringBuilder();


while ([Link]()) {
[Link]("ID: ").append([Link](0)).append("\n");
[Link]("Name: ").append([Link](1)).append("\n");
[Link]("Email: ").append([Link](2)).append("\n\n");
}

[Link] builder = new [Link]([Link]);


[Link]("User Entries");
[Link]([Link]());
[Link]("OK", null);
[Link]();
});
}
}

[Link]:
package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class DBHelper extends SQLiteOpenHelper {

public DBHelper(Context context) {


super(context, "[Link]", null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
[Link]("CREATE TABLE UserDetails(id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT, email TEXT)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
[Link]("DROP TABLE IF EXISTS UserDetails");
onCreate(db);
}

public Boolean insertUser(String name, String email) {


SQLiteDatabase db = [Link]();
ContentValues contentValues = new ContentValues();
[Link]("name", name);
[Link]("email", email);
long result = [Link]("UserDetails", null, contentValues);
return result != -1;
}
public Boolean updateUser(String id, String name, String email) {
SQLiteDatabase db = [Link]();
ContentValues contentValues = new ContentValues();
[Link]("name", name);
[Link]("email", email);
int result = [Link]("UserDetails", contentValues, "id = ?", new String[]{id});
return result > 0;
}

public Boolean deleteUser(String id) {


SQLiteDatabase db = [Link]();
int result = [Link]("UserDetails", "id = ?", new String[]{id});
return result > 0;
}

public Cursor getAllUsers() {


SQLiteDatabase db = [Link]();
return [Link]("SELECT * FROM UserDetails", null);
}
}
OUTPUT
Insert record:
Question 10:
Create an application to pick up any image from the native application gallery and display it on the
screen.
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="[Link]
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:padding="20dp">

<ImageView
android:id="@+id/imgGallery"
android:layout_width="250dp"
android:layout_height="250dp"
android:background="#ddd"
android:scaleType="centerCrop" />

<Button
android:id="@+id/btnGallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open Gallery"
android:layout_marginTop="20dp"/>
</LinearLayout>

[Link]:
package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {

private final int GALLERY_REQ_CODE = 1;


ImageView imgGallery;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);

setContentView([Link].activity_main);

imgGallery = findViewById([Link]);
Button btnGallery = findViewById([Link]);

[Link](new [Link]() {
@Override
public void onClick(View view) {
Intent iGallery = new Intent(Intent.ACTION_PICK);
[Link]([Link].EXTERNAL_CONTENT_URI);
startActivityForResult(iGallery,GALLERY_REQ_CODE);

}
});

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

[Link](requestCode, resultCode, data);

if(resultCode==RESULT_OK){
if(requestCode==GALLERY_REQ_CODE){
[Link]([Link]());

}
}
}
}
OUTPUT
Question 11:
Create an application to take picture using native application
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="[Link]
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:padding="20dp">

<ImageView
android:id="@+id/imageView"
android:layout_width="250dp"
android:layout_height="250dp"
android:background="#ddd"
android:scaleType="centerCrop" />

<Button
android:id="@+id/btnOpenCamera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open Camera"
android:layout_marginTop="20dp"/>
</LinearLayout>

[Link]:
package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends Activity {

ImageView imageView;
Button btnOpenCamera;
static final int REQUEST_IMAGE_CAPTURE = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
imageView = findViewById([Link]);
btnOpenCamera = findViewById([Link]);

[Link](v -> {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE);
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
[Link](requestCode, resultCode, data);

if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {


Bitmap photo = (Bitmap) [Link]().get("data");
[Link](photo);
}
}
}
OUTPUT

You might also like