Lab on Android Application Development Program
Lab on Android Application Development Program
S’s
Vidhyadhan Commerce college Wadel Road Dhule
Department of Computer Science
Class :- TYBCA 606:LAB ON ANDROID APPLICATION DEVELOPMENT
Student Name :-
Practical 2 - Create “Hello World” application. That will display “Hello
World” in the middle of the screen using TextView Widget in the red color.
********************************************************************************
activity_main.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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textColor="#FF0000"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
OUTPUT
S.S. and S.S’s
Vidhyadhan Commerce college Wadel Road Dhule
Department of Computer Science
Class :- TYBCA SEM-II BCA 606:LAB ON ANDROID APPLICATION DEVELOPMENT
Student Name :-
Practical 3 - Create Registration page to demonstration of Basic widgets
available in android.
******************************************************************************
activity_main.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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:textSize="18sp"
android:layout_marginTop="40dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/et_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Enter your name"
android:inputType="textPersonName"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tv_name" />
<!-- Email Field -->
<TextView
android:id="@+id/tv_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email"
android:textSize="18sp"
android:layout_marginTop="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/et_name" />
<EditText
android:id="@+id/et_email"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Enter your email"
android:inputType="textEmailAddress"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tv_email" />
<!-- Password Field -->
<TextView
android:id="@+id/tv_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password"
android:textSize="18sp"
android:layout_marginTop="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/et_email" />
<EditText
android:id="@+id/et_password"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Enter your password"
android:inputType="textPassword"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tv_password" />
<!-- Confirm Password Field -->
<TextView
android:id="@+id/tv_confirm_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Confirm Password"
android:textSize="18sp"
android:layout_marginTop="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/et_password" />
<EditText
android:id="@+id/et_confirm_password"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Confirm your password"
android:inputType="textPassword"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tv_confirm_password" />
<!-- Terms and Conditions -->
<CheckBox
android:id="@+id/cb_terms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I agree to the terms and conditions"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/et_confirm_password" />
<!-- Gender Selection -->
<RadioGroup
android:id="@+id/rg_gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/cb_terms">
<RadioButton
android:id="@+id/rb_male"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Male" />
<RadioButton
android:id="@+id/rb_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female" />
</RadioGroup>
<!-- Register Button -->
<Button
android:id="@+id/btn_register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Register"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/rg_gender"
app:layout_constraintVertical_bias="0.322" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.practical2;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
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 nameEditText, emailEditText, passwordEditText,
confirmPasswordEditText;
private CheckBox termsCheckBox;
private RadioGroup genderRadioGroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize views
nameEditText = findViewById(R.id.et_name);
emailEditText = findViewById(R.id.et_email);
passwordEditText = findViewById(R.id.et_password);
confirmPasswordEditText = findViewById(R.id.et_confirm_password);
termsCheckBox = findViewById(R.id.cb_terms);
genderRadioGroup = findViewById(R.id.rg_gender);
Button registerButton = findViewById(R.id.btn_register);
// Set up the register button click listener
registerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Get input data
String name = nameEditText.getText().toString();
String email = emailEditText.getText().toString();
String password = passwordEditText.getText().toString();
String confirmPassword = confirmPasswordEditText.getText().toString();
// Validation
if (name.isEmpty() || email.isEmpty() || password.isEmpty() ||
confirmPassword.isEmpty()) {
Toast.makeText(MainActivity.this, "Please fill in all fields",
Toast.LENGTH_SHORT).show();
} else if (!password.equals(confirmPassword)) {
Toast.makeText(MainActivity.this, "Passwords do not match",
Toast.LENGTH_SHORT).show();
} else if (!termsCheckBox.isChecked()) {
Toast.makeText(MainActivity.this, "You must agree to the terms and
conditions", Toast.LENGTH_SHORT).show();
} else {// Get selected gender
int selectedGenderId = genderRadioGroup.getCheckedRadioButtonId();
if (selectedGenderId == -1) {
Toast.makeText(MainActivity.this, "Please select your gender",
Toast.LENGTH_SHORT).show();
} else {
RadioButton selectedGenderButton = findViewById(selectedGenderId);
String gender = selectedGenderButton.getText().toString();// Registration
logic (e.g., save to database or send to server)
Toast.makeText(MainActivity.this, "Registration Successful\nName: " +
name + "\nEmail: " + email + "\nGender: " + gender, Toast.LENGTH_LONG).show();
}
}
}
});
}
}
OUTPUT
S.S. and S.S’s
Vidhyadhan Commerce college Wadel Road Dhule
Department of Computer Science
Class :- TYBCA SEM-II BCA 606:LAB ON ANDROID APPLICATION DEVELOPMENT
Student Name :-
Practical 4 - Create sample application with login module.(Check username
and password) On successful login, Change TextView “Login Successful”.
And on failing login, alert user using Toast “Login fail”.
********************************************************************************
activity_main.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/editTextUsername"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Username"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="120dp"/>
<EditText
android:id="@+id/editTextPassword"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/editTextUsername"
android:layout_marginTop="20dp"/>
<Button
android:id="@+id/buttonLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
app:layout_constraintTop_toBottomOf="@id/editTextPassword"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="30dp"/>
<TextView
android:id="@+id/textViewStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Please login"
android:textSize="18sp"
app:layout_constraintTop_toBottomOf="@id/buttonLogin"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="30dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.practical3;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private EditText editTextUsername;
private EditText editTextPassword;
private Button buttonLogin;
private TextView textViewStatus;
// Sample credentials (you can replace this with your own logic)
private String correctUsername = "user";
private String correctPassword = "password";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize UI elements
editTextUsername = findViewById(R.id.editTextUsername);
editTextPassword = findViewById(R.id.editTextPassword);
buttonLogin = findViewById(R.id.buttonLogin);
textViewStatus = findViewById(R.id.textViewStatus);
// Set up click listener for the login button
buttonLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Get username and password entered by the user
String username = editTextUsername.getText().toString();
String password = editTextPassword.getText().toString();
// Check credentials
if (username.equals(correctUsername) && password.equals(correctPassword)) {
// If login is successful
textViewStatus.setText("Login Successful");
} else {
// If login fails
textViewStatus.setText("Login Failed");
Toast.makeText(MainActivity.this, "Login Failed", Toast.
LENGTH_SHORT).show();
}
}
});
}
}
OUTPUT
S.S. and S.S’s
Vidhyadhan Commerce college Wadel Road Dhule
Department of Computer Science
Class :- TYBCA SEM-II BCA 606:LAB ON ANDROID APPLICATION DEVELOPMENT
Student Name :-
Practical 5 - Create an application for demonstration of Scroll view in
android.
********************************************************************************
Activity_main.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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
app:layout_constraintStart_toStartOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="This is a simple demonstration of ScrollView in Android."
android:textSize="16sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="Scroll down to see more content..."
android:textSize="16sp" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="Click Me" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="More text content."
android:textSize="16sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="Scroll view helps in scrolling long content!"
android:textSize="16sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="Android's ScrollView widget is great for creating scrollable layouts."
android:textSize="16sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="Here is even more text content."
android:textSize="16sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="And we are almost at the end of the scroll!"
android:textSize="16sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="More text content."
android:textSize="16sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="Scroll view helps in scrolling long content!"
android:textSize="16sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="Android's ScrollView widget is great for creating scrollable layouts."
android:textSize="16sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="Here is even more text content."
android:textSize="16sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="And we are almost at the end of the scroll!"
android:textSize="16sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="More text content."
android:textSize="16sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="Scroll view helps in scrolling long content!"
android:textSize="16sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="Android's ScrollView widget is great for creating scrollable layouts."
android:textSize="16sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="End of the content."
android:textSize="16sp" />
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.practical4;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
MainActivity.java
package com.example.practical5;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
// UI Elements
private EditText editTextUsername;
private EditText editTextPassword;
private Button buttonLogin;
private TextView textViewStatus;
// Sample credentials (replace with your own validation logic if needed)
private final String correctUsername = "android";
private final String correctPassword = "practical";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize UI elements
editTextUsername = findViewById(R.id.editTextUsername);
editTextPassword = findViewById(R.id.editTextPassword);
buttonLogin = findViewById(R.id.buttonLogin);
textViewStatus = findViewById(R.id.textViewStatus);
// Initially, the login button is disabled
buttonLogin.setEnabled(false);
// Add text change listeners for the EditText fields
editTextUsername.addTextChangedListener(validationTextWatcher);
editTextPassword.addTextChangedListener(validationTextWatcher);
// Set up the button click listener
buttonLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Get username and password entered by the user
String username = editTextUsername.getText().toString();
String password = editTextPassword.getText().toString();
// Check credentials
if (username.equals(correctUsername) && password.equals(correctPassword)) {
textViewStatus.setText("Login Successful");
Toast.makeText(MainActivity.this, "Welcome!",
Toast.LENGTH_SHORT).show();
} else {
textViewStatus.setText("Invalid Username or Password");
Toast.makeText(MainActivity.this, "Login Failed",
Toast.LENGTH_SHORT).show();
}
}
});
}
// TextWatcher to monitor changes in both the username and password fields
private final TextWatcher validationTextWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int start, int count, int
after) {
// Not needed for this use case
}
@Override
public void onTextChanged(CharSequence charSequence, int start, int before, int
count) {
// Enable the login button only if both username and password fields are not
empty
String username = editTextUsername.getText().toString();
String password = editTextPassword.getText().toString();
buttonLogin.setEnabled(username.equals(correctUsername) &&
password.equals(correctPassword));
}
@Override
public void afterTextChanged(Editable editable) {
// Not needed for this use case
}
};
}
OUTPUT
S.S. and S .S’s
Vidhyadhan Commerce college Wadel Road Dhule
Department of Computer Science
Class :- TYBCA SEM-II BCA 606:LAB ON ANDROID APPLICATION DEVELOPMENT
Student Name :-
Practical 7 - Create an application for calculator.
********************************************************************************
activity_main.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">
<TextView
android:id="@+id/resultText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:gravity="end"
android:text=""
android:textSize="36sp"
android:textColor="#000000"
android:background="#eaeaea"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:padding="16dp"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:textSize="24sp" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:textSize="24sp" />
<Button
android:id="@+id/buttonAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:textSize="24sp" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5"
android:textSize="24sp" />
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6"
android:textSize="24sp" />
<Button
android:id="@+id/buttonSubtract"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:textSize="24sp" />
<Button
android:id="@+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8"
android:textSize="24sp" />
<Button
android:id="@+id/button9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9"
android:textSize="24sp" />
<Button
android:id="@+id/buttonMultiply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="*"
android:textSize="24sp" />
<Button
android:id="@+id/buttonClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C"
android:textSize="24sp" />
<Button
android:id="@+id/buttonEquals"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="="
android:textSize="24sp" />
<Button
android:id="@+id/buttonDivide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="/"
android:textSize="24sp" />
</GridLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.practical6;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private TextView resultText;
private String currentInput = "";
private String lastInput = "";
private String operator = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
resultText = findViewById(R.id.resultText);
// Digit buttons
setDigitButtonClickListener(R.id.button0);
setDigitButtonClickListener(R.id.button1);
setDigitButtonClickListener(R.id.button2);
setDigitButtonClickListener(R.id.button3);
setDigitButtonClickListener(R.id.button4);
setDigitButtonClickListener(R.id.button5);
setDigitButtonClickListener(R.id.button6);
setDigitButtonClickListener(R.id.button7);
setDigitButtonClickListener(R.id.button8);
setDigitButtonClickListener(R.id.button9);
// Operator buttons
setOperatorButtonClickListener(R.id.buttonAdd, "+");
setOperatorButtonClickListener(R.id.buttonSubtract, "-");
setOperatorButtonClickListener(R.id.buttonMultiply, "*");
setOperatorButtonClickListener(R.id.buttonDivide, "/");
// Clear button
Button buttonClear = findViewById(R.id.buttonClear);
buttonClear.setOnClickListener(v -> {
currentInput = "";
lastInput = "";
operator = "";
resultText.setText("");
});
// Equals button
Button buttonEquals = findViewById(R.id.buttonEquals);
buttonEquals.setOnClickListener(v -> {
if (!currentInput.isEmpty() && !lastInput.isEmpty() && !operator.isEmpty()) {
double result = calculate(Double.parseDouble(lastInput),
Double.parseDouble(currentInput), operator);
resultText.setText(String.valueOf(result));
currentInput = String.valueOf(result);
lastInput = "";
operator = "";
}
});
}
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">
<Button
android:id="@+id/btnOpenSecondActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open Second Activity" />
</LinearLayout>
activity_second.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">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Received Message"
android:textSize="20sp" />
</LinearLayout>
MainActivity.java
package com.example.practical7;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnOpenSecondActivity = findViewById(R.id.btnOpenSecondActivity);
// Set click listener on the button
btnOpenSecondActivity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Create an Intent to start SecondActivity
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
// Pass data to SecondActivity
intent.putExtra("message", "Hello from MainActivity!");
// Start SecondActivity
startActivity(intent);
}
});
}
}
SecondActivity.java
package com.example.practical7;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
// Get the Intent that started this activity
Intent intent = getIntent();
// Retrieve the data passed via the Intent
String message = intent.getStringExtra("message");
// Display the message on a TextView
TextView textView = findViewById(R.id.textView);
textView.setText(message);
}
}
OUTPUT