Exercise 1: Create an Android Project and run a simple "Hello World" app on
an emulator or physical device.
Aim: To Create a new Android Project and run a simple "Hello World" app on an
emulator or physical device.
Procedure:
1. Open Android Studio software
2. From File Menu, Choose New -> New Project
3. Click Next and Choose the Empty Activity and Click Finish
4. Give helloworld as the project name
5. In the explorer window click the activity_main.xml
6. In Design view Drag and Drop the Text view control into the design layout
7. Click -> Code
8. Write the following code in the activity_main.xml file.
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
d"
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="89dp"
android:layout_height="wrap_content"
android:fontFamily="cursive"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</[Link]>
9. Click Device Manager and Create new device.
10. Then Download any available OS
11. Click Run Button
Output:
2. Design a layout with TextView, ImageView, Button, and EditText.
Aim: To Design an Android application with layout containing TextView, ImageView, Button, and
EditText and run on emulator or physical device.
Procedure:
1. Open Android Studio software
2. From File Menu, Choose New -> New Project
3. Click Next and Choose the Empty Activity and Click Finish
4. In the explorer window click the activity_main.xml
5. Click -> Code
6. Write the following code in the activity_main.xml file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp"
tools:context=".MainActivity">
<ImageView
android:layout_width="179dp"
android:layout_height="73dp"
android:layout_gravity="center"
android:src="@drawable/ic_launcher_background">
</ImageView>
<EditText
android:id="@+id/editTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
android:inputType="textPersonName" />
<EditText
android:id="@+id/editTextEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword" />
<Button
android:id="@+id/buttonRegister"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Register"
android:onClick="displyData"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ResultData">
</TextView>
</ScrollView>
</LinearLayout>
7. Click [Link] file and write the following code:
package [Link].inputexample2;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
private EditText editTextName,editTextEmail, editTextPassword;
private Button buttonRegister;
private TextView result;
String gender;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
editTextName = findViewById([Link]);
editTextEmail = findViewById([Link]);
editTextPassword = findViewById([Link]);
buttonRegister = findViewById([Link]);
result = findViewById([Link]);
[Link](new [Link]()
{
@Override
public void onClick(View v) {
displyData();
}
});
}
private void registerUser() {
String name = [Link]().toString().trim();
String email =
[Link]().toString().trim();
String password =
[Link]().toString().trim();
if ([Link]() || [Link]() ||
[Link]()) {
[Link](this, "Please fill in all fields",
Toast.LENGTH_SHORT).show();
return;
}
// Here you would typically send this data to a backend
server
// or save it locally (e.g., SharedPreferences, SQLite)
[Link](this, "Registration successful!",
Toast.LENGTH_LONG).show();
// You can then navigate to another activity or perform
other actions
}
public void displyData() {
String name = [Link]().toString();
String email = [Link]().toString();
String pass = [Link]().toString();
[Link](name + "\n" + email + "\n" + "\n" + pass
+ "\n" +
);
}
}
8. Click Device Manager and Create new device.
9. Then Download any available OS
10. Click Run Button and see new android application
Output:
Exercise 3: Design a Screen with ImageView Showing an Image Resource
Aim: To design an Android application screen with ImageView displaying an image resource
from the drawable folder.
Procedure:
1. Open Android Studio software
2. From File Menu, Choose New → New Project
3. Click Next and Choose the Empty Activity and Click Finish
4. Give imageviewapp as the project name
5. In the explorer window, click activity_main.xml
6. Click → Code
7. Write the following code in the activity_main.xml file:
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="0dp"
android:layout_height="0dp"
android:scaleType="centerCrop"
android:src="@drawable/ic_launcher_background"
android:contentDescription="Sample Image"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</[Link]>
8. Click [Link] file and write the following code:
package [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
// ImageView is displayed directly from XML layout
// Image source: @drawable/ic_launcher_background
}
}
9. Click Device Manager and Create new device (if not already created)
10. Click Run Button and test the application
Testing Steps:
1. Launch the app on emulator or device
2. Verify that the image (Android launcher background) fills the entire screen
3. Image should be clearly visible with proper scaling
Key Concepts Demonstrated:
ImageView Widget: Using ImageView to display images
Image Resources: Loading images from drawable folder using @drawable/image_name
ScaleType: Setting how images are scaled/cropped (centerCrop)
Fullscreen Display: ImageView filling the entire screen using constraints
Content Description: Adding accessibility support with contentDescription attribute
Constraint Layout: Using constraints to position views
Output:
Exercise 4: Add an Alert Dialog with OK/Cancel Buttons in Response to a Button Click
Aim: To create an Android application that displays an Alert Dialog with OK and Cancel
buttons when a button is clicked
Procedure:
1. Open Android Studio software
2. From File Menu, Choose New → New Project
3. Click Next and Choose the Empty Activity and Click Finish
4. Give dialogalert as the project name
5. In the explorer window, click activity_main.xml
6. Click → Code
7. Write the following code in the activity_main.xml file:
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btnShowDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Alert Dialog"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</[Link]>
8. Click [Link] file and write the following code:
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
private Button btnShowDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
btnShowDialog = findViewById([Link]);
[Link](new [Link]() {
@Override
public void onClick(View v) {
showAlertDialog();
}
});
}
private void showAlertDialog() {
[Link] builder = new [Link](this);
[Link]("Confirm Action");
[Link]("Are you sure you want to proceed?");
// OK button
[Link]("OK", new
[Link]() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Handle OK button click
[Link]([Link], "OK clicked",
Toast.LENGTH_SHORT).show();
[Link]();
}
});
// Cancel button
[Link]("Cancel", new
[Link]() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Handle Cancel button click
[Link]([Link], "Cancel clicked",
Toast.LENGTH_SHORT).show();
[Link]();
}
});
// Create and show the dialog
AlertDialog alertDialog = [Link]();
[Link]();
}
}
9. Click Device Manager and Create new device (if not already created)
10. Click Run Button and test the application
Testing Steps:
1. Launch the app on emulator or device
2. Verify that a button "Show Alert Dialog" appears in the center of the screen
3. Click the "Show Alert Dialog" button
4. Verify that an alert dialog appears with:
o Title: "Confirm Action"
o Message: "Are you sure you want to proceed?"
o Two buttons: "OK" and "Cancel"
5. Click the "OK" button
6. Verify that:
o The dialog closes
o A toast message "OK clicked" appears briefly
7. Click "Show Alert Dialog" again
8. Click the "Cancel" button
9. Verify that:
o The dialog closes
o A toast message "Cancel clicked" appears briefly
Key Concepts Demonstrated:
AlertDialog: Creating popup dialogs to get user confirmation
[Link]: Using the Builder pattern to construct dialogs
Positive Button: Creating an affirmative action button (OK)
Negative Button: Creating a dismissive action button (Cancel)
[Link]: Handling button clicks in dialogs
Toast Messages: Providing feedback to the user
Anonymous Inner Classes: Implementing listeners inline
Output:
Exercise 5: Implement Navigation between Two Activities Using Intents
Aim: To implement navigation between two activities using intents and pass data between
them.
Procedure
1. Open Android Studio software
2. From File Menu, Choose New → New Project
3. Click Next and Choose the Empty Activity and Click Finish
4. Give intentexample as the project name
5. In the explorer window, right-click on java/[Link] folder
6. Select New → Activity → Empty Activity
7. Name it SecondActivity and click Finish
8. Open activity_main.xml and Click → Code
9. Write the following code in the activity_main.xml file:
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity">
<EditText
android:id="@+id/etMessage"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Enter message"
android:inputType="text"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@+id/btnNavigate"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintVertical_chainStyle="packed" />
<Button
android:id="@+id/btnNavigate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go to Second Activity"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@+id/etMessage"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</[Link]>
10. Open activity_second.xml and Click → Code
11. Write the following code in the activity_second.xml file:
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".SecondActivity">
<TextView
android:id="@+id/tvReceivedMessage"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Received Message"
android:textSize="18sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@+id/btnBackToMain"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintVertical_chainStyle="packed" />
<Button
android:id="@+id/btnBackToMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@+id/tvReceivedMessage"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</[Link]>
12. Click [Link] file and write the following code:
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
private Button btnNavigate;
private EditText etMessage;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
btnNavigate = findViewById([Link]);
etMessage = findViewById([Link]);
[Link](new [Link]() {
@Override
public void onClick(View v) {
String message = [Link]().toString().trim();
if ([Link]()) {
[Link]([Link], "Please enter a
message", Toast.LENGTH_SHORT).show();
return;
}
// Create intent and pass data
Intent intent = new Intent([Link],
[Link]);
[Link]("MESSAGE", message);
startActivity(intent);
}
});
}
}
13. Click [Link] file and write the following code:
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class SecondActivity extends AppCompatActivity {
private TextView tvReceivedMessage;
private Button btnBackToMain;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_second);
tvReceivedMessage = findViewById([Link]);
btnBackToMain = findViewById([Link]);
// Get data from intent
Intent intent = getIntent();
String message = [Link]("MESSAGE");
// Display received message
[Link](message);
[Link](new [Link]() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
14. Click Device Manager and Create new device (if not already created)
15. Click Run Button and test the navigation
Testing Steps:
1. Enter a message in the EditText field on MainActivity
2. Click "Go to Second Activity" button
3. Verify that SecondActivity opens and displays the message
4. Click "Back" button
5. Verify navigation back to MainActivity
Key Concepts Demonstrated:
Intent Creation: Using Intent class to navigate between activities
Data Passing: Using putExtra() to send data and getStringExtra() to receive data
Activity Navigation: Using startActivity() and finish() methods
Input Validation: Checking if EditText is empty before navigation
Toast Messages: Providing user feedback
Minimal UI Design: Simple, functional interface