0% found this document useful (0 votes)
8 views

Android Practical 1 & 2

The document outlines practical assignments for B.Sc. Third Year students in Android Programming, detailing the steps to create a 'Hello World' application and a login module application using Android Studio with Java. It includes prerequisites, layout design, Java activity setup, and instructions for running the applications. The document serves as a guide for students to understand basic Android development concepts and implement them in practical scenarios.

Uploaded by

kalpnamundhe7
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Android Practical 1 & 2

The document outlines practical assignments for B.Sc. Third Year students in Android Programming, detailing the steps to create a 'Hello World' application and a login module application using Android Studio with Java. It includes prerequisites, layout design, Java activity setup, and instructions for running the applications. The document serves as a guide for students to understand basic Android development concepts and implement them in practical scenarios.

Uploaded by

kalpnamundhe7
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Dr.

Babasaheb Ambedkar Marathwada University,


S.B.E.S. College of Computer Science, Chh. Sambhajinagar.
Department of Computer Science & Information Technology
Practical 1
Class: B.Sc. Third Year (VI Semester) Teacher: Dr. Yogesh D. Rajendra
Subject: Android Programming Date: 27/01/2025

Aim: To create a simple "Hello World" application in Android Studio with Java that displays "Hello World"
in red colour in the middle of the screen with a white background, follow the steps below:

Prerequisites:

 Android Studio installed on your machine.


 A working knowledge of Java programming.

Steps to create the "Hello World" application:

Step 1: Create a New Project

1. Open Android Studio.


2. Click on Start a new Android Studio project.
3. Select Empty Activity as the template and click Next.
4. Name your project as "HelloWorld" or any name you prefer.
5. Set the language to Java.
6. Select the Minimum API level. For this example, API 21 (Lollipop) is be fine as it covers most devices.
7. Click on Finish to create the project.

Step 2: Edit the activity_main.xml Layout

Once the project is created, Android Studio will automatically open the activity_main.xml file under the
res/layout folder. This is where you design the UI.

1. In the activity_main.xml file, you will define the layout. Modify the file to make the background white
and the "Hello World" text appear in red, centered on the screen.
2. Replace the default content with the following 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"
android:background="#FFFFFF">
<TextView
android:id="@+id/helloTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World"
android:textColor="#FF0000" <!-- Red text colour -->
android:textSize="30sp"
android:layout_centerInParent="true" /> <!-- Center text in parent -->
</RelativeLayout>
Explanation:
 RelativeLayout: We use RelativeLayout to position elements relative to each other. Here, we use it
to center the TextView.
 TextView: The TextView displays the "Hello World" text.
o android:textColor="#FF0000" sets the text colour to red.
o android:textSize="30sp" sets the font size to 30 scale-independent pixels.
o android:layout_centerInParent="true" ensures that the TextView is centered both
horizontally and vertically in the parent layout.

Page 1 of 20
Step 3: Update the Java Activity File
Next, you need to ensure that the Java activity is properly set up. This is where the behavior of the app is
defined.
1. Open the MainActivity.java file located in the java folder under the package name (e.g.,
com.example.helloworld).
2. Replace the existing code with the following:
package com.example.helloworld;
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); // Set the layout defined in
activity_main.xml
}
}
Explanation:
 onCreate(Bundle savedInstanceState): This is where the app starts. When the app is launched, the
onCreate() method is called.
 setContentView(R.layout.activity_main): This tells the app to use the layout defined in
activity_main.xml for the UI.
Step 4: Run the Application
Now that the layout and the activity are set up, it’s time to run the app:
1. Connect an Android device to your computer or use the Android Emulator in Android Studio.
2. Click on the Run button (green triangle) in the toolbar at the top of Android Studio.
3. Select the connected device or emulator to run the app.
Step 5: See the Output
Once the app starts, you should see:
 The background is white.
 The text "Hello World" is displayed in the center of the screen in red.
Output:

*****
Page 2 of 20
Dr. Babasaheb Ambedkar Marathwada University,
S.B.E.S. College of Computer Science, Chh. Sambhajinagar.
Department of Computer Science & Information Technology
Practical 2
Class: B.Sc. Third Year (VI Semester) Teacher: Dr. Yogesh D. Rajendra
Subject: Android Programming Date: 31/01/2025
Aim: To understand Activity, Intent Create sample application with login module. (Check username and
password), on successful login, go to next screen. And on failing login, alert user using Toast. Also pass
username and password to next.

Steps:

Step 1: Create a New Project in Android Studio

1. Open Android Studio.


2. Click on Start a new Android Studio project.
3. Choose the Empty Activity template and click Next.
4. Set the Name of the app as LoginApp (or any name you prefer).
5. Set the Language to Java.
6. Choose the Minimum API level (API 21: Lollipop is fine for this example).
7. Click Finish.

