0% found this document useful (0 votes)
8 views50 pages

Android Problem Statement 2

Uploaded by

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

Android Problem Statement 2

Uploaded by

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

Ritesh Rawat 22151876 BCA 5TH SEC-D

Problem statement 01 - Write a program to create a simple


Android app that takes input from the user and displays it on
the screen.
XML Code:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<EditText
android:id="@+id/et1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Enter your text here"
android:inputType="text"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_margin="16dp"
app:layout_constraintHorizontal_bias="0.5" />

<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
app:layout_constraintTop_toBottomOf="@+id/et1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="16dp" />

<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your input will appear here"
android:textSize="18sp"
android:padding="16dp"
app:layout_constraintTop_toBottomOf="@+id/bt1"
app:layout_constraintStart_toStartOf="parent"
Ritesh Rawat 22151876 BCA 5TH SEC-D

app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="16dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

Java Code:
package com.example.simpleuuserinput;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
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 MainActivity extends AppCompatActivity {

EditText e1;
Button b1;
TextView t1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Ritesh Rawat 22151876 BCA 5TH SEC-D

e1 = findViewById(R.id.et1);
b1 = findViewById(R.id.bt1);
t1 = findViewById(R.id.tv1);

b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

String userInput = e1.getText().toString();


t1.setText(userInput);
}
});
}
}

Output:
Ritesh Rawat 22151876 BCA 5TH SEC-D

Problem Statement 02 - Write a program to print on screen


whether a number given by the user is prime or not.
XML Code:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
Ritesh Rawat 22151876 BCA 5TH SEC-D

xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<EditText
android:id="@+id/et1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:layout_marginTop="388dp"
android:hint="Enter a number"
android:inputType="number"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="52dp"
android:text="Check Prime"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/et1" />

<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
app:layout_constraintTop_toBottomOf="@+id/bt1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="16dp" />

</androidx.constraintlayout.widget.ConstraintLayout>
Ritesh Rawat 22151876 BCA 5TH SEC-D

Java Code:
package com.example.myapplication1;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
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 MainActivity extends AppCompatActivity {

EditText e1;
Button b1;
TextView t1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

e1 = findViewById(R.id.et1);
b1 = findViewById(R.id.bt1);
t1 = findViewById(R.id.tv1);

b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String input = e1.getText().toString();
if (!input.isEmpty()) {
int number = Integer.parseInt(input);
if (isPrime(number)) {
t1.setText(number + " is a prime number.");
} else {
t1.setText(number + " is not a prime number.");
}
Ritesh Rawat 22151876 BCA 5TH SEC-D

} else {
t1.setText("Please enter a valid number.");
}
}
});
}

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:
Ritesh Rawat 22151876 BCA 5TH SEC-D

Problem Statement 03 - Write a program to print the input


given by the user in uppercase.
XML Code:
Ritesh Rawat 22151876 BCA 5TH SEC-D

<?xml version="1.0" encoding="utf-8"?>


<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<EditText
android:id="@+id/et1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Enter text"
android:layout_margin="16dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:inputType="text" />

<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Convert to Uppercase"
app:layout_constraintTop_toBottomOf="@+id/et1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="16dp" />

<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
app:layout_constraintTop_toBottomOf="@+id/bt1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="16dp"
android:textSize="18sp" />

</androidx.constraintlayout.widget.ConstraintLayout>

Java Code:
Ritesh Rawat 22151876 BCA 5TH SEC-D

package com.example.touppercase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
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 MainActivity extends AppCompatActivity {

EditText e1;
Button b1;
TextView t1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

e1 = findViewById(R.id.et1);
b1 = findViewById(R.id.bt1);
t1 = findViewById(R.id.tv1);

b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String userInput = e1.getText().toString();
String upperCaseText = userInput.toUpperCase();
t1.setText(upperCaseText);
}
});
}}

Output:
Ritesh Rawat 22151876 BCA 5TH SEC-D

Problem Statement 04 - Write a program to find the total


