MAD Practical File
MAD Practical File
2
Create an android app to display various android lifecycle phases.
10
Create an application to pick any image from the native application
gallery and display it on the screen.
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];
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
}
}
OUTPUT
Emulator Andriod phone
Question 2:
[Link]:
package [Link];
import [Link];
import [Link];
import [Link];
<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"/>
<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"/>
</RelativeLayout>
[Link]:
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@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");
}
});
}
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];
EditText etRupees;
Button btnDollar, btnYen, btnPound;
TextView tvResult;
@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");
}
});
}
if ([Link]()) {
[Link](this, "Please enter an amount", Toast.LENGTH_SHORT).show();
return;
}
[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];
Spinner spinner;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
spinner = findViewById([Link]);
imageView = findViewById([Link]);
[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">
<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>
[Link]:
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
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);
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;
}
<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];
@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();
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];
Button btnLogout;
TextView tvWelcome;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_welcome);
tvWelcome = findViewById([Link]);
btnLogout = findViewById([Link]);
[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];
@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;
}
[Link](view -> {
String id = [Link]().toString();
String name = [Link]().toString();
String email = [Link]().toString();
[Link](view -> {
String id = [Link]().toString();
if ([Link]()) {
[Link]([Link], "Please enter ID", Toast.LENGTH_SHORT).show();
return;
}
[Link](view -> {
Cursor res = [Link]();
if ([Link]() == 0) {
[Link]([Link], "No records to display", Toast.LENGTH_SHORT).show();
return;
}
[Link]:
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@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);
}
<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];
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){
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];
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);