0% found this document useful (0 votes)
3 views38 pages

Android Assignment 2

The document outlines a series of assignments focused on Android app development, including installation of Android Studio and creating basic applications like 'Hello Universe', a login activity, and a gallery app. Each assignment includes code snippets for MainActivity.java and XML layout files, demonstrating various functionalities such as user input handling and activity lifecycle. The document serves as a practical guide for beginners to learn Android programming through hands-on projects.

Uploaded by

Somosree Dey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views38 pages

Android Assignment 2

The document outlines a series of assignments focused on Android app development, including installation of Android Studio and creating basic applications like 'Hello Universe', a login activity, and a gallery app. Each assignment includes code snippets for MainActivity.java and XML layout files, demonstrating various functionalities such as user input handling and activity lifecycle. The document serves as a practical guide for beginners to learn Android programming through hands-on projects.

Uploaded by

Somosree Dey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Assignment 1

a. Installation of Android Studio.


b. Create an Android app to run the first mobile app of the “Hello Universe” application.
c. Run the app on mobile , tab and AVD.

Step 1: Downloading Android Studio for Windows

Download android studio from official website

Step 2: Start the Installer

Step 2: Choose Components

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:

Step 3: Choose the Installation Location and click next

Step 4: Choose the Start Menu Folder and Install

Click install

Step 5: Complete the Installation

Click Finish

Step 6: Import Android Studio Settings

Select Do not import settings

Step 7: Choose Settings

Select standard and click on next

Step 8: Accept License Agreement

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;

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;
});
}
}
Assignment 2
Android activity life cycle: Design and develop an application to demonstrate the android activity life cycle.
Also demonstrate the whole process through proper diagram.

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;

public class MainActivity extends AppCompatActivity {

Button Submit,Reset;

EditText Email,Password;

TextView Result;

@Override

protected void onCreate(Bundle savedInstanceState) {

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

public void onClick(View v) {

String email=Email.getText().toString();

String password=Password.getText().toString();

Result.setText("Email Id:"+email+"\nPassword: "+password);

});

Reset.setOnClickListener(new View.OnClickListener() {
@Override

public void onClick(View v) {

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;

public class MainActivity extends AppCompatActivity {


private EditText emailEditText;
private EditText passwordEditText;
private TextView displayTextView;

@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;

public class MainActivity extends AppCompatActivity {


ImageView img;
Button sinchan,jerry,tom;
TextView ctTom,ctJerry,ctShinchan;
private int cT=0;
private int cJ=0;
private int cS=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

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;

public class MainActivity extends AppCompatActivity {


private EditText editText1, editText2;
private TextView resultTextView;
private Button addButton, subtractButton, multiplyButton, divideButton;

@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);

// Set listeners for buttons


addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
performOperation('+');
}
});

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('/');
}
});
}

private void performOperation(char operator) {


String input1 = editText1.getText().toString();
String input2 = editText2.getText().toString();

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;
}

resultTextView.setText("Result = " + result);


} catch (NumberFormatException e) {
Toast.makeText(this, "Invalid input", Toast.LENGTH_SHORT).show();
}
}
}

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;

public class MainActivity extends AppCompatActivity {

private EditText username, password, address, age, dob;


private RadioGroup gender;
private Spinner state;
private Button submitButton, resetButton;
private TextView output;

@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);

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,


R.array.states_array, (android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
state.setAdapter(adapter);

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);

DatePickerDialog datePickerDialog = new DatePickerDialog(MainActivity.this,


(view, selectedYear, selectedMonth, selectedDay) -> {
dob.setText(selectedDay + "/" + (selectedMonth + 1) + "/" + selectedYear);
}, year, month, day);
datePickerDialog.show();
});

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();

output.setText("User Name: " + userName + "\n" +


"Password: " + userPassword + "\n" +
"Address: " + userAddress + "\n" +
"Gender: " + userGender + "\n" +
"Age: " + userAge + "\n" +
"Date of Birth: " + userDob + "\n" +
"State: " + userState);
});

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;

public class MainActivity extends AppCompatActivity {

private EditText numberInput;


private Button checkButton;
private TextView resultText;

@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.");
}
}
});
}

private boolean isPrime(int num) {


if (num <= 1) return false;
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
}

● 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:hint="Enter the first number"

android:inputType="number" />

<EditText

android:id="@+id/secondNumberInput"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="Enter the second number"

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;