number of vowels, consonants, and characters in a given
string.
XML Code:
Ritesh Rawat 22151876 BCA 5TH SEC-D

<?xml version="1.0" encoding="utf-8"?>


<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<EditText
android:id="@+id/et1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Enter a string"
android:layout_margin="16dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:inputType="text" />

<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Count"
app:layout_constraintTop_toBottomOf="@+id/et1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="16dp" />

<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Vowels: "
app:layout_constraintTop_toBottomOf="@+id/bt1"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="16dp"
android:layout_marginStart="16dp"
android:textSize="16sp" />

<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Consonants: "
Ritesh Rawat 22151876 BCA 5TH SEC-D

app:layout_constraintTop_toBottomOf="@+id/tv1"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="16dp"
android:layout_marginStart="16dp"
android:textSize="16sp" />

<TextView
android:id="@+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Characters: "
app:layout_constraintTop_toBottomOf="@+id/tv2"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="16dp"
android:layout_marginStart="16dp"
android:textSize="16sp" />

</androidx.constraintlayout.widget.ConstraintLayout>

Java Code:
package com.example.tocharacters;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
Ritesh Rawat 22151876 BCA 5TH SEC-D

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 {

EditText e1;
Button b1;
TextView t1, t2, t3;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

e1 = findViewById(R.id.et1);
b1 = findViewById(R.id.bt1);
t1 = findViewById(R.id.tv1);
t2 = findViewById(R.id.tv2);
t3 = findViewById(R.id.tv3);

b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {

String input = e1.getText().toString().toLowerCase();


int vowels = 0, consonants = 0, characters = input.length();

for (char c : input.toCharArray()) {


if (Character.isLetter(c)) {
if (isVowel(c)) {
vowels++;
}
else {
consonants++;
}
}
}

t1.setText("Vowels: " + vowels);


t2.setText("Consonants: " + consonants);
t3.setText("Characters: " + characters);
}
Ritesh Rawat 22151876 BCA 5TH SEC-D

});
}

boolean isVowel(char c) {
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
}
}

Output:
Ritesh Rawat 22151876 BCA 5TH SEC-D

Problem Statement 05 - Write a program to change the


background color of a TextView in an Android app.
XML Code:
Ritesh Rawat 22151876 BCA 5TH SEC-D

<?xml version="1.0" encoding="utf-8"?>


<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text View Color"
android:textSize="18sp"
android:layout_marginTop="32dp"
android:padding="16dp"
android:background="@color/design_default_color_primary"
android:textColor="@android:color/white"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />

<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Color"
app:layout_constraintTop_toBottomOf="@+id/tv1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="16dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

Java Code:
package com.example.changetextview;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
Ritesh Rawat 22151876 BCA 5TH SEC-D

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;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

TextView t1;
Button b1;
Random r1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

t1 = findViewById(R.id.tv1);
b1 = findViewById(R.id.bt1);
r1 = new Random();

b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int color = getRandomColor();
t1.setBackgroundColor(color);
}
});
}

int getRandomColor() {

int red = r1.nextInt(256);


int green = r1.nextInt(256);
int blue = r1.nextInt(256);
return Color.rgb(red, green, blue);
}
}
Ritesh Rawat 22151876 BCA 5TH SEC-D

Output:
Ritesh Rawat 22151876 BCA 5TH SEC-D

Problem Statement 06- Write a program to Set Background


Image in Android.
XML Code:
Ritesh Rawat 22151876 BCA 5TH SEC-D

<?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"
android:id="@+id/cl1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/a1">

<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Background"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

Java Code:
package com.example.changebackgroundimage;

import android.os.Bundle;
import android.view.View;
Ritesh Rawat 22151876 BCA 5TH SEC-D

import android.widget.Button;
import android.widget.RelativeLayout;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {

ConstraintLayout c1;
Button b1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

c1 = findViewById(R.id.cl1);
b1 = findViewById(R.id.bt1);

b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
c1.setBackgroundResource(R.drawable.a2);
}
});
}
}

