P=1
Create a login screen with EditText fields for username and password and a
button to submit. Include validation for empty screen.
MainActivity.java
package com.example.loginscreen;
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 {
private EditText etUsername, etPassword;
private Button btnLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize UI components
etUsername = findViewById(R.id.etUsername);
etPassword = findViewById(R.id.etPassword);
btnLogin = findViewById(R.id.btnLogin);
// Set button click listener
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
validateLogin();
}
});
}
private void validateLogin() {
String username = etUsername.getText().toString().trim();
String password = etPassword.getText().toString().trim();
if (username.isEmpty() || password.isEmpty()) {
Toast.makeText(this, "Username and Password cannot be empty",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Login Successful",
Toast.LENGTH_SHORT).show();
}
}
}
activity_main.xml
<?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"
android:gravity="center">
<EditText
android:id="@+id/etUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:inputType="textPersonName"
android:padding="10dp"/>
<EditText
android:id="@+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"
android:padding="10dp"
android:layout_marginTop="10dp"/>
<Button
android:id="@+id/btnLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:layout_marginTop="20dp"/>
</LinearLayout>
Output:
Create an android application in android studio in java to display Alertdialog on pressing
the back button.
MainActivity.java
package com.example.alertdialogbox;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onBackPressed() {
// Show an AlertDialog when the back button is pressed
new AlertDialog.Builder(this)
.setTitle("Exit App")
.setMessage("Are you sure you want to exit?")
.setPositiveButton("Yes", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish(); // Exit the app
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss(); // Dismiss the dialog
}
})
.setCancelable(false) // Prevents dismissing by tapping
outside
.show();
}
}
activity_main.xml
<?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:gravity="center"
android:padding="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press the back button to see the AlertDialog."
android:textSize="18sp"
android:textAlignment="center"/>
</LinearLayout>
output
P-2
Explain android activity life cycle in detail . implement a simple app that logs the life cycle methods
onCreate() , onResume() ,onPause() ,onStop() ,onDestroy()
MainActivity.java
package com.example.lifecyclelogger;
import android.os.Bundle;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "LifecycleLogger";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate() called");
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume() called");
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause() called");
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG, "onStop() called");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy() called");
}
}
output
P=3
Write a program Design a listview that display a list of items . each item should be a simple text
view with a string from an array of names . the list shold be populated dynamically in your
activity
MainActivity.java
package com.example.listviewexample;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
c // Sample list of names
String[] names = {"Alice", "Bob", "Charlie", "David", "Emma", "Frank",
"Grace", "Hannah", "Isaac", "Jack"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find the ListView by its ID
ListView listView = findViewById(R.id.listView);
// Create an ArrayAdapter to populate the ListView
ArrayAdapter<String> adapter = new ArrayAdapter<>(
this,
android.R.layout.simple_list_item_1, // Predefined layout for
list items
names // Data source
);
// Set the adapter to the ListView
listView.setAdapter(adapter);
}
}
activity_main.xml
<?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">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Output
P=4
Create registration form with the following fields username a text input email address input
gender radio button with options male and female password input submit button
MainActivity.java
package com.example.registrationform;
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.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private EditText etUsername, etEmail, etPassword;
private RadioGroup rgGender;
private Button btnSubmit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etUsername = findViewById(R.id.etUsername);
etEmail = findViewById(R.id.etEmail);
etPassword = findViewById(R.id.etPassword);
rgGender = findViewById(R.id.rgGender);
btnSubmit = findViewById(R.id.btnSubmit);
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = etUsername.getText().toString().trim();
String email = etEmail.getText().toString().trim();
String password = etPassword.getText().toString().trim();
int selectedGenderId = rgGender.getCheckedRadioButtonId();
RadioButton selectedGender = findViewById(selectedGenderId);
String gender = selectedGender.getText().toString();
if (username.isEmpty() || email.isEmpty() ||
password.isEmpty()) {
Toast.makeText(MainActivity.this, "Please fill all
fields", Toast.LENGTH_SHORT).show();
} else {
String message = "Registered:\nUsername: " + username + "\
nEmail: " + email + "\nGender: " + gender;
Toast.makeText(MainActivity.this, message,
Toast.LENGTH_LONG).show();
}
}
});
}
}
Activitymain.xml
<?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">
<EditText
android:id="@+id/etUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:inputType="text" />
<EditText
android:id="@+id/etEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email"
android:inputType="textEmailAddress" />
<RadioGroup
android:id="@+id/rgGender"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rbMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male"
android:checked="true"/>
<RadioButton
android:id="@+id/rbFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female"/>
</RadioGroup>
<EditText
android:id="@+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword" />
<Button
android:id="@+id/btnSubmit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Register"/>
</LinearLayout>
Output
P=5
Create a registration form with the following field username takes input email address input
phone number takes input password password improve submit button floating action button
Main Activity.java
package com.example.registrationform;
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;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
public class MainActivity extends AppCompatActivity {
private EditText username, email, phone, password;
private Button submitBtn;
private FloatingActionButton fab;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username = findViewById(R.id.username);
email = findViewById(R.id.email);
phone = findViewById(R.id.phone);
password = findViewById(R.id.password);
submitBtn = findViewById(R.id.submitBtn);
fab = findViewById(R.id.fab);
submitBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String user = username.getText().toString();
String mail = email.getText().toString();
String phoneNum = phone.getText().toString();
String pass = password.getText().toString();
if (user.isEmpty() || mail.isEmpty() || phoneNum.isEmpty() ||
pass.isEmpty()) {
Toast.makeText(MainActivity.this, "Please fill all
fields", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Registration
Successful", Toast.LENGTH_SHORT).show();
}
}
});
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Floating Button Clicked",
Toast.LENGTH_SHORT).show();
}
});
}
}
Activitymain.xml
<?xml version="1.0" encoding="utf-8"?>
<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/username"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Username"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_margin="16dp"/>
<EditText
android:id="@+id/email"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Email Address"
android:inputType="textEmailAddress"
app:layout_constraintTop_toBottomOf="@id/username"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_margin="16dp"/>
<EditText
android:id="@+id/phone"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Phone Number"
android:inputType="phone"
app:layout_constraintTop_toBottomOf="@id/email"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_margin="16dp"/>
<EditText
android:id="@+id/password"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"
app:layout_constraintTop_toBottomOf="@id/phone"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_margin="16dp"/>
<Button
android:id="@+id/submitBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
app:layout_constraintTop_toBottomOf="@id/password"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_margin="16dp"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_input_add"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_margin="16dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Output:
P=6
Create a registration form with the following face first name takes input last name takes input
email address text input country spinner dropdown password password input confirm password
input submit button
Activitymain.xml
<?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">
<EditText
android:id="@+id/etFirstName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="First Name"
android:inputType="text"/>
<EditText
android:id="@+id/etLastName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Last Name"
android:inputType="text"/>
<EditText
android:id="@+id/etEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email Address"
android:inputType="textEmailAddress"/>
<Spinner
android:id="@+id/spinnerCountry"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"/>
<EditText
android:id="@+id/etConfirmPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Confirm Password"
android:inputType="textPassword"/>
<Button
android:id="@+id/btnSubmit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Register"/>
</LinearLayout>
Main Activity.java
package com.example.registrationform;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private EditText etFirstName, etLastName, etEmail, etPassword,
etConfirmPassword;
private Spinner spinnerCountry;
private Button btnSubmit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etFirstName = findViewById(R.id.etFirstName);
etLastName = findViewById(R.id.etLastName);
etEmail = findViewById(R.id.etEmail);
etPassword = findViewById(R.id.etPassword);
etConfirmPassword = findViewById(R.id.etConfirmPassword);
spinnerCountry = findViewById(R.id.spinnerCountry);
btnSubmit = findViewById(R.id.btnSubmit);
// Populate Spinner with country list
String[] countries = {"Select Country", "USA", "Canada", "UK",
"India", "Australia"};
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_spinner_dropdown_item, countries);
spinnerCountry.setAdapter(adapter);
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String firstName = etFirstName.getText().toString().trim();
String lastName = etLastName.getText().toString().trim();
String email = etEmail.getText().toString().trim();
String password = etPassword.getText().toString().trim();
String confirmPassword =
etConfirmPassword.getText().toString().trim();
String country = spinnerCountry.getSelectedItem().toString();
if (firstName.isEmpty() || lastName.isEmpty() ||
email.isEmpty() ||
password.isEmpty() || confirmPassword.isEmpty() ||
country.equals("Select Country")) {
Toast.makeText(MainActivity.this, "Please fill all
fields", Toast.LENGTH_SHORT).show();
} else if (!password.equals(confirmPassword)) {
Toast.makeText(MainActivity.this, "Passwords do not
match", Toast.LENGTH_SHORT).show();
} else {
String message = "Registered:\n" +
"Name: " + firstName + " " + lastName + "\n" +
"Email: " + email + "\n" +
"Country: " + country;
Toast.makeText(MainActivity.this, message,
Toast.LENGTH_LONG).show();
}
}
});
}
}
output: