Android Application Development Lab 2024-25
Android Application Development Lab 2024-25
LAB MANUAL
This XML file defines the user interface. Open res/layout/activity_main.xml and modify it to
include an EditText for entering the user's name and a TextView to display the greeting message.
<androidx.constraintlayout.widget.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=".MainActivity">
<EditText
android:id="@+id/nameEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:padding="16dp"
android:inputType="text"
android:textSize="18sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<Button
android:id="@+id/greetButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Greet Me"
android:layout_marginTop="30dp"
app:layout_constraintTop_toBottomOf="@id/nameEditText"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="@+id/greetingTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello!"
android:textSize="24sp"
android:layout_marginTop="50dp"
app:layout_constraintTop_toBottomOf="@id/greetButton"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Now, modify the MainActivity.java to handle the logic of taking user input and displaying the
greeting message.
package com.example.hellouserapp;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find references to the EditText, Button, and TextView
greetButton.setOnClickListener(new View.OnClickListener() {
@Override
// If the name is not empty, update the TextView with the greeting message
if (!name.isEmpty()) {
} else {
greetingTextView.setText("Hello!");
});
1. The user will be prompted to enter their name in the EditText field.
2. When the user clicks the Greet Me button, the greeting message will appear below the button
with the text: "Hello, [user's name]!"
If the user doesn't enter a name, the message will remain as "Hello!".
(b) Create an application that takes the name from a text box and shows hello message along
with the name entered in text box, when the user clicks the OK button
To create an Android application that takes the name from a text box and displays a "Hello" message with
the entered name when the user clicks the "OK" button, you can follow these steps:
<androidx.constraintlayout.widget.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=".MainActivity">
<EditText
android:id="@+id/nameEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="16dp"
android:textSize="18sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="@+id/okButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
android:layout_marginTop="30dp"
app:layout_constraintTop_toBottomOf="@id/nameEditText"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="@+id/greetingTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello!"
android:textSize="24sp"
android:layout_marginTop="50dp"
app:layout_constraintTop_toBottomOf="@id/okButton"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Now, open MainActivity.java and add the logic for handling user input and displaying the greeting
message. You’ll need to get the name from the EditText, and when the user clicks the OK button, the
app will update the TextView to say "Hello, [name]".
package com.example.hellonameapp;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
okButton.setOnClickListener(new View.OnClickListener() {
@Override
if (!name.isEmpty()) {
} else {
greetingTextView.setText("Hello!");
}
});
To create an Android application that takes the name from a text box and displays a "Hello" message with
the entered name when the user clicks the "OK" button, you can follow these steps:
xml
Copy code
<androidx.constraintlayout.widget.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=".MainActivity">
<EditText
android:id="@+id/nameEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:padding="16dp"
android:textSize="18sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="@+id/okButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
android:layout_marginTop="30dp"
app:layout_constraintTop_toBottomOf="@id/nameEditText"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="@+id/greetingTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello!"
android:textSize="24sp"
android:layout_marginTop="50dp"
app:layout_constraintTop_toBottomOf="@id/okButton"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Now, open MainActivity.java and add the logic for handling user input and displaying the greeting
message. You’ll need to get the name from the EditText, and when the user clicks the OK button, the
app will update the TextView to say "Hello, [name]".
java
Copy code
package com.example.hellonameapp;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView greetingTextView =
findViewById(R.id.greetingTextView);
// Set an OnClickListener on the OK button
okButton.setOnClickListener(new View.OnClickListener() {
@Override
if (!name.isEmpty()) {
} else {
greetingTextView.setText("Hello!");
});
1. Build the Application: In Android Studio, click Build > Make Project to ensure there are no
errors.
2. Set up an Emulator (if you don't have a real device):
○ Go to Tools > AVD Manager (Android Virtual Device Manager) and create a new virtual
device.
○ Select a device, then choose the Android version (system image) you want to use.
○ After the emulator is created, click the Play button to launch the emulator.
3. Run the App: Click the Run button (green triangle) or press Shift + F10 to run the app.
4. Test the App:
○ Enter a name in the EditText field.
○ Tap the OK button to see the greeting message displayed below, like "Hello, [name]".
Q.2.Create a screen that has input boxes for User Name, Password, Address, Gender (radio buttons
for male and female), Age (numeric), Date of Birth (Date Picket), State (Spinner) and a Submit
button. On clicking the submit button, print all the data below the Submit Button. Use (a) Linear
Layout , (b) Relative Layout and (c) Grid Layout or Table Layout.
To create a screen that includes input boxes for User Name, Password, Address, Gender (radio buttons for
Male and Female), Age (numeric), Date of Birth (Date Picker), State (Spinner), and a Submit button, we'll
create three versions of the same layout using different types of layouts: LinearLayout, RelativeLayout,
and GridLayout or TableLayout.
1. Using LinearLayout
In this layout, all the components are stacked vertically. We'll use LinearLayout with vertical
orientation for this purpose.
activity_main.xml (LinearLayout)
xml
Copy code
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="User Name"
android:inputType="text" />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword" />
<!-- Address -->
<EditText
android:id="@+id/address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Address"
android:inputType="text" />
<RadioGroup
android:id="@+id/genderGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/maleRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male" />
<RadioButton
android:id="@+id/femaleRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female" />
</RadioGroup>
<EditText
android:id="@+id/age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Age"
android:inputType="number" />
<DatePicker
android:id="@+id/dob"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Spinner
android:id="@+id/stateSpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/submitButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit" />
<TextView
android:id="@+id/displayData"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:layout_marginTop="16dp" />
</LinearLayout>
2. Using RelativeLayout
In this layout, elements are positioned relative to each other using RelativeLayout. This allows for
more flexible positioning of UI elements.
activity_main.xml (RelativeLayout)
xml
Copy code
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="User Name"
android:inputType="text"
android:layout_alignParentTop="true" />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"
android:layout_below="@id/username"
android:layout_marginTop="16dp" />
<EditText
android:id="@+id/address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Address"
android:inputType="text"
android:layout_below="@id/password"
android:layout_marginTop="16dp" />
<RadioGroup
android:id="@+id/genderGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@id/address"
android:layout_marginTop="16dp">
<RadioButton
android:id="@+id/maleRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male" />
<RadioButton
android:id="@+id/femaleRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female" />
</RadioGroup>
<EditText
android:id="@+id/age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Age"
android:inputType="number"
android:layout_below="@id/genderGroup"
android:layout_marginTop="16dp" />
<DatePicker
android:id="@+id/dob"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/age"
android:layout_marginTop="16dp" />
<Spinner
android:id="@+id/stateSpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/dob"
android:layout_marginTop="16dp" />
<Button
android:id="@+id/submitButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit"
android:layout_below="@id/stateSpinner"
android:layout_marginTop="16dp" />
<TextView
android:id="@+id/displayData"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:layout_below="@id/submitButton"
android:layout_marginTop="16dp" />
</RelativeLayout>
GridLayout or TableLayout arranges the UI elements in a grid-like fashion. I'll use a GridLayout
for this example. You could also use TableLayout in a similar way.
activity_main.xml (GridLayout)
xml
Copy code
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2"
android:padding="16dp">
<TextView
android:text="User Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text" />
<TextView
android:text="Password"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword" />
<TextView
android:text="Address"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text" />
<TextView
android:text="Gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioGroup
android:id="@+id/genderGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/maleRadio"
android:text="Male" />
<RadioButton
android:id="@+id/femaleRadio"
android:text="Female" />
</RadioGroup>
<TextView
android:text="Age"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number" />
android:text="Date of Birth"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<DatePicker
android:id="@+id/dob"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="State"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Spinner
android:id="@+id/stateSpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/submitButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit"
android:layout_columnSpan="2" />
<TextView
android:id="@+id/displayData"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:layout_columnSpan="2"
android:layout_marginTop="16dp" />
</GridLayout>
4. MainActivity.java
Now, we'll implement the logic for handling the form submission. When the Submit button is clicked, it
should collect the entered data and display it in the TextView.
java
Copy code
package com.example.formsubmission;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.DatePicker;
import android.widget.ArrayAdapter;
import androidx.appcompat.app.AppCompatActivity;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter<CharSequence> adapter =
ArrayAdapter.createFromResource(this,
R.array.states, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdo
wn_item);
stateSpinner.setAdapter(adapter);
submitButton.setOnClickListener(new View.OnClickListener() {
@Override
int selectedGenderId =
genderGroup.getCheckedRadioButtonId();
RadioButton selectedGender =
findViewById(selectedGenderId);
String stateText =
stateSpinner.getSelectedItem().toString();
});
}
}
Q.3.Develop an application that shows names as a list and on selecting a name it should show
the details of the candidate on the next screen with a “Back” button. If the screen is rotated to
landscape mode (width greater than height), then the screen should show list on left fragment
and details on right fragment instead of second screen with back button. Use Fragment
transactions and Rotation event listener
● Open Android Studio and create a new project with an Empty Activity.
● Select Java as the language and API 21 as the minimum SDK.
In portrait mode, we show the list and details in separate screens, but in landscape, both fragments
(list and details) will be shown side by side.
xml
Copy code
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
3. ListFragment (ListFragment.java)
This fragment displays the list of names. When a name is selected, it triggers the display of the
details in the next screen or fragment.
java
Copy code
public class ListFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup
container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_list,
container, false);
ListView listView = view.findViewById(R.id.listView);
ArrayAdapter<String> adapter = new
ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1,
names);
listView.setAdapter(adapter);
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container,
detailsFragment)
.addToBackStack(null)
.commit();
} else {
// Landscape mode: Replace the fragment directly
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container,
detailsFragment)
.commit();
}
});
return view;
}
}
4. DetailsFragment (DetailsFragment.java)
This fragment shows the details of the selected name. It has a "Back" button in portrait mode.
java
Copy code
public class DetailsFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup
container, Bundle savedInstanceState) {
if (getArguments() != null) {
name = getArguments().getString(ARG_NAME);
}
return view;
}
}
5. Layout for fragment_list.xml
xml
Copy code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
xml
Copy code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/nameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name:"
android:textSize="20sp" />
<Button
android:id="@+id/backButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back"
android:visibility="gone"/>
</LinearLayout>
java
Copy code
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (getResources().getConfiguration().orientation ==
Configuration.ORIENTATION_PORTRAIT) {
// Portrait: Display ListFragment
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, new
ListFragment())
.commit();
} else {
// Landscape: Display both ListFragment and
DetailsFragment
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, new
ListFragment())
.commit();
// Optionally, show details fragment by default in
landscape mode
}
}
}
When the screen rotates to landscape, the list and details will be shown side by side. If the user
selects a name, the right fragment will update with the details.
.
Q.4.Develop an application that uses a menu with 3 options for dialing a number, opening a
website and to send an SMS. On selecting an option, the appropriate action should be invoked
using intents.
● Open Android Studio and create a new project with an Empty Activity.
● Choose Java as the language and API 21 as the minimum SDK.
xml
Copy code
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/welcomeText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_centerInParent="true"/>
</RelativeLayout>
3. Create Menu (res/menu/menu_main.xml)
xml
Copy code
<menu xmlns:android="http://schemas.android.com/apk/res/android">
</menu>
java
Copy code
package com.example.intentmenu;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.Menu;
import android.view.MenuItem;
import androidx.appcompat.app.AppCompatActivity;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
@Override
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
@Override
switch (item.getItemId()) {
case R.id.dial_number:
dialNumber();
return true;
case R.id.open_website:
openWebsite();
return true;
case R.id.send_sms:
sendSMS();
return true;
default:
return super.onOptionsItemSelected(item);
startActivity(dialIntent);
startActivity(webIntent);
smsIntent.putExtra("sms_body", "Hello!");
startActivity(smsIntent);
5. Permissions (AndroidManifest.xml)
xml
Copy code
How It Works:
To develop an Android application that inserts notifications into the notification area and displays a
toast with the notification details when the notification is inserted, follow these steps:
1. Layout (activity_main.xml)
xml
Copy code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btnNotify"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Notification"
android:layout_centerInParent="true"/>
</RelativeLayout>
2. AndroidManifest.xml
Ensure no special permissions are required for notifications, but here's the basic manifest structure.
xml
Copy code
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.notifications">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.DayNight">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
3. MainActivity.java
Create the notification and show the toast when the button is clicked.
java
Copy code
package com.example.notifications;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
notificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
// Show Notification
notificationManager.notify(1, notification);
4. Key Points:
Q.6.Create an application that uses a text file to store user names and passwords (tab separated
fields and one record per line). When the user submits a login name and password through a
screen, the details should be verified with the text file data and if
they match, show a dialog saying that login is successful. Otherwise, show the dialog with Login
Failed message.
1. Layout (activity_main.xml)
xml
Copy code
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<EditText
android:id="@+id/edtUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username" />
<EditText
android:id="@+id/edtPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword" />
<Button
android:id="@+id/btnLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login" />
</LinearLayout>
3. MainActivity.java
Handles reading the text file, verifying credentials, and showing a dialog.
java
Copy code
package com.example.loginwithfile;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtUsername = findViewById(R.id.edtUsername);
edtPassword = findViewById(R.id.edtPassword);
btnLogin = findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(v -> {
String username = edtUsername.getText().toString();
String password = edtPassword.getText().toString();
if (verifyCredentials(username, password)) {
showDialog("Login Successful", "Welcome " +
username);
} else {
showDialog("Login Failed", "Invalid username or
password.");
}
});
}
Key Points:
● The verifyCredentials() method reads the users.txt file from the assets folder,
checks if the entered username and password match, and returns true or false.
● A dialog is displayed based on whether the credentials are correct.
Q.7.Create a user registration application that stores the user details in a database
table.
To create a user registration application that stores user details in a database table, follow these steps:
1. Project Setup
java
Copy code
package com.example.registrationapp;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
@Override
db.execSQL(TABLE_CREATE);
@Override
onCreate(db);
Create a simple registration form with EditText fields for name, email, password, and a Button to
submit.
xml
Copy code
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<EditText
android:id="@+id/edtName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name" />
<EditText
android:id="@+id/edtEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/edtPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword" />
<Button
android:id="@+id/btnRegister"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Register" />
</LinearLayout>
java
Copy code
package com.example.registrationapp;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtName = findViewById(R.id.edtName);
edtEmail = findViewById(R.id.edtEmail);
edtPassword = findViewById(R.id.edtPassword);
btnRegister = findViewById(R.id.btnRegister);
btnRegister.setOnClickListener(v -> {
if (name.isEmpty() || email.isEmpty() ||
password.isEmpty()) {
Toast.makeText(MainActivity.this, "Please fill all
fields", Toast.LENGTH_SHORT).show();
} else {
});
SQLiteDatabase db = dbHelper.getWritableDatabase();
db.execSQL(insertQuery);
5. Key Points
● Database Helper: DatabaseHelper manages SQLite database operations (create, upgrade,
insert).
● Layout: Simple form with fields for name, email, and password.
Q.8.Create a database and a user table where the details of login names and passwords are
stored. Insert some names and passwords initially. Now the login details entered by the user
should be verified with the database and an appropriate dialog should be shown to the user.
Here's how to create an Android app with a database to store login details (username and password),
verify the user's input, and show an appropriate dialog based on the verification.
We create a DatabaseHelper class to manage SQLite database creation and CRUD operations.
java
Copy code
package com.example.loginapp;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int
newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_USERS);
onCreate(db);
}
}
2. Layout (activity_main.xml)
Create a simple login form with EditText for username and password and a Button for login.
xml
Copy code
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<EditText
android:id="@+id/edtUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username" />
<EditText
android:id="@+id/edtPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword" />
<Button
android:id="@+id/btnLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login" />
</LinearLayout>
java
Copy code
package com.example.loginapp;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtUsername = findViewById(R.id.edtUsername);
edtPassword = findViewById(R.id.edtPassword);
btnLogin = findViewById(R.id.btnLogin);
if (verifyCredentials(username, password)) {
showDialog("Login Successful", "Welcome " +
username);
} else {
showDialog("Login Failed", "Invalid username or
password.");
}
});
}
if (cursor.moveToFirst()) {
cursor.close();
return true;
} else {
cursor.close();
return false;
}
}
● DatabaseHelper: This class creates the SQLite database with a users table and inserts some
initial user data (user1, pass1 and user2, pass2).
● Login Verification: The verifyCredentials method checks if the entered username and
password match any record in the users table.
● Dialog: After verifying credentials, the app shows a success or failure dialog.
5. Result
● On entering correct login details, a success dialog will appear.
● On entering incorrect details, a failure dialog will appear.