Android Assignment 2
Android Assignment 2
The first step in the installation wizard is to Choose Components. By default, the only component selected is
the Android Virtual Device, which is required for running and testing your Android apps on an emulator:
Click install
Click Finish
At the bottom of the screen, you’ll see a Finish button which will only be enabled if you accept the license
agreement. Once you click Finish, the installation process will be complete, and Android Studio will be ready
for use.
Main Activity.java
package com.example.hellouniverse;
import android.os.Bundle;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;});}}
Mainactivity.xml
package com.example.hellouniverse;
import android.os.Bundle;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
Main Activity.java
package com.example.loginreset;
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 com.example.loginreset.R;
Button Submit,Reset;
EditText Email,Password;
TextView Result;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Submit=findViewById(R.id.Login);
Reset=findViewById(R.id.Reset);
Email=findViewById(R.id.Email);
Password=findViewById(R.id.Password);
Result=findViewById(R.id.Result);
Submit.setOnClickListener(new View.OnClickListener() {
@Override
String email=Email.getText().toString();
String password=Password.getText().toString();
});
Reset.setOnClickListener(new View.OnClickListener() {
@Override
Email.setText("");
Password.setText("");
Result.setText("");
});
Main Activity.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">
<Button
android:id="@+id/Reset"
android:layout_width="126dp"
android:layout_height="54dp"
android:text="Reset"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.852"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.475" />
<TextView
android:id="@+id/textView"
android:layout_width="278dp"
android:layout_height="89dp"
android:text="LOGIN ACTIVITY"
android:textColor="#ff0000"
android:textSize="34sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.096" />
<EditText
android:id="@+id/Email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textEmailAddress"
android:hint=" Enter your Email"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.666"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.263" />
<TextView
android:id="@+id/textView2"
android:layout_width="89dp"
android:layout_height="48dp"
android:shadowColor="#00F6F6F6"
android:text="EMAIL: "
android:textColor="#FF5722"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.067"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.274" />
<TextView
android:id="@+id/textView3"
android:layout_width="92dp"
android:layout_height="46dp"
android:shadowColor="#00F6F6F6"
android:text="Password: "
android:textColor="#FF5722"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.072"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.356" />
<EditText
android:id="@+id/Password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter Your Password"
android:inputType="textPassword"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.666"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.357" />
<Button
android:id="@+id/Login"
android:layout_width="126dp"
android:layout_height="54dp"
android:text="Login"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.476" />
<TextView
android:id="@+id/Result"
android:layout_width="170dp"
android:layout_height="78dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.24"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.719" />
</androidx.constraintlayout.widget.ConstraintLayout>
OUTPUT:
Assignment 3
Create an application that takes the email and password from text box and shows a
hello message along with the name entered in the text box when the user clicks the
OK button.
● 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:padding="16dp">
<EditText
android:id="@+id/emailEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/passwordEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword" />
<Button
android:id="@+id/submitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:layout_marginTop="16dp"/>
<Button
android:id="@+id/resetButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset"
android:layout_marginTop="8dp"/>
<TextView
android:id="@+id/displayTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:layout_marginTop="16dp"
android:textSize="18sp"/>
</LinearLayout>
● MainActivity.java
package com.example.uservalue;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
emailEditText = findViewById(R.id.emailEditText);
passwordEditText = findViewById(R.id.passwordEditText);
displayTextView = findViewById(R.id.displayTextView);
Button submitButton = findViewById(R.id.submitButton);
Button resetButton = findViewById(R.id.resetButton);
submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = emailEditText.getText().toString();
String password = passwordEditText.getText().toString();
displayTextView.setText("Email: " + email + "\nPassword: " + password);
}
});
resetButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
emailEditText.setText("");
passwordEditText.setText("");
displayTextView.setText("");
}
});
}
}
● Output: -
Assignment -4
Create an Android app using ImageView to show a picture using Linear Layout and
try to design a basic gallery.
● activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:srcCompat="@tools:sample/avatars" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="245dp">
<Button
android:id="@+id/s"
android:layout_width="98dp"
android:layout_height="68dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="153dp"
android:layout_marginTop="15dp"
android:layout_marginEnd="159dp"
android:layout_marginBottom="162dp"
android:text="Sinchan" />
<Button
android:id="@+id/J"
android:layout_width="108dp"
android:layout_height="67dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="147dp"
android:layout_marginTop="89dp"
android:layout_marginEnd="156dp"
android:layout_marginBottom="89dp"
android:text="Jerry" />
<Button
android:id="@+id/T"
android:layout_width="108dp"
android:layout_height="62dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="145dp"
android:layout_marginTop="166dp"
android:layout_marginEnd="158dp"
android:layout_marginBottom="17dp"
android:text="Tom" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sinchan Clicked" />
<TextView
android:id="@+id/SinchanClicked"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="0" />
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Jerry" />
<TextView
android:id="@+id/JerryClicked"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="0" />
<TextView
android:id="@+id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Tom" />
<TextView
android:id="@+id/Tomclicked"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="0" />
</LinearLayout>
</LinearLayout>
● MainActivity.java
package com.example.imageview;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
package com.example.image_layout;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
setContentView(R.layout.activity_main);
img = findViewById(R.id.image);
sinchan=findViewById(R.id.s);
tom=findViewById(R.id.T);
jerry=findViewById(R.id.J);
ctJerry = findViewById(R.id.JerryClicked);
ctTom = findViewById(R.id.Tomclicked);
ctShinchan = findViewById(R.id.SinchanClicked);
sinchan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
img.setImageResource(R.drawable.shinchan);
cS ++;
ctShinchan.setText(String.valueOf(cS));
}
});
tom.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
img.setImageResource(R.drawable.tom);
cT ++;
ctTom.setText(String.valueOf(cT));
}
});
jerry.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
img.setImageResource(R.drawable.jerry);
cJ ++;
ctJerry.setText(String.valueOf(cJ));
}});}}
Assignment 5
a. Create an Android app to add and subtract two numbers supplied from the user interface having two text
boxes.
b. Create an Android app to multiply and divide two numbers supplied from the user interface having two text
boxes.
● activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FBEAEB"
android:textSize="40sp"
android:text="MiNi calculator"
android:textAlignment="center"/>
<EditText
android:id="@+id/editTextText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:ems="10"
android:inputType="numberDecimal"
android:hint="Enter first value" />
<EditText
android:id="@+id/editTextText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:ems="10"
android:inputType="numberDecimal"
android:hint="Enter second value" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:orientation="horizontal">
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="+" />
<Button
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="-" />
<Button
android:id="@+id/button4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="*" />
<Button
android:id="@+id/button5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="/" />
</LinearLayout>
<TextView
android:id="@+id/textView4"
android:textSize="40sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#2F3C7E"
android:text="Result="
android:textColor="#ff0000"
android:textStyle="bold"/>
</LinearLayout>
● MainActivity.java
package com.example.calculator;
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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize UI elements
editText1 = findViewById(R.id.editTextText);
editText2 = findViewById(R.id.editTextText2);
resultTextView = findViewById(R.id.textView4);
addButton = findViewById(R.id.button2);
subtractButton = findViewById(R.id.button3);
multiplyButton = findViewById(R.id.button4);
divideButton = findViewById(R.id.button5);
subtractButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
performOperation('-');
}
});
multiplyButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
performOperation('*');
}
});
divideButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
performOperation('/');
}
});
}
if (input1.isEmpty() || input2.isEmpty()) {
Toast.makeText(this, "Please enter both values", Toast.LENGTH_SHORT).show();
return;
}
try {
double value1 = Double.parseDouble(input1);
double value2 = Double.parseDouble(input2);
double result = 0;
switch (operator) {
case '+':
result = value1 + value2;
break;
case '-':
result = value1 - value2;
break;
case '*':
result = value1 * value2;
break;
case '/':
if (value2 != 0) {
result = value1 / value2;
result = Math.round(result * 100.0) / 100.0;
} else {
Toast.makeText(this, "Cannot divide by zero", Toast.LENGTH_SHORT).show();
return;
}
break;
}
Assignment 6
a. Create a screen that has input boxes for User Name, Password, Address, Gender (radio buttons for male and female),
Age (numeric), Date of Birth (Date Picker), State (Spinner), and a Submit button. On clicking the submit button, print all
the data below the Submit Button (use any layout).
● 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:padding="16dp">
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="User Name" />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword" />
<EditText
android:id="@+id/address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Address" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gender" />
<RadioGroup
android:id="@+id/gender"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male" />
<RadioButton
android:id="@+id/female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female" />
</RadioGroup>
<EditText
android:id="@+id/age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Age"
android:inputType="number" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date of Birth" />
<EditText
android:id="@+id/dob"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Select Date of Birth"
android:focusable="false"
android:clickable="true" />
<Spinner
android:id="@+id/state"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/submit_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit" />
<Button
android:id="@+id/reset_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Reset" />
<TextView
android:id="@+id/output"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="16dp" />
</LinearLayout>
● strings.xml
<resources>
<string name="app_name">USER VALUE</string>
<string-array name="states_array">
<item>Select State</item>
<item>WEST BENGAL</item>
<item>TAMILNADU</item>
<item>ODISHA</item>
<item>MUMBAI</item>
<item>CHENNAI</item>
</string-array>
</resources>
● MainActivity.java
package com.example.uservalue;
import android.app.DatePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Calendar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username = findViewById(R.id.username);
password = findViewById(R.id.password);
address = findViewById(R.id.address);
age = findViewById(R.id.age);
dob = findViewById(R.id.dob);
gender = findViewById(R.id.gender);
state = findViewById(R.id.state);
submitButton = findViewById(R.id.submit_button);
resetButton = findViewById(R.id.reset_button);
output = findViewById(R.id.output);
dob.setOnClickListener(v -> {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
submitButton.setOnClickListener(v -> {
String userName = username.getText().toString();
String userPassword = password.getText().toString();
String userAddress = address.getText().toString();
String userAge = age.getText().toString();
String userDob = dob.getText().toString();
String userState = state.getSelectedItem().toString();
String userGender = ((RadioButton) findViewById(gender.getCheckedRadioButtonId())).getText().toString();
resetButton.setOnClickListener(v -> {
username.setText("");
password.setText("");
address.setText("");
age.setText("");
dob.setText("");
gender.clearCheck(); // Clears the selected radio button
state.setSelection(0); // Resets the spinner to the first item
output.setText(""); // Clears the output TextView
});
}
}
b. Create an Android app that will check whether the given number supplied as an
input is prime or not.
● activity_main.xml
<?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:padding="16dp">
<EditText
android:id="@+id/numberInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter a number"
android:inputType="number" />
<Button
android:id="@+id/checkButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check Prime"
android:layout_below="@id/numberInput"
android:layout_marginTop="16dp" />
<TextView
android:id="@+id/resultText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/checkButton"
android:layout_marginTop="16dp"
android:textSize="18sp" />
</RelativeLayout>
● MainActivity.java
package com.example.primechecker;
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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
numberInput = findViewById(R.id.numberInput);
checkButton = findViewById(R.id.checkButton);
resultText = findViewById(R.id.resultText);
checkButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String input = numberInput.getText().toString();
if (!input.isEmpty()) {
int number = Integer.parseInt(input);
boolean isPrime = isPrime(number);
resultText.setText(number + " is " + (isPrime ? "a prime number." : "not a prime number."));
} else {
resultText.setText("Please enter a valid number.");
}
}
});
}
● Output: -
c. Create an Android app that will check whether given two numbers are palindrome
or not.
● activity_main.xml
<?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:padding="16dp">
<EditText
android:id="@+id/firstNumberInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number" />
<EditText
android:id="@+id/secondNumberInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/firstNumberInput"
android:layout_marginTop="16dp"
android:inputType="number" />
<Button
android:id="@+id/checkButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check Palindrome"
android:layout_below="@id/secondNumberInput"
android:layout_marginTop="16dp" />
<TextView
android:id="@+id/resultText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/checkButton"
android:layout_marginTop="16dp"
android:textSize="18sp" />
</RelativeLayout>
● MainActivity.java
package com.example.palindromechecker;
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;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firstNumberInput = findViewById(R.id.firstNumberInput);
secondNumberInput = findViewById(R.id.secondNumberInput);
checkButton = findViewById(R.id.checkButton);
resultText = findViewById(R.id.resultText);
checkButton.setOnClickListener(new View.OnClickListener() {
@Override
resultText.setText("First Number: " + firstInput + " is " + (isFirstPalindrome ? "a palindrome." : "not a palindrome.") +
"\nSecond Number: " + secondInput + " is " + (isSecondPalindrome ? "a palindrome." : "not a palindrome."));
} else {
});
return number.equals(reversed);
● Output: -
Assignment 7
a. Create an Android app to show and implement table layout.
b. Develop an Android Application that can calculate the sum of digits of a given
number.
● activity_main.xml
<?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:padding="16dp">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter a number:"
android:padding="8dp" />
<EditText
android:id="@+id/numberInput"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Number"
android:inputType="number" />
</TableRow>
<TableRow>
<Button
android:id="@+id/calculateButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculate Sum" />
</TableRow>
<TableRow>
<TextView
android:id="@+id/resultText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:textSize="18sp" />
</TableRow>
</TableLayout>
</RelativeLayout>
● MainActivity.java
package com.example.sumofdigitstablelayout;
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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
numberInput = findViewById(R.id.numberInput);
calculateButton = findViewById(R.id.calculateButton);
resultText = findViewById(R.id.resultText);
calculateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String input = numberInput.getText().toString();
if (!input.isEmpty()) {
int sum = calculateSumOfDigits(input);
resultText.setText("Sum of digits: " + sum);
} else {
resultText.setText("Please enter a valid number.");
}
}
});
}
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FBEAEB"
android:textSize="40sp"
android:text="MiNi calculator"
android:textAlignment="center"/>
<EditText
android:id="@+id/editTextText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:ems="10"
android:inputType="numberDecimal"
android:hint="Enter first value" />
<EditText
android:id="@+id/editTextText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:ems="10"
android:inputType="numberDecimal"
android:hint="Enter second value" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:orientation="horizontal">
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="+" />
<Button
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="-" />
<Button
android:id="@+id/button4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="*" />
<Button
android:id="@+id/button5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="/" />
</LinearLayout>
<TextView
android:id="@+id/textView4"
android:textSize="40sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#2F3C7E"
android:text="Result="
android:textColor="#ff0000"
android:textStyle="bold"/>
</LinearLayout>
● MainActivity.java
package com.example.calculator;
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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize UI elements
editText1 = findViewById(R.id.editTextText);
editText2 = findViewById(R.id.editTextText2);
resultTextView = findViewById(R.id.textView4);
addButton = findViewById(R.id.button2);
subtractButton = findViewById(R.id.button3);
multiplyButton = findViewById(R.id.button4);
divideButton = findViewById(R.id.button5);
subtractButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
performOperation('-');
}
});
multiplyButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
performOperation('*');
}
});
divideButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
performOperation('/');
}
});
}
if (input1.isEmpty() || input2.isEmpty()) {
Toast.makeText(this, "Please enter both values", Toast.LENGTH_SHORT).show();
return;
}
try {
double value1 = Double.parseDouble(input1);
double value2 = Double.parseDouble(input2);
double result = 0;
switch (operator) {
case '+':
result = value1 + value2;
break;
case '-':
result = value1 - value2;
break;
case '*':
result = value1 * value2;
break;
case '/':
if (value2 != 0) {
result = value1 / value2;
result = Math.round(result * 100.0) / 100.0;
} else {
Toast.makeText(this, "Cannot divide by zero", Toast.LENGTH_SHORT).show();
return;
}
break;
}
Assignment 9
Create an android app that will check whether given username and password
matches with the predefined user name and password.
● activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:layout_margin="15dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/tvProfileDetails"
android:layout_width="match_parent"
android:layout_height="35dp"
android:text=" " />
<TextView
android:id="@+id/tvProfileName"
android:layout_width="match_parent"
android:layout_height="35dp"
android:text="Name" />
<TextView
android:id="@+id/tvProfileEmail"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="Email" />
<TextView
android:id="@+id/tvProfilePhone"
android:layout_width="match_parent"
android:layout_height="33dp"
android:text="Phone" />
<TextView
android:id="@+id/tvProfileAddress"
android:layout_width="match_parent"
android:layout_height="32dp"
android:text="Address" />
<Button
android:id="@+id/btnLogout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Logout" />
</LinearLayout>
● MainActivity.java
package com.example.userform;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvProfileDetails = findViewById(R.id.tvProfileDetails);
tvProfileName = findViewById(R.id.tvProfileName);
tvProfileEmail = findViewById(R.id.tvProfileEmail);
tvProfilePhone = findViewById(R.id.tvProfilePhone);
tvProfileAddress = findViewById(R.id.tvProfileAddress);
btnLogout = findViewById(R.id.btnLogout);
btnLogout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, LOGIN.class);
startActivity(intent);
finish(); // close profile activity
}
});
}
}
● activity_login.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_margin="15dp"
tools:context=".LOGIN">
<EditText
android:id="@+id/etLoginEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email" />
<EditText
android:id="@+id/etLoginPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword" />
<Button
android:id="@+id/btnLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login" />
<Button
android:id="@+id/btnSignUp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sign Up" />
</LinearLayout>
● LOGIN.java
package com.example.userform;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
etLoginEmail = findViewById(R.id.etLoginEmail);
etLoginPassword = findViewById(R.id.etLoginPassword);
btnLogin = findViewById(R.id.btnLogin);
btnSignUp = findViewById(R.id.btnSignUp);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String email = etLoginEmail.getText().toString();
String password = etLoginPassword.getText().toString();
if (REGISTRATION.registeredUser != null) {
if (email.equals(REGISTRATION.registeredUser.getEmail()) &&
password.equals(REGISTRATION.registeredUser.getPassword())) {
Intent intent = new Intent(LOGIN.this, MainActivity.class);
startActivity(intent);
} else {
Toast.makeText(LOGIN.this, "Invalid credentials", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(LOGIN.this, "No registered user", Toast.LENGTH_SHORT).show();
}
}
});
btnSignUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(LOGIN.this, REGISTRATION.class);
startActivity(intent);
}
});
}
}
● activity_registration.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_margin="15dp"
android:layout_height="match_parent"
tools:context=".REGISTRATION">
<EditText
android:id="@+id/etName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name" />
<EditText
android:id="@+id/etEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword" />
<EditText
android:id="@+id/etPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Phone"
android:inputType="phone" />
<EditText
android:id="@+id/etAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Address" />
<Button
android:id="@+id/btnRegister"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Register" />
<Button
android:id="@+id/btnToLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Go to Login" />
</LinearLayout>
● REGISTRATION.java
package com.example.userform;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.registrationapp.User;
@SuppressLint("WrongViewCast")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration);
etName = findViewById(R.id.etName);
etEmail = findViewById(R.id.etEmail);
etPassword = findViewById(R.id.etPassword);
etPhone = findViewById(R.id.etPhone);
etAddress = findViewById(R.id.etAddress);
btnRegister = findViewById(R.id.btnRegister);
btnToLogin = findViewById(R.id.btnToLogin);
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = etName.getText().toString();
String email = etEmail.getText().toString();
String password = etPassword.getText().toString();
String phone = etPhone.getText().toString();
String address = etAddress.getText().toString();
btnToLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(REGISTRATION.this, LOGIN.class));
}
});
}
}
Output :
Assignments 10
Create an android application demonstrating Toast, Intent etc.
● activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:layout_margin="20dp"
android:background="@drawable/img_1"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/edittext_name_xml"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text"
android:textSize="25dp"
android:hint="Name" />
<EditText
android:id="@+id/edittext_email_xml"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:textSize="25dp"
android:hint="Email"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/edittext_phone_xml"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Phone"
android:textSize="25dp"
android:inputType="phone" />
<EditText
android:id="@+id/edittext_password_xml"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Password"
android:textSize="25dp"
android:inputType="textPassword" />
<Button
android:id="@+id/btn_login_xml"
android:layout_width="match_parent"
🔑
android:layout_height="wrap_content"
android:text="Login " />
</LinearLayout>
● MainActivity.java
package com.example.intentapplication;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity {
private EditText txt_name, txt_email, txt_password, txt_phone;
private Button button_login;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
// Initialize views
txt_name = findViewById(R.id.edittext_name_xml);
txt_email = findViewById(R.id.edittext_email_xml);
txt_phone = findViewById(R.id.edittext_phone_xml);
txt_password = findViewById(R.id.edittext_password_xml);
button_login = findViewById(R.id.btn_login_xml);
<TextView
android:id="@+id/textview_name_xml"
android:layout_width="match_parent"
android:layout_height="36dp"
android:textSize="25dp"
android:textStyle="bold"
android:text="Name:" />
<TextView
android:id="@+id/textview_email_xml"
android:layout_width="match_parent"
android:layout_height="36dp"
android:text="Email:"
android:textSize="25dp"
android:textStyle="bold" />
<TextView
android:id="@+id/textview_phone_xml"
android:layout_width="match_parent"
android:layout_height="36dp"
android:text="Phone:"
android:textSize="25dp"
android:textStyle="bold" />
<TextView
android:id="@+id/textview_password_xml"
android:layout_width="match_parent"
android:layout_height="36dp"
android:text="Password:"
android:textSize="25dp"
android:textStyle="bold" />
<Button
android:id="@+id/btn_back_xml"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Back" />
</LinearLayout>
● display.java
package com.example.intentapplication;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_display);
// Initialize views
txt_name = findViewById(R.id.textview_name_xml);
txt_email = findViewById(R.id.textview_email_xml);
txt_phone = findViewById(R.id.textview_phone_xml);
txt_password = findViewById(R.id.textview_password_xml);
button_back = findViewById(R.id.btn_back_xml);
● Output: -