0% found this document useful (0 votes)
40 views28 pages

Mob App Assignment

assignment about building basic android application

Uploaded by

Akash Yadav
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)
40 views28 pages

Mob App Assignment

assignment about building basic android application

Uploaded by

Akash Yadav
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/ 28

Q.

1 Develop Android Application for demonstrating

Android Activity Lifecycle Step 1: - Add and Override


onStart, onResume, onPause, onStop, onRestrat,
onDestroy Methods in ActivityMain.java file.

Xml-

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


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

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Java-
package com.example.lifecycle;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("Lifecycle","onCreate Called");
}

@Override
protected void onStart(){
super.onStart();
Log.d("Lifecycle", "onStart Called");
}

@Override
protected void onResume(){
super.onResume();
Log.d("Lifecycle", "onResume Called");
}

@Override
protected void onPause(){
super.onPause();
Log.d("Lifecycle", "onPause Called");
}

@Override
protected void onStop(){
super.onStop();
Log.d("Lifecycle", "onResume Called");
}

@Override
protected void onDestroy(){
super.onDestroy();
Log.d("Lifecycle", "onDestroy Called");
}
}

Q2. Develop an application to find the area and

perimeter of:
i) Square with –

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:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Area and Perimeter of Square"
android:textAlignment="center"
android:textSize="24sp" />

<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enter Side:"
android:textSize="16sp" />

<EditText
android:id="@+id/editTextText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" />

<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enter" />

<EditText
android:id="@+id/editTextText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" />

<EditText
android:id="@+id/editTextText4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" />
</LinearLayout>

Java-
package com.example.sqaurew;

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 {

EditText num1;
Button b1;
TextView result, result2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

num1 =findViewById(R.id.editTextText);
b1 = findViewById(R.id.button);
result = findViewById(R.id.editTextText3);
result2 = findViewById(R.id.editTextText4);

b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Calculate();
}
});
}
private void Calculate(){
int x=Integer.parseInt(num1.getText().toString());
int area= x*x;
int perimeter=4*x;
String sol ="Area of Square:" +area;
String sol2 = "Perimeter of Square:" +perimeter;
result.setText(sol);
result2.setText(sol2);
}
}
square without listener-

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:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Area of Square"
android:textAlignment="center"
android:textSize="24sp" />

<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enter Side:"
android:textSize="16sp" />

<EditText
android:id="@+id/editTextText4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" />

<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="Area"
android:text="Enter" />

<EditText
android:id="@+id/editTextText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" />

<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Perimeter of Square"
android:textAlignment="center"
android:textSize="24sp" />

<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enter Side:" />
<EditText
android:id="@+id/editTextText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" />

<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="Perimeter"
android:text="Enter" />

<EditText
android:id="@+id/editTextText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" />
</LinearLayout>

Java-
package com.example.square;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {

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

public void Area(View view)


{
int side,area;
EditText t1=(EditText) findViewById(R.id.editTextText4);

side=Integer.parseInt(t1.getText().toString());
area=side*side;

TextView tv1= (TextView) findViewById(R.id.editTextText2);


tv1.setText("Area of Square: " + area);
}

public void Perimeter(View view)


{
int side2,perimeter;
EditText t2=(EditText) findViewById(R.id.editTextText3);
side2 = Integer.parseInt(t2.getText().toString());
perimeter = 4 * side2;

TextView tv2 =(TextView) findViewById(R.id.editTextText);


tv2.setText("Perimeter of Square: " + perimeter);
}
}

ii) Rectangle with-

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:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Area and Perimeter of Rectangle"
android:textAlignment="center"
android:textSize="24sp" />

<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Lenght:"
android:textSize="16sp" />

<EditText
android:id="@+id/editTextNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />

<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Breadth:"
android:textSize="16sp" />

<EditText
android:id="@+id/editTextNumber2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />

<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enter" />

<EditText
android:id="@+id/editTextText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" />

<EditText
android:id="@+id/editTextText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" />
</LinearLayout>

Java-
package com.example.rectanglew;

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 {

EditText num1,num2;
Button b1;
TextView result,result2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

num1=findViewById(R.id.editTextNumber);
num2=findViewById(R.id.editTextNumber2);
b1=findViewById(R.id.button);
result=findViewById(R.id.editTextText);
result2=findViewById(R.id.editTextText2);

b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Calculate();
}
});
}
private void Calculate()
{
int a=Integer.parseInt(num1.getText().toString());
int b=Integer.parseInt(num2.getText().toString());
int area=a*b;
int perimeter=2*(a*b);
String sol="Area of Rectangle:"+area;
String sol2="Perimeter of Rectangle:"+perimeter;
result.setText(sol);
result2.setText(sol2);
}
}