public class MainActivity extends AppCompatActivity {

private EditText firstNumberInput;

private EditText secondNumberInput;

private Button checkButton;

private TextView resultText;

@Override

protected void onCreate(Bundle savedInstanceState) {

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

public void onClick(View view) {

String firstInput = firstNumberInput.getText().toString();

String secondInput = secondNumberInput.getText().toString();

if (!firstInput.isEmpty() && !secondInput.isEmpty()) {

boolean isFirstPalindrome = isPalindrome(firstInput);

boolean isSecondPalindrome = isPalindrome(secondInput);

resultText.setText("First Number: " + firstInput + " is " + (isFirstPalindrome ? "a palindrome." : "not a palindrome.") +

"\nSecond Number: " + secondInput + " is " + (isSecondPalindrome ? "a palindrome." : "not a palindrome."));

} else {

resultText.setText("Please enter valid numbers.");

});

private boolean isPalindrome(String number) {

String reversed = new StringBuilder(number).reverse().toString();

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;

public class MainActivity extends AppCompatActivity {

private EditText numberInput;


private Button calculateButton;
private TextView resultText;

@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.");
}
}
});
}

private int calculateSumOfDigits(String number) {


int sum = 0;
for (char digit : number.toCharArray()) {
sum += Character.getNumericValue(digit);
}
return sum;
}}
Assignment 8
Write an android app to develop a calculator with basic operations.
● 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;

public class MainActivity extends AppCompatActivity {


private EditText editText1, editText2;
private TextView resultTextView;
private Button addButton, subtractButton, multiplyButton, divideButton;

@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);

// Set listeners for buttons


addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
performOperation('+');
}
});

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('/');
}
});
}

private void performOperation(char operator) {


String input1 = editText1.getText().toString();
String input2 = editText2.getText().toString();

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;
}

resultTextView.setText("Result = " + result);


} catch (NumberFormatException e) {
Toast.makeText(this, "Invalid input", Toast.LENGTH_SHORT).show();
}
}
}
● Output: -

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;

public class MainActivity extends AppCompatActivity {


TextView tvProfileDetails, tvProfileName, tvProfileEmail, tvProfilePhone, tvProfileAddress;
Button btnLogout;

@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);

com.example.registrationapp.User user = REGISTRATION.registeredUser;


if (user != null) {
tvProfileDetails.setText("Welcome to " +user.getName() + "r village...");
tvProfileName.setText("Name: " + user.getName());
tvProfileEmail.setText("Email: " + user.getEmail());
tvProfilePhone.setText("Phone: " + user.getPhone());
tvProfileAddress.setText("Address: " + user.getAddress());
}

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;

public class LOGIN extends AppCompatActivity {


EditText etLoginEmail, etLoginPassword;
Button btnLogin, btnSignUp;

@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;

public class REGISTRATION extends AppCompatActivity {


EditText etName, etEmail, etPassword, etPhone, etAddress;
Button btnRegister, btnToLogin;
static com.example.registrationapp.User registeredUser;

@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();

if (!name.isEmpty() && !email.isEmpty() && !password.isEmpty()) {


registeredUser = new User(name, email, password, phone, address);
Toast.makeText(REGISTRATION.this, "Registration Successful!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(REGISTRATION.this, "Please fill all fields", Toast.LENGTH_SHORT).show();
}
}
});

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);

// Apply window insets for edge-to-edge experience


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;
});

// Set button click listener


button_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name_first = txt_name.getText().toString();
String email_first = txt_email.getText().toString();
String phone_first = txt_phone.getText().toString();
String password_first = txt_password.getText().toString();

Toast.makeText(MainActivity.this, "Kochi is Loading....", Toast.LENGTH_SHORT).show();

new Handler().postDelayed(new Runnable() {


@Override
public void run() {
Intent intent = new Intent(MainActivity.this, display.class);
intent.putExtra("name_intent", name_first);
intent.putExtra("email_intent", email_first);
intent.putExtra("phone_intent", phone_first);
intent.putExtra("password_intent", password_first);
startActivity(intent);
}
}, 2000);
}
});
}
}
● activity_display.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="25dp"
android:layout_height="match_parent"
tools:context=".display">

<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;

public class display extends AppCompatActivity {

private TextView txt_name, txt_email, txt_password, txt_phone;


private Button button_back;

@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);

// Apply window insets for edge-to-edge experience


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;
});

// Get the intent data


String name_second = getIntent().getStringExtra("name_intent");
String email_second = getIntent().getStringExtra("email_intent");
String phone_second = getIntent().getStringExtra("phone_intent");
String password_second = getIntent().getStringExtra("password_intent");

// Set the text views with the received data


txt_name.setText("Name: " + name_second);
txt_email.setText("Email: " + email_second);
txt_phone.setText("Phone: " + phone_second);
txt_password.setText("Password: " + password_second);

// Set the button click listener


button_back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Clear text views
txt_name.setText("Name:");
txt_email.setText("Email:");
txt_phone.setText("Phone:");
txt_password.setText("Password:");

// Navigate back to MainActivity


Intent intent = new Intent(display.this, MainActivity.class);
startActivity(intent);
finish();
}
});
}
}

● Output: -

You might also like