MAD1
MAD1
MAD1
<LinearLayout
android:layout_width="409dp"
android:layout_height="729dp"
android:orientation="vertical"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="1dp"
tools:ignore="MissingConstraints">
<EditText
android:id="@+id/editTextTextPersonName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Name" />
<EditText
android:id="@+id/editTextTextPostalAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:text="Address"
android:inputType="textPostalAddress" />
<EditText
android:id="@+id/editTextPhone2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:text="Phone"
android:inputType="phone" />
<EditText
android:id="@+id/editTextTextEmailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:text="mail"
android:inputType="textEmailAddress" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Main.java
package com.example.myapplication;
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;
EditText name;
EditText add,phone,mail;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name=findViewById(R.id.editTextTextPersonName);
add=findViewById(R.id.editTextTextPostalAddress);
phone=findViewById(R.id.editTextPhone2);
mail=findViewById(R.id.editTextTextEmailAddress);
btn=findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Data is Submitted",
Toast.LENGTH_LONG).show();
}
});
}
}
ScreenShot
2. Write a program to change text of the button on the click of
button.
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">
<LinearLayout
android:layout_width="409dp"
android:layout_height="729dp"
android:orientation="vertical"
+*-
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="1dp"
tools:ignore="MissingConstraints">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Main.java
package com.example.app;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
Button bt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt=(Button) findViewById(R.id.button);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bt.setText("changed");
}
});
}
}
Screenshots
3. Write a program which changes the color of the text given by user.
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="409dp"
android:layout_height="729dp"
android:orientation="vertical"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="1dp"
tools:ignore="MissingConstraints">
<EditText
android:id="@+id/editTextTextPersonName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Name" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Main.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
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 {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tv1.setTextColor(Color.RED);
}
});
}
}
Screenshot
4. Write a program to add, multiply, division, subtraction of two
numbers
Main.XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="409dp"
android:layout_height="354dp"
android:orientation="vertical"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="1dp"
tools:ignore="MissingConstraints">
<EditText
android:id="@+id/editTextNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<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="Addition" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sub" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="mult" />
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="div" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="80dp"
android:text="TextView" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Main.java
package com.example.addition;
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;
EditText ed1,ed2;
Button btn,btn2,btn3,btn4;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int x = Integer.parseInt(ed1.getText().toString());
int y = Integer.parseInt(ed2.getText().toString());
int z = x/y;
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int x = Integer.parseInt(ed1.getText().toString());
int y = Integer.parseInt(ed2.getText().toString());
int z = x*y;
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int x = Integer.parseInt(ed1.getText().toString());
int y = Integer.parseInt(ed2.getText().toString());
int z = x-y;
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int x = Integer.parseInt(ed1.getText().toString());
int y = Integer.parseInt(ed2.getText().toString());
int z = x+y;
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="409dp"
android:layout_height="354dp"
android:orientation="vertical"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="1dp"
tools:ignore="MissingConstraints">
<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="Button" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Main.java
package com.example.oddeven;
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;
EditText ed1;
Button btn;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int x = Integer.parseInt(ed1.getText().toString());
if(x % 2 == 0) {
Toast.makeText(MainActivity.this, "Is Even",
Toast.LENGTH_SHORT).show();
tv.setText("Number is Even");
}
else {
Toast.makeText(MainActivity.this,"Is
Odd",Toast.LENGTH_LONG).show();
tv.setText("Number is Odd");
}
}
});
}
}
Screenshot
6. Write a program to find a greater number from given 3 numbers.
MainActivity.xml
<LinearLayout
android:layout_width="409dp"
android:layout_height="729dp"
android:orientation="vertical"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="1dp"
tools:ignore="MissingConstraints">
<EditText
android:id="@+id/editTextTextPersonName4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Name" />
<EditText
android:id="@+id/editTextTextPersonName5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Name" />
<EditText
android:id="@+id/editTextTextPersonName6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Name" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="271dp"
android:text="TextView" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.graterapp;
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;
int temp,largest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int a=Integer.parseInt(ed1.getText().toString());
int b=Integer.parseInt(ed2.getText().toString());
int c=Integer.parseInt(ed3.getText().toString());
temp=a>b?a:b;
largest=c>temp?c:temp;
tv.setText("Largest Number is:"+largest);
}
});
}
}
Screenshot
7. Create a simple calculator
Main.xml
<LinearLayout
android:layout_width="408dp"
android:layout_height="889dp"
android:orientation="vertical"
tools:layout_editor_absoluteX="2dp"
tools:layout_editor_absoluteY="1dp"
tools:ignore="MissingConstraints">
<EditText
android:id="@+id/editTextTextPersonName9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:minHeight="48dp"
android:text=""
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteY="100dp" />
<EditText
android:id="@+id/editTextTextPersonName8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:minHeight="48dp"
android:text=""
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteY="55dp" />
<TextView
android:id="@+id/textView8"
android:layout_width="205dp"
android:layout_height="40dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="155dp"
tools:ignore="MissingConstraints" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="/"
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="7dp"
tools:layout_editor_absoluteY="320dp" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="X2"
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="103dp"
tools:layout_editor_absoluteY="320dp" />
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="="
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="197dp"
tools:layout_editor_absoluteY="320dp" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="*"
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="197dp"
tools:layout_editor_absoluteY="250dp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="9dp"
tools:layout_editor_absoluteY="250dp" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="103dp"
tools:layout_editor_absoluteY="250dp" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Main.java
package com.example.calculator1app;
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;
Button b1,b2,b3,b4,b5,b6;
EditText e1,e2;
TextView t1;
int num1,num2;
e2 = (EditText) findViewById(R.id.editTextTextPersonName9);
t1 = (TextView) findViewById(R.id.textView8);
String s1 = e1.getText().toString();
String s2 = e2.getText().toString();
if(s1.equals("Please enter value 1") && s2.equals(null))
{
String result = "Please enter value 2";
e2.setText(result);
return false;
}
if(s1.equals(null) && s2.equals("Please enter value 2"))
{
String result = "Please enter value 1";
e1.setText(result);
return false;
}
if(s1.equals("Please enter value 1") || s2.equals("Please enter value 2"))
{
return false;
}
e2.setText(result);
return false;
}
if((s1.equals(null) && !s2.equals(null))|| (s1.equals("") && !s2.equals("")) ){
else {
num1 = Integer.parseInt(s1);
num2 = Integer.parseInt(s2);
return true;
}
if (getNumbers()) {
int sum = num1 + num2;
t1.setText(Integer.toString(sum));
}
else
{
t1.setText("Error Please enter Required Values");
}
e1.getText().clear();
}
public void clearTextNum2(View v) {
e2.getText().clear();
}
if (getNumbers()) {
double sum = Math.pow(num1, num2);
t1.setText(Double.toString(sum));
}
else
{
t1.setText("Error Please enter Required Values");
}
}
if (getNumbers()) {
int sum = num1 - num2;
t1.setText(Integer.toString(sum));
}
else
{
t1.setText("Error Please enter Required Values");
}
}
if (getNumbers()) {
int sum = num1 * num2;
t1.setText(Integer.toString(sum));
}
else
{
t1.setText("Error Please enter Required Values");
}
}
if (getNumbers()) {
if (getNumbers()) {
double sum = num1 % num2;
t1.setText(Double.toString(sum));
}
else
{
t1.setText("Error Please enter Required Values");
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button) findViewById(R.id.button);
b2=(Button) findViewById(R.id.button2);
b3=(Button) findViewById(R.id.button3);
b4=(Button) findViewById(R.id.button4);
b5=(Button) findViewById(R.id.button5);
b6=(Button) findViewById(R.id.button6);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
doSum();
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
doSub();
}
});
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
doPow();
}
});
b4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
doMul();
}
});
b5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
doDiv();
}
});
b6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
doMod();
}
});
}
}
screenshot
8. Write a program to find out given number is prime or not.
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="409dp"
android:layout_height="729dp"
android:orientation="vertical"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="1dp">
<EditText
android:id="@+id/editTextTextPersonName3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="75dp"
android:text="TextView" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Main.java
package com.example.gridapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn=(Button) findViewById(R.id.button);
EditText e=(EditText) findViewById(R.id.editTextTextPersonName3);
TextView t=(TextView) findViewById(R.id.textView);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int n=Integer.parseInt(e.getText().toString());
if (!flag)
t.setText(n + " is a prime number.");
else
t.setText(n + " is not a prime number."); }
});
}
}
Screenshot
9. Write a programme to display simple listview.
Main.xml
<LinearLayout
android:layout_width="409dp"
android:layout_height="729dp"
android:orientation="vertical"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="1dp"
tools:ignore="MissingConstraints">
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Main.java
package com.example.listapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
ListView lv;
String sub[]={"Mad","SPM","PHP","Java"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=(ListView) findViewById(R.id.list);
ArrayAdapter<String> aa;
String ps =aa.getItem(i);
Toast.makeText(getApplicationContext(), ps,
Toast.LENGTH_LONG).show();
}
});
}
}
screenshot