Rectangle without listener-

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:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Area of Rectangle"
android:textAlignment="center"
android:textSize="24sp" />

<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enter Lenght:"
android:textSize="16sp" />

<EditText
android:id="@+id/editTextText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" />

<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enter Breadth:"
android:textSize="16sp" />

<EditText
android:id="@+id/editTextText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" />

<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="Area"
android:text="Enter" />

<EditText
android:id="@+id/editTextText4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" />

<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Perimeter of Rectangle"
android:textAlignment="center"
android:textSize="24sp" />

<TextView
android:id="@+id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enter Lenght:"
android:textSize="16sp" />

<EditText
android:id="@+id/editTextText5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" />

<TextView
android:id="@+id/textView6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enter Breadth:"
android:textSize="16sp" />

<EditText
android:id="@+id/editTextText6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" />

<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="Perimeter"
android:text="Enter" />

<EditText
android:id="@+id/editTextText7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" />
</LinearLayout>

Java-
package com.example.rectangle;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

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

public void Area(View view)


{
int lenght,breadth,area;
EditText t1=(EditText) findViewById(R.id.editTextText2);
EditText t2=(EditText) findViewById(R.id.editTextText);

lenght=Integer.parseInt(t1.getText().toString());
breadth=Integer.parseInt(t2.getText().toString());

area=lenght*breadth;

TextView Tv1=(TextView) findViewById(R.id.editTextText4);


Tv1.setText("Area of Rectangle:"+area);
}

public void Perimeter(View view)


{
int l,b,perimeter;
EditText t3=(EditText) findViewById(R.id.editTextText5);
EditText t4=(EditText) findViewById(R.id.editTextText6);

l=Integer.parseInt(t3.getText().toString());
b=Integer.parseInt(t4.getText().toString());

perimeter=2*(l*b);

TextView Tv2=(TextView) findViewById(R.id.editTextText7);


Tv2.setText("Perimeter of Rectangle:"+perimeter);
}
}

iii) Circle (Area and circumference) with-

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:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Area and Circumference of Circle"
android:textAlignment="center"
android:textSize="24sp" />

<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enter Radius:"
android:textSize="16sp" />

<EditText
android:id="@+id/editTextNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />

<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enter" />

<EditText
android:id="@+id/editTextText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" />

<EditText
android:id="@+id/editTextText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" />
</LinearLayout>

Java-
package com.example.circle;

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 {

EditText num1;
Button b1;
TextView result,result2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
num1=findViewById(R.id.editTextNumber);
b1=findViewById(R.id.button);
result=findViewById(R.id.editTextText);
result2=findViewById(R.id.editTextText2);

b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Calculate();
}
});
}
private void Calculate(){
double a=Double.parseDouble(num1.getText().toString());
double area= 3.14*a*a;
double circumference= 2*3.14*a;

String sol="Area of Circle:"+area;


String sol2="circumference of circle:"+circumference;
result.setText(sol);
result2.setText(sol2);
}

Circle without listener-

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:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Area of Circle"
android:textAlignment="center"
android:textSize="24sp" />

<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enter Radius:"
android:textSize="16sp" />

<EditText
android:id="@+id/editTextText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" />

<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="Area"
android:text="Enter" />

<EditText
android:id="@+id/editTextText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" />

<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Circumference of Circle:"
android:textAlignment="center"
android:textSize="24sp" />

<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enter Radius:"
android:textSize="16sp" />

<EditText
android:id="@+id/editTextText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" />

<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="Circumference"
android:text="Enter" />

<EditText
android:id="@+id/editTextText4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" />
</LinearLayout>

Java-
package com.example.circlewi;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewTreeViewModelKt;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {

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

public void Area(View view)


{
Double rad,area;
EditText t1=(EditText) findViewById(R.id.editTextText);
rad=Double.parseDouble(t1.getText().toString());
area= 3.14*rad*rad;

TextView Tv1=(TextView)findViewById(R.id.editTextText2);
Tv1.setText("Area of Circle:"+area);
}

public void Circumference(View view)


{
Double radius,Circumference;
EditText t2=(EditText) findViewById(R.id.editTextText3);
radius=Double.parseDouble(t2.getText().toString());
Circumference= 2*3.14*radius;

TextView Tv2=(TextView) findViewById(R.id.editTextText4);


Tv2.setText("Circumference of Circle:"+Circumference);
}
}

Q.3Develop an application to perform Arithmetic

operations:
i) Addition
ii) Subtraction
iii) Multiplication
iv) Division