Output:
Ritesh Rawat 22151876 BCA 5TH SEC-D

Problem Statement 07 - Write a program to change the


background color of a Component view in an Android app.
XML Code:
Ritesh Rawat 22151876 BCA 5TH SEC-D

<?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">

<View
android:id="@+id/vw1"
android:layout_width="200dp"
android:layout_height="200dp"
android:background="#FFCCBC"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintVertical_bias="0.4" />

<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Color"
app:layout_constraintTop_toBottomOf="@+id/vw1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintVertical_bias="0.6"
android:layout_marginTop="16dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

Java Code:
package com.example.changebackgroundcomponent;

import android.graphics.Color;
import android.os.Bundle;
Ritesh Rawat 22151876 BCA 5TH SEC-D

import android.view.View;
import android.widget.Button;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

View v1;
Button b1;
Random r1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

v1 = findViewById(R.id.vw1);
b1 = findViewById(R.id.bt1);

b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

int color = getRandomColor();


v1.setBackgroundColor(color);
}
});
}

int getRandomColor() {

int red = r1.nextInt(256);


int green = r1.nextInt(256);
int blue = r1.nextInt(256);
return Color.rgb(red, green, blue);
}
}
Ritesh Rawat 22151876 BCA 5TH SEC-D

Output:
Ritesh Rawat 22151876 BCA 5TH SEC-D

Problem Statement 08 - Write a program to change the


text color of a textview in an Android app.
XML Code:
Ritesh Rawat 22151876 BCA 5TH SEC-D

<?xml version="1.0" encoding="utf-8"?>


<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change text color!"
android:textSize="24sp"
android:gravity="center"
android:padding="16dp"
android:textColor="#000000"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="32dp"/>

<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Text Color"
app:layout_constraintTop_toBottomOf="@id/tv1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="16dp"/>

</androidx.constraintlayout.widget.ConstraintLayout>

Java Code:
package com.example.changetextviewcolor;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
Ritesh Rawat 22151876 BCA 5TH SEC-D

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;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

TextView t1;
Button b1;

@Override
protected void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

t1 = findViewById(R.id.tv1);
b1 = findViewById(R.id.bt1);

b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {

Random random = new Random();


int color = Color.rgb(random.nextInt(256), random.nextInt(256),
random.nextInt(256));
t1.setTextColor(color);
}
});
}
}
}

Output:
Ritesh Rawat 22151876 BCA 5TH SEC-D

Problem Statement 09 - Write a program to create a basic


calculator in order to perform basic 4 arithmetic operations
i.e. addition, division, multiplication & subtraction.
XML Code:
Ritesh Rawat 22151876 BCA 5TH SEC-D

<?xml version="1.0" encoding="utf-8"?>


<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<EditText
android:id="@+id/et1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Enter First Number"
android:inputType="numberDecimal"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="32dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp" />

<EditText
android:id="@+id/et2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Enter Second Number"
android:inputType="numberDecimal"
app:layout_constraintTop_toBottomOf="@+id/et1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="16dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp" />

<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
app:layout_constraintTop_toBottomOf="@+id/et2"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="16dp"
android:layout_marginStart="16dp" />

<Button
Ritesh Rawat 22151876 BCA 5TH SEC-D

android:id="@+id/bt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Subtract"
app:layout_constraintTop_toBottomOf="@+id/et2"
app:layout_constraintStart_toEndOf="@id/bt1"
android:layout_marginTop="16dp"
android:layout_marginStart="8dp" />

<Button
android:id="@+id/bt3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Multiply"
app:layout_constraintTop_toBottomOf="@+id/et2"
app:layout_constraintStart_toEndOf="@id/bt2"
android:layout_marginTop="16dp"
android:layout_marginStart="8dp" />

<Button
android:id="@+id/bt4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Divide"
app:layout_constraintTop_toBottomOf="@+id/et2"
app:layout_constraintStart_toEndOf="@id/bt3"
android:layout_marginTop="16dp"
android:layout_marginStart="8dp" />

