Android App with GUI and Event Listeners
Android App with GUI and Event Listeners
Activity_xml.xml
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
Activity_xml.xml
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:text="Button 2" />
</LinearLayout>
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Type something" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_below="@id/editText"
android:text="Submit"
android:layout_marginTop="8dp" />
</RelativeLayout>
<ImageView
android:id="@+id/imageView"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@android:drawable/ic_menu_gallery"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Image Preview"
app:layout_constraintTop_toBottomOf="@id/imageView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="8dp"/>
</[Link]>
</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 {
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
// Initialize views
button1 = findViewById([Link].button1);
button2 = findViewById([Link].button2);
button3 = findViewById([Link].button3);
editText = findViewById([Link]);
imageView = findViewById([Link]);
textView = findViewById([Link]);
[Link](new [Link]() {
@Override
public void onClick(View v) {
[Link]([Link], "Button 2 Clicked",
Toast.LENGTH_SHORT).show();
}
});
[Link](new [Link]() {
@Override
public void onClick(View v) {
String inputText = [Link]().toString();
if (![Link]()) {
[Link](inputText);
[Link]([Link], "Submitted: " +
inputText, Toast.LENGTH_SHORT).show();
} else {
[Link]([Link], "Please enter text",
Toast.LENGTH_SHORT).show();
}
}
});
Activity_xml.xml
<Button
android:id="@+id/drawButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Draw Shape" />
<[Link]
android:id="@+id/shapeView"
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_marginTop="16dp" />
</LinearLayout>
[Link]
// src/com/example/shapedrawer/[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class ShapeView extends View {
private Paint paint;
private int shapeType = 0; // 0 = circle, 1 = square, 2 = rectangle, 3 = triangle
@Override
protected void onDraw(Canvas canvas) {
[Link](canvas);
switch (shapeType) {
case 0:
// Draw a Circle
[Link](width / 2, height / 2, [Link](width, height) / 4, paint);
break;
case 1:
// Draw a Square
[Link](width / 4, height / 4, 3 * width / 4, 3 * height / 4, paint);
break;
case 2:
// Draw a Rectangle
[Link](width / 4, height / 3, 3 * width / 4, 2 * height / 3, paint);
break;
case 3:
// Draw a Triangle
Path path = new Path();
[Link](width / 2, height / 4);
[Link](width / 4, 3 * height / 4);
[Link](3 * width / 4, 3 * height / 4);
[Link]();
[Link](path, paint);
break;
}
}
}
[Link]
// src/com/example/shapedrawer/[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
shapeView = findViewById([Link]);
Button drawButton = findViewById([Link]);
[Link](new [Link]() {
@Override
public void onClick(View v) {
clickCount = (clickCount + 1) % 4; // Cycle through shapes 0, 1, 2, 3
[Link](clickCount);
}
});
}
}
[Link] (already there under manifest)
<application
android:allowBackup="true"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/[Link]">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="[Link]" />
<category android:name="[Link]" />
</intent-filter>
</activity>
</application>
</manifest>
[Link] an application that makes use of databases.
Activity_xml.xml
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@Override
public void onCreate(SQLiteDatabase db) {
[Link]("CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY
AUTOINCREMENT, NAME TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
[Link]("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
// Insert data
public boolean insertData(String name) {
SQLiteDatabase db = [Link]();
ContentValues contentValues = new ContentValues();
[Link](COL_2, name);
long result = [Link](TABLE_NAME, null, contentValues);
return result != -1;
}
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
DatabaseHelper myDb;
EditText editTextName;
TextView textViewDisplay;
Button buttonAdd, buttonViewAll, buttonDelete;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
editTextName = findViewById([Link]);
textViewDisplay = findViewById([Link]);
buttonAdd = findViewById([Link]);
buttonViewAll = findViewById([Link]);
buttonDelete = findViewById([Link]);
addData();
viewAllData();
deleteData();
}
// Method to add data
public void addData() {
[Link](new [Link]() {
@Override
public void onClick(View v) {
String name = [Link]().toString();
if ([Link]()) {
[Link]([Link], "Please enter a name",
Toast.LENGTH_SHORT).show();
return;
}
boolean isInserted = [Link](name);
if (isInserted) {
[Link]([Link], "Name Added",
Toast.LENGTH_SHORT).show();
[Link]("");
} else {
[Link]([Link], "Error Adding Name",
Toast.LENGTH_SHORT).show();
}
}
});
}
<manifest xmlns:android="[Link]
package="[Link]">
<application
android:allowBackup="true"
android:label="Simple Database App"
android:supportsRtl="true"
android:theme="@style/[Link]">
<activity android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="[Link]" />
<category android:name="[Link]" />
</intent-filter>
</activity>
</application>
</manifest>
[Link] a calculator application that uses Layout Managers and event listeners.
Activity_xml.xml
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
display = findViewById([Link]);
setupButtonListeners();
}
Activity_xml.xml
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@Override
public void onCreate(SQLiteDatabase db) {
[Link]("CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY
AUTOINCREMENT, NAME TEXT, SUBJECT TEXT, SCORE INTEGER)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
[Link]("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
// Insert data
public boolean insertData(String name, String subject, int score) {
SQLiteDatabase db = [Link]();
ContentValues contentValues = new ContentValues();
[Link](COL_2, name);
[Link](COL_3, subject);
[Link](COL_4, score);
long result = [Link](TABLE_NAME, null, contentValues);
return result != -1;
}
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
DatabaseHelper myDb;
EditText editTextName, editTextSubject, editTextScore;
TextView textViewResults;
Button buttonAdd, buttonViewAll;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
editTextName = findViewById([Link]);
editTextSubject = findViewById([Link]);
editTextScore = findViewById([Link]);
textViewResults = findViewById([Link]);
buttonAdd = findViewById([Link]);
buttonViewAll = findViewById([Link]);
addData();
viewAllData();
}
<application
android:allowBackup="true"
android:label="Student Results App"
android:supportsRtl="true"
android:theme="@style/[Link]">
<activity android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="[Link]" />
<category android:name="[Link]" />
</intent-filter>
</activity>
</application>
</manifest>
7. Design an android application using Cordova for a user login screen with
username,password, reset button and a submit button. Also, include header image and a
label.
html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Login Screen</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="css/[Link]">
</head>
<body>
<div class="login-container">
<img src="img/[Link]" alt="Header Image" class="header-img">
<h2 class="label">Login</h2>
<form id="loginForm">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required>
<div class="button-container">
<button type="button" id="resetBtn">Reset</button>
<button type="button" id="submitBtn">Submit</button>
</div>
</form>
</div>
<script src="[Link]"></script>
<script src="js/[Link]"></script>
</body>
</html>
css
body {
font-family: Arial, sans-serif;
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
background-color: #f7f7f7;
}
.login-container {
width: 90%;
max-width: 400px;
padding: 20px;
border-radius: 8px;
background-color: white;
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
}
.header-img {
width: 100%;
height: auto;
margin-bottom: 20px;
}
.label {
text-align: center;
margin-bottom: 20px;
font-size: 24px;
color: #333;
}
input[type="text"], input[type="password"] {
width: 100%;
padding: 10px;
margin: 10px 0;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
.button-container {
display: flex;
justify-content: space-between;
}
button {
width: 45%;
padding: 10px;
font-size: 16px;
border: none;
border-radius: 4px;
color: white;
cursor: pointer;
}
#resetBtn {
background-color: #d9534f;
}
#submitBtn {
background-color: #5cb85c;
}
Js
[Link]('deviceready', function() {
// Get form elements
const resetBtn = [Link]("resetBtn");
const submitBtn = [Link]("submitBtn");
const usernameInput = [Link]("username");
const passwordInput = [Link]("password");
[Link]
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Current Location</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="css/[Link]">
</head>
<body>
<div class="location-container">
<h2>Find My Location</h2>
<button id="getLocationBtn">Get Current Location</button>
<div id="locationDisplay">
<p id="latitude">Latitude: </p>
<p id="longitude">Longitude: </p>
</div>
</div>
<script src="[Link]"></script>
<script src="js/[Link]"></script>
</body>
</html>
css/[Link]
body {
font-family: Arial, sans-serif;
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
}
.location-container {
width: 90%;
max-width: 400px;
padding: 20px;
border-radius: 8px;
background-color: white;
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
text-align: center;
}
h2 {
color: #333;
margin-bottom: 20px;
}
button {
padding: 10px 20px;
font-size: 16px;
border: none;
border-radius: 4px;
background-color: #5cb85c;
color: white;
cursor: pointer;
margin-bottom: 20px;
}
#locationDisplay p {
font-size: 18px;
color: #555;
}
js/[Link]
[Link]("deviceready", function() {
const getLocationBtn = [Link]("getLocationBtn");
const latitudeDisplay = [Link]("latitude");
const longitudeDisplay = [Link]("longitude");
[Link]("click", function() {
[Link](onSuccess, onError, {
enableHighAccuracy: true
});
});
function onSuccess(position) {
const latitude = [Link];
const longitude = [Link];
Activity_xml.xml
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<EditText
android:id="@+id/editTextRecipient"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Recipient Email"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/editTextSubject"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Subject" />
<EditText
android:id="@+id/editTextMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Message"
android:inputType="textMultiLine"
android:minLines="4" />
<Button
android:id="@+id/buttonSendEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send Email" />
</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);
try {
// Start the email client
startActivity([Link](emailIntent, "Choose an email
client"));
} catch (Exception e) {
[Link](this, "No email client found on device.",
Toast.LENGTH_SHORT).show();
}
}
}
[Link] an android application to generate notifications on receiving mails or messages.
Acitivity_main.xml
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Click Me"
android:onClick="shownofn"/>
</LinearLayout>
[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];
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
}
public void shownvofn(View view) {
//this code is for before Oreo
// 1. create a Intent
// 2. create a PendingIntent
// 3. design Notification
// 4. initialise NotifivstionManager
[Link](0, notification);
int notificationId = 1;
String channelId = "channel-01";
String channelName = "Channel Name";
int importance = NotificationManager.IMPORTANCE_HIGH;
[Link](notificationId, [Link]());
}
}
[Link] calculator in Android and Cordova
<form id="bmiForm">
<label for="weight">Weight (kg):</label>
<input type="number" id="weight" name="weight" required>
<script src="[Link]"></script>
<script src="js/[Link]"></script>
</body>
</html>
css/[Link]
body {
font-family: Arial, sans-serif;
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
}
.bmi-container {
width: 90%;
max-width: 400px;
padding: 20px;
border-radius: 8px;
background-color: white;
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
text-align: center;
}
h2 {
color: #333;
margin-bottom: 20px;
}
label {
font-size: 16px;
color: #555;
}
input[type="number"] {
width: 100%;
padding: 10px;
margin: 10px 0;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
button {
width: 100%;
padding: 10px;
font-size: 16px;
border: none;
border-radius: 4px;
background-color: #5cb85c;
color: white;
cursor: pointer;
margin-top: 10px;
}
.result p {
font-size: 18px;
color: #333;
}
js/[Link]
[Link]("deviceready", function() {
const calculateBtn = [Link]("calculateBtn");
const weightInput = [Link]("weight");
const heightInput = [Link]("height");
const bmiValueDisplay = [Link]("bmiValue");
const bmiCategoryDisplay = [Link]("bmiCategory");
[Link]("click", function() {
const weight = parseFloat([Link]);
const height = parseFloat([Link]) / 100; // Convert height from cm to meters
[Link] = bmi;
[Link] = getBMICategory(bmi);
} else {
alert("Please enter valid values for weight and height.");
}
});
function getBMICategory(bmi) {
if (bmi < 18.5) return "Underweight";
if (bmi >= 18.5 && bmi < 24.9) return "Normal weight";
if (bmi >= 25 && bmi < 29.9) return "Overweight";
return "Obesity";
}
});
Activity_xml.xml
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<ListView android:id="@+id/listViewBooks"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="@android:color/darker_gray"
android:dividerHeight="1dp" />
<EditText android:id="@+id/editTextBookName"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:hint="Enter Book Name" />
<Button android:id="@+id/buttonReserve"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:text="Reserve Book" />
</LinearLayout>
[Link]
package [Link];
import [Link];
import [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);
listViewBooks = findViewById([Link]);
editTextBookName = findViewById([Link]);
buttonBorrow = findViewById([Link]);
buttonReserve = findViewById([Link]);
[Link]
package [Link];