With listener-

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:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >

<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Arithmetic Calculator"
android:textAlignment="center"
android:textSize="34sp" />
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enter 1st num:"
android:textSize="16sp" />

<EditText
android:id="@+id/editTextText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" />

<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enter 2nd num:"
android:textSize="16sp" />

<EditText
android:id="@+id/editTextText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" />

<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add" />

<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Subtract" />

<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Multiply" />

<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Divide" />

<EditText
android:id="@+id/editTextText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" />

</LinearLayout>
Java-
package com.example.arithmetic;

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 {

EditText num1, num2;


Button add, sub, mul, div;
TextView result;

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

num1 = findViewById(R.id.editTextText);
num2 = findViewById(R.id.editTextText2);
result = findViewById(R.id.editTextText3);

add = findViewById(R.id.button);
sub = findViewById(R.id.button2);
mul = findViewById(R.id.button3);
div = findViewById(R.id.button4);

add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addition();
}
});

sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
subtraction();
}
});

mul.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
multiplication();
}
});

div.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
division();
}
});
}

public void addition() {


int x = Integer.parseInt(num1.getText().toString());
int y = Integer.parseInt(num2.getText().toString());
int sum = x + y;
result.setText("Sum of two numbers is " + sum);
}

public void subtraction() {


int x = Integer.parseInt(num1.getText().toString());
int y = Integer.parseInt(num2.getText().toString());
int sub = x - y;
result.setText("Subtraction of two numbers is " + sub);
}

public void multiplication() {


int x = Integer.parseInt(num1.getText().toString());
int y = Integer.parseInt(num2.getText().toString());
int mul = x * y;
result.setText("Multiplication of two numbers is " + mul);
}

public void division() {


int x = Integer.parseInt(num1.getText().toString());
int y = Integer.parseInt(num2.getText().toString());
if (y != 0) {
int div = x / y;
result.setText("Division of two numbers is " + div);
} else {
result.setText("Cannot divide by zero");
}
}
}

Without listener-

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:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Arithmetic Calculator"
android:textAlignment="center"
android:textSize="24sp" />

<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enter 1st num:"
android:textSize="16sp" />
<EditText
android:id="@+id/editTextText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" />

<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enter 2nd num:"
android:textSize="16sp" />

<EditText
android:id="@+id/editTextText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" />

<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="Addition"
android:text="Add" />

<Button
android:id="@+id/button5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="Subtraction"
android:text="Subtract" />

<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="Multiply"
android:text="Multiply" />

<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="Division"
android:text="Division" />

<EditText
android:id="@+id/editTextText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" />

</LinearLayout>

Java-
package com.example.arithmeticwi;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {

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

public void Addition(View view) {


int num1, num2, add;
EditText t1 = findViewById(R.id.editTextText);
EditText t2 = findViewById(R.id.editTextText2);
num1 = Integer.parseInt(t1.getText().toString());
num2 = Integer.parseInt(t2.getText().toString());

add = num1 + num2;


TextView Tv1 = findViewById(R.id.editTextText3);
Tv1.setText("Addition of two num is:" + add);
}

public void Subtraction(View view) {


int n1, n2, sub;
EditText t3 = findViewById(R.id.editTextText);
EditText t4 = findViewById(R.id.editTextText2);
n1 = Integer.parseInt(t3.getText().toString());
n2 = Integer.parseInt(t4.getText().toString());
sub = n1 - n2;

TextView Tv2 = findViewById(R.id.editTextText3);


Tv2.setText("Subtraction of two num is:" + sub);
}

public void Multiply(View view) {


int number1, number2, mul;
EditText t5 = findViewById(R.id.editTextText);
EditText t6 = findViewById(R.id.editTextText2);
number1 = Integer.parseInt(t5.getText().toString());
number2 = Integer.parseInt(t6.getText().toString());
mul = number1 * number2;

TextView Tv3 = findViewById(R.id.editTextText3);


Tv3.setText("Multiplication of two num is:" + mul);
}

public void Division(View view) {


int no1, no2, div;
EditText t7 = findViewById(R.id.editTextText);
EditText t8 = findViewById(R.id.editTextText2);
no1 = Integer.parseInt(t7.getText().toString());
no2 = Integer.parseInt(t8.getText().toString());

// Check if divisor is zero


if (no2 == 0) {
// Handle division by zero
TextView Tv4 = findViewById(R.id.editTextText3);
Tv4.setText("Cannot divide by zero");
return;
}

div = no1 / no2;

TextView Tv4 = findViewById(R.id.editTextText3);


Tv4.setText("Division of two num is:" + div);
}

Q.4Develop an application to find the Simple Interest

and Compound interest.


Use Linear Layout.

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:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Interest Calculator"
android:textAlignment="center"
android:textSize="34sp" />

<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Principal Amount-"
android:textSize="16sp" />

<EditText
android:id="@+id/editTextText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" />

<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Rate of Interest-"
android:textSize="16sp" />
<EditText
android:id="@+id/editTextText4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" />

<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Time-"
android:textSize="16sp" />

<EditText
android:id="@+id/editTextText5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" />

<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Calculate Interest" />

<EditText
android:id="@+id/editTextText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" />

<EditText
android:id="@+id/editTextText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" />
</LinearLayout>

Java-
package com.example.interestcalculator;

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 {

EditText principalAmount, RateOfInterest, Time;


Button Calculate;
TextView Result, Result2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

principalAmount = findViewById(R.id.editTextText3);
RateOfInterest = findViewById(R.id.editTextText4);
Time = findViewById(R.id.editTextText5);
Calculate = findViewById(R.id.button);
Result = findViewById(R.id.editTextText);
Result2 = findViewById(R.id.editTextText2);

Calculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CalculateInterest();
}
});
}