Step 2: Create the Login Layout (activity_main.xml)

Now, let’s define the login screen layout where users will input their username and password.

1. Navigate to the res/layout/activity_main.xml file.


2. Replace the existing code with the following:

<?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="30dp">

<!-- Username input field -->


<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Username"
android:inputType="text"
android:padding="10dp"
android:layout_marginBottom="20dp"/>

<!-- Password input field -->


<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Password"
android:inputType="textPassword"
android:padding="10dp"
android:layout_marginBottom="20dp"/>

<!-- Login Button -->


<Button
android:id="@+id/loginButton"
android:layout_width="match_parent"
Page 3 of 20
android:layout_height="wrap_content"
android:text="Login"
android:textSize="18sp"
android:backgroundTint="#4CAF50"
android:textColor="#FFFFFF"/>

</LinearLayout>

Explanation of the Layout:

 LinearLayout: This layout is used to align all elements vertically.


 EditText (username): The field where the user will input the username.
 EditText (password): The field where the user will input the password.
 Button (loginButton): The button to trigger the login action.

Step 3: Create the Next Activity Layout (activity_dashboard.xml)

Next, let's create the layout for the next screen, which will be displayed if the login is successful.

1. Right-click on res/layout and select New → Layout Resource File.


2. Name the file activity_dashboard.xml.
3. Set the root element to LinearLayout.
4. Replace the code with the following:

<?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="30dp"
android:gravity="center">

<TextView
android:id="@+id/welcomeMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome!"
android:textSize="24sp"
android:textColor="#000000"/>

</LinearLayout>

This layout simply contains a TextView to display a welcome message.

Step 4: Implementing the Login Logic in MainActivity.java

1. Open the MainActivity.java file under the java/com.example.loginapp directory.


2. Replace the existing code with the following code:

package com.example.loginapp;

import android.content.Intent;
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 {

EditText usernameEditText, passwordEditText;


Button loginButton;
Page 4 of 20
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Initialize UI components
usernameEditText = findViewById(R.id.username);
passwordEditText = findViewById(R.id.password);
loginButton = findViewById(R.id.loginButton);

// Set click listener for the login button


loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Get the entered username and password
String username = usernameEditText.getText().toString();
String password = passwordEditText.getText().toString();
// Check if username and password are correct
if (username.equals("admin") && password.equals("1234")) {
// Create an Intent to go to the next screen (Dashboard)
Intent intent = new Intent(MainActivity.this, DashboardActivity.class);
intent.putExtra("username", username); // Pass username to the next activity
intent.putExtra("password", password); // Pass password to the next activity
startActivity(intent);
} else {
// Show a Toast message if login fails
Toast.makeText(MainActivity.this, "Invalid Credentials", Toast.LENGTH_SHORT).show();
}
}
});
}
}

Explanation:

 EditText usernameEditText: For getting the username input.


 EditText passwordEditText: For getting the password input.
 Button loginButton: The button the user clicks to initiate login.
 OnClickListener: When the button is clicked, it checks if the username is "admin" and the password
is "1234". If the credentials match, it uses an Intent to navigate to the DashboardActivity, passing
the username and password. Otherwise, a Toast is shown with an error message.

Step 5: Implement the DashboardActivity.java

Now, let's create the DashboardActivity that will display the username and password passed from the
previous activity.

1. Right-click on the java folder and create a new Java class called DashboardActivity.
2. Implement the code as shown below:

package com.example.loginapp;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class DashboardActivity extends AppCompatActivity {

TextView welcomeMessageTextView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Page 5 of 20
setContentView(R.layout.activity_dashboard);

// Initialize the TextView


welcomeMessageTextView = findViewById(R.id.welcomeMessage);

// Get the username and password from the Intent


Intent intent = getIntent();
String username = intent.getStringExtra("username");
String password = intent.getStringExtra("password");

// Display the welcome message along with the username


welcomeMessageTextView.setText("Welcome, " + username + "!\nPass: " + password);
}
}

Explanation:

 Intent: The getIntent() method retrieves the Intent that was used to start this activity.
 getStringExtra(): This retrieves the username and password that were passed from the MainActivity.
 The username and password are then displayed in the TextView.

Step 6: Add DashboardActivity to AndroidManifest.xml

1. Open the AndroidManifest.xml file.


2. Add the new DashboardActivity inside the <application> tag as shown below:

<activity android:name=".DashboardActivity"></activity>

Step 7: Run the Application

1. Connect your device or start an emulator.


2. Click the Run button in Android Studio to deploy the app.
3. On the login screen:
o Enter username: admin and password: 1234 for a successful login. This will take you to the next
screen where the username and password are displayed.
o Enter any incorrect username or password to see the Toast message: "Invalid username or
password".

*****
Page 6 of 20

You might also like