Android Practical Updated
Android Practical Updated
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 22
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 22
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.
Page 3 of 22
android:text="Login"
android:textSize="18sp"
android:backgroundTint="#4CAF50"
android:textColor="#FFFFFF"/>
</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;
Page 4 of 22
@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);
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);
setContentView(R.layout.activity_dashboard);
Page 5 of 22
// Initialize the TextView
welcomeMessageTextView = findViewById(R.id.welcomeMessage);
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 22
Dr. Babasaheb Ambedkar Marathwada University,
S.B.E.S. College of Computer Science, Chh. Sambhajinagar.
Department of Computer Science & Information Technology
Practical 3
Class: B.Sc. Third Year (VI Semester) Teacher: Dr. Yogesh D. Rajendra
Subject: Android Programming Date: 03/02/2025
Aim: Create a login application where:
1. The Email ID (Username) must be in a valid format.
2. The Password must meet certain conditions (e.g., at least 6 characters).
3. The Login button will remain disabled until both the username and password are validated.
Steps:
Page 7 of 22
android:layout_height="wrap_content"
android:hint="Enter Password"
android:inputType="textPassword"
android:layout_marginBottom="20dp"
android:padding="10dp"/>
</LinearLayout>
Explanation:
LinearLayout: The root layout is a vertical LinearLayout to align all the elements in a column.
EditText (emailEditText): This is where the user will input their email (username).
EditText (passwordEditText): This is where the user will input their password.
Button (loginButton): The login button will be initially disabled (android:enabled="false") and
will be enabled when both fields are validated.
package com.example.loginvalidationapp;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Patterns;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Page 9 of 22
Explanation:
TextWatcher: We use TextWatcher for both the email and password EditText fields to listen for
changes. Whenever the text in either field changes, the validateFields() method is called to check
if the input meets the validation criteria.
validateFields(): This method checks if the email is in a valid format using
Patterns.EMAIL_ADDRESS.matcher(email).matches() and if the password has at least 6 characters.
If both conditions are satisfied, the login button is enabled; otherwise, it remains disabled.
onClickListener for loginButton: When the login button is clicked, it checks the hardcoded email
([email protected]) and password (password123). If they match, a success message is shown;
otherwise, a failure message is displayed.
1. Connect your Android device to your computer or use the Android Emulator in Android Studio.
2. Click on the Run button (green triangle) in the toolbar.
3. Test the app:
o Enter a valid email (e.g., [email protected]) and a valid password (e.g., password123) to enable
the login button and log in successfully.
o If you enter an invalid email or password, the login button will remain disabled, and a Toast message
will appear for the invalid input.
*****
Page 10 of 22
Dr. Babasaheb Ambedkar Marathwada University,
S.B.E.S. College of Computer Science, Chh. Sambhajinagar.
Department of Computer Science & Information Technology
Practical 4
Class: B.Sc. Third Year (VI Semester) Teacher: Dr. Yogesh D. Rajendra
Subject: Android Programming Date: 10/02/2025
Aim: Create a login application in Android Studio using Java, where the user must enter a valid
Email ID (Username) and Password. The login button will remain disabled until both fields are
validated. Upon successful login, the application will open a browser and load the URL
"www.sbscience.org".
Step-by-Step Guide
Explanation:
LinearLayout: The root layout is a vertical LinearLayout to align all elements in a column.
EditText (emailEditText): This is where the user will input their email (username).
EditText (passwordEditText): This is where the user will input their password.
Button (loginButton): The login button is initially disabled (android:enabled="false") and will only be enabled
when both the email and password are valid.
package com.example.loginappwithbrowser;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Patterns;
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 emailEditText, passwordEditText;
Button loginButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize UI components
emailEditText = findViewById(R.id.emailEditText);
passwordEditText = findViewById(R.id.passwordEditText);
loginButton = findViewById(R.id.loginButton);
// Add a TextWatcher for email validation
emailEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int start, int
count, int after) {}
@Override
public void onTextChanged(CharSequence charSequence, int start, int before,
int count) {
validateFields();
}
@Override
public void afterTextChanged(Editable editable) {}
});
// Add a TextWatcher for password validation
passwordEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int start, int
count, int after) {}
@Override
Page 12 of 22
public void onTextChanged(CharSequence charSequence, int start, int before,
int count) {
validateFields();
}
@Override
public void afterTextChanged(Editable editable) {}
});
// Set up the click listener for the login button
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String email = emailEditText.getText().toString();
String password = passwordEditText.getText().toString();
// Check the email and password (hardcoded validation)
if (email.equals("[email protected]") && password.equals("password123"))
{ // Successful login, open browser
openBrowser("https://www.sbscience.org");
} else { // Failed login
Toast.makeText(MainActivity.this, "Invalid Email or Password",
Toast.LENGTH_SHORT).show();
}
}
});
}
// Method to validate email and password fields
private void validateFields() {
String email = emailEditText.getText().toString().trim();
String password = passwordEditText.getText().toString().trim();
// Check if the email is valid and password is at least 6 characters long
boolean isEmailValid = Patterns.EMAIL_ADDRESS.matcher(email).matches();
boolean isPasswordValid = password.length() >= 6;
// Enable the login button only if both fields are valid
loginButton.setEnabled(isEmailValid && isPasswordValid);
}
// Method to open the browser with the specified URL
private void openBrowser(String url) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(browserIntent);
}
}Explanation:
Page 13 of 22
Step 4: Run the Application
*****
Page 14 of 22