private void CalculateInterest(){


double principal =
Double.parseDouble(principalAmount.getText().toString());
double rate =
Double.parseDouble(RateOfInterest.getText().toString());
double time = Double.parseDouble(Time.getText().toString());

double simpleInterest = (principal * rate * time) / 100;


double compoundInterest = principal * Math.pow((1 + rate / 100),
time) - principal;

String result = "Simple Interest:" +simpleInterest;


String result2 = "Compound Interest:" +compoundInterest;
Result.setText(result);
Result2.setText(result2);
}

Q.7Develop an application to accept qualification of a

student.

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:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<CheckBox
android:id="@+id/checkBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="10th" />

<CheckBox
android:id="@+id/checkBox2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="12th" />

<CheckBox
android:id="@+id/checkBox3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="UG" />

<CheckBox
android:id="@+id/checkBox4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="PG" />

<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>

Java-
package com.example.qualification;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

CheckBox ch1, ch2, ch3, ch4;


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

Button b1=(Button) findViewById(R.id.button);

b1.setOnClickListener(buttonListener);
}

private View.OnClickListener buttonListener=(new View.OnClickListener()


{
@Override
public void onClick(View view) {
CheckBox ch1=(CheckBox) findViewById(R.id.checkBox);
CheckBox ch2=(CheckBox) findViewById(R.id.checkBox2);
CheckBox ch3=(CheckBox) findViewById(R.id.checkBox3);
CheckBox ch4=(CheckBox) findViewById(R.id.checkBox4);

StringBuffer result=new StringBuffer();

result.append("10th").append(ch1.isChecked());
result.append("12th").append(ch2.isChecked());
result.append("UG").append(ch3.isChecked());
result.append("PG").append(ch4.isChecked());

Toast.makeText(MainActivity.this,result.toString(),Toast.LENGTH_LONG).show(
);
}
});

Q.9 Develop an application to demonstrate Intent

i) visit to next screen

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/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click here"
android:onClick="onClickButton"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click the button to go to the next page."
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Java-
package com.example.intentprogram;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {

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

public void onClickButton(View view) {


Intent intent = new Intent(this, MainActivity2.class);
startActivity(intent);
}
}

2nd XML file-


<?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=".MainActivity2">

<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/image1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

ii) visit a website

iii) Pass a message to next screen

Xml-

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


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

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter your message:"
app:layout_constraintBottom_toTopOf="@+id/editText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="468dp"
android:onClick="onClickButton"
android:text="Next"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.526"
app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Java-

package com.example.messageintent;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

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

public void onClickButton(View view) {


EditText editText = findViewById(R.id.editText);
String message = editText.getText().toString();

Intent intent = new Intent(this, MainActivity2.class);


intent.putExtra("MESSAGE", message);
startActivity(intent);
}
}
2nd xml file-

<?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"
tools:context=".MainActivity2">

<TextView
android:id="@+id/messageTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="40dp" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="328dp"
android:onClick="onClickBackButton"
android:text="Back" />

</LinearLayout>

2nd java file-

package com.example.messageintent;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity2 extends AppCompatActivity {

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

// Get the message from the intent


Intent intent = getIntent();
String message = intent.getStringExtra("MESSAGE");

// Display the message in the TextView


TextView textView = findViewById(R.id.messageTextView);
textView.setText(message);
}

public void onClickBackButton() {


finish();
}
}

You might also like