Android Practical 1 & 2
Android Practical 1 & 2
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:
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:
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:
Now, let’s define the login screen layout where users will input their username and password.
</LinearLayout>
Next, let's create the layout for the next screen, which will be displayed if the login is successful.
<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>
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;
// Initialize UI components
usernameEditText = findViewById(R.id.username);
passwordEditText = findViewById(R.id.password);
loginButton = findViewById(R.id.loginButton);
Explanation:
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;
TextView welcomeMessageTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Page 5 of 20
setContentView(R.layout.activity_dashboard);
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.
<activity android:name=".DashboardActivity"></activity>
*****
Page 6 of 20