<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Result: "
android:textSize="18sp"
app:layout_constraintTop_toBottomOf="@id/bt1"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="32dp"
android:layout_marginStart="16dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
Ritesh Rawat 22151876 BCA 5TH SEC-D

Java Code:
package com.example.calculator;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
Ritesh Rawat 22151876 BCA 5TH SEC-D

import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {

EditText e1 , e2;
TextView t1;
Button b1, b2, b3, b4;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

e1 = findViewById(R.id.et1);
e2 = findViewById(R.id.et2);

t1 = findViewById(R.id.tv1);
b1 = findViewById(R.id.bt1);
b2 = findViewById(R.id.bt2);
b3 = findViewById(R.id.bt3);
b4 = findViewById(R.id.bt4);

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

b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
performOperation("-");
}
});

b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
performOperation("*");
}
});

b4.setOnClickListener(new View.OnClickListener() {
Ritesh Rawat 22151876 BCA 5TH SEC-D

@Override
public void onClick(View v) {
performOperation("/");
}
});
}

void performOperation(String operation) {


String num1Str = e1.getText().toString();
String num2Str = e2.getText().toString();

if (!num1Str.isEmpty() && !num2Str.isEmpty()) {


double num1 = Double.parseDouble(num1Str);
double num2 = Double.parseDouble(num2Str);
double result = 0;

switch (operation) {
case "+":
result = num1 + num2;
break;
case "-":
result = num1 - num2;
break;
case "*":
result = num1 * num2;
break;
case "/":
if (num2 != 0) {
result = num1 / num2;
} else {
t1.setText("Cannot divide by 0");
return;
}
break;
}
t1.setText("Result: " + result);
} else {
t1.setText("Please enter both numbers");
}
}

}
Ritesh Rawat 22151876 BCA 5TH SEC-D

Output:
Ritesh Rawat 22151876 BCA 5TH SEC-D

Problem statement 10 - write a program to perform simple


validation on name and age.
 Age must be small than 100 & greater than 1
 Name should contain only alphabets and underscore
Ritesh Rawat 22151876 BCA 5TH SEC-D

 No field should be left blank.

XML Code:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<EditText
android:id="@+id/et1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Enter your name"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="32dp"
android:padding="16dp" />

<EditText
android:id="@+id/et2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Enter your age"
android:inputType="number"
app:layout_constraintTop_toBottomOf="@id/et1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="16dp"
android:padding="16dp" />

<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Validate"
app:layout_constraintTop_toBottomOf="@id/et2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="32dp"/>
Ritesh Rawat 22151876 BCA 5TH SEC-D

<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="18sp"
app:layout_constraintTop_toBottomOf="@id/bt1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="16dp"
android:padding="16dp"/>

</androidx.constraintlayout.widget.ConstraintLayout>

Java Code:
package com.example.simplevalidation;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
Ritesh Rawat 22151876 BCA 5TH SEC-D

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 {

EditText e1;
EditText e2;
Button b1;
TextView t1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

e1 = findViewById(R.id.et1);
e2 = findViewById(R.id.et2);
b1 = findViewById(R.id.bt1);
t1 = findViewById(R.id.tv1);

b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
validateInput();
}
});
}

private void validateInput() {


String name = e1.getText().toString().trim();
String ageString = e2.getText().toString().trim();

if (name.isEmpty() || ageString.isEmpty()) {
Toast.makeText(this, "No field should be left blank.",
Toast.LENGTH_SHORT).show();
return;
}

if (!name.matches("[a-zA-Z_]+")) {
Toast.makeText(this, "Name should contain only alphabets and
underscores.", Toast.LENGTH_SHORT).show();
return;
Ritesh Rawat 22151876 BCA 5TH SEC-D

int age;
try {
age = Integer.parseInt(ageString);
} catch (NumberFormatException e) {
Toast.makeText(this, "Please enter a valid age.",
Toast.LENGTH_SHORT).show();
return;
}

if (age < 1 || age >= 100) {


Toast.makeText(this, "Age must be greater than 1 and less than 100.",
Toast.LENGTH_SHORT).show();
return;
}

t1.setText("Validation Successful! Name: " + name + ", Age: " + age);


}
}

Output:
Ritesh Rawat 22151876 BCA 5TH SEC-D

Problem Statement 11 - Write a program to check whether


the sum of digits present in alphanumeric input given by
user is Armstrong or not.
Ritesh Rawat 22151876 BCA 5TH SEC-D

XML Code:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<EditText
android:id="@+id/et1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Enter alphanumeric input"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="32dp"
android:padding="16dp" />

<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check Armstrong"
app:layout_constraintTop_toBottomOf="@id/et1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="16dp"/>

<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="18sp"
app:layout_constraintTop_toBottomOf="@id/bt1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="16dp"
android:padding="16dp"/>

</androidx.constraintlayout.widget.ConstraintLayout>

Java Code:
Ritesh Rawat 22151876 BCA 5TH SEC-D

package com.example.armstrongnumber;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
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 {

EditText e1;
Button b1;
TextView t1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

e1 = findViewById(R.id.et1);
b1 = findViewById(R.id.bt1);
t1 = findViewById(R.id.tv1);

b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkArmstrong();
}
});
}

private void checkArmstrong() {


String input = e1.getText().toString().trim();
if (input.isEmpty()) {
Toast.makeText(this, "Please enter a value.",
Toast.LENGTH_SHORT).show();
return;
}

int sum = 0;
int digitCount = 0;
Ritesh Rawat 22151876 BCA 5TH SEC-D

for (char c : input.toCharArray()) {


if (Character.isDigit(c)) {
int digit = Character.getNumericValue(c);
sum += digit;
digitCount++;
}
}

if (digitCount == 0) {
t1.setText("No digits found in the input.");
return;
}

int tempSum = sum;


int armstrongSum = 0;
while (tempSum != 0) {
int digit = tempSum % 10;
armstrongSum += Math.pow(digit, digitCount);
tempSum /= 10;
}

if (armstrongSum == sum) {
t1.setText(sum + " is an Armstrong number.");
} else {
t1.setText(sum + " is not an Armstrong number.");
}
}
}

Output:
Ritesh Rawat 22151876 BCA 5TH SEC-D

Problem statement 12 - Write a program to show the


working of Activity Lifecycle.

XML Code:
Ritesh Rawat 22151876 BCA 5TH SEC-D

<?xml version="1.0" encoding="utf-8"?>


<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/tv1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Activity Lifecycle State"
android:textSize="18sp"
android:padding="16dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="32dp" />

<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Restart Activity"
app:layout_constraintTop_toBottomOf="@id/tv1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="16dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

Java Code:
package com.example.activitylifecyclemethod;

import android.os.Bundle;
Ritesh Rawat 22151876 BCA 5TH SEC-D

import android.util.Log;
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 MainActivity extends AppCompatActivity {

static final String TAG = "LifecycleDemo";


TextView t1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

t1 = findViewById(R.id.tv1);
logAndDisplay("\nonCreate");

Button restartButton = findViewById(R.id.bt1);


restartButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
recreate();
}
});
}

@Override
protected void onStart() {
super.onStart();
logAndDisplay("onStart");
}

@Override
protected void onResume() {
super.onResume();
logAndDisplay("onResume");
}

@Override
protected void onPause() {
super.onPause();
logAndDisplay("onPause");
Ritesh Rawat 22151876 BCA 5TH SEC-D

@Override
protected void onStop() {
super.onStop();
logAndDisplay("onStop");
}

@Override
protected void onDestroy() {
super.onDestroy();
logAndDisplay("onDestroy");
}

@Override
protected void onRestart() {
super.onRestart();
logAndDisplay("onRestart");
}

private void logAndDisplay(String message) {


Log.d(TAG, message);
t1.append(message + "\n");
}
}

Output:
Ritesh Rawat 22151876 BCA 5TH SEC-D

You might also like