MAD1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 31

Practical Assignment 1

Given Date : 17-10-2022

1. Create a graphical layout for registration having fields Name,


Address, contact number, E mail id and submit 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">

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

public class MainActivity extends AppCompatActivity {

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;

public class MainActivity extends AppCompatActivity {

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

EditText tv1=(EditText) findViewById(R.id.editTextTextPersonName);


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

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;

public class MainActivity extends AppCompatActivity {

EditText ed1,ed2;
Button btn,btn2,btn3,btn4;
TextView tv;

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

ed1 = (EditText) findViewById(R.id.editTextNumber);


ed2 = (EditText) findViewById(R.id.editTextNumber2);
btn = (Button) findViewById(R.id.button);
btn2 = (Button) findViewById(R.id.button2);
btn3 = (Button) findViewById(R.id.button3);
btn4 = (Button) findViewById(R.id.button4);
tv = (TextView) findViewById(R.id.textView);

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;

tv.setText("The div is :" +z);


}
});

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;

tv.setText("The mult is :" +z);


}
});

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;

tv.setText("The sub is :" +z);


}
});

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;

tv.setText("The Sum is :" +z);


}
});
}
}
Screenshot
5. Write a programme to find out a given number is odd or even.

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;

public class MainActivity extends AppCompatActivity {

EditText ed1;
Button btn;
TextView tv;

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

ed1 = (EditText) findViewById(R.id.editTextNumber);


btn = (Button) findViewById(R.id.button);
tv = (TextView) findViewById(R.id.textView);

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

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

public class MainActivity extends AppCompatActivity {

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

EditText ed1=(EditText) findViewById(R.id.editTextTextPersonName4);


EditText ed2=(EditText) findViewById(R.id.editTextTextPersonName5);
EditText ed3=(EditText) findViewById(R.id.editTextTextPersonName6);
TextView tv=(TextView) findViewById(R.id.textView);
Button btn=(Button) findViewById(R.id.button2);

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

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

public class MainActivity extends AppCompatActivity {

Button b1,b2,b3,b4,b5,b6;
EditText e1,e2;
TextView t1;
int num1,num2;

public boolean getNumbers() {


e1 = (EditText) findViewById(R.id.editTextTextPersonName8);

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

if((!s1.equals(null) && s2.equals(null))|| (!s1.equals("") && s2.equals("")) ){

String result = "Please enter value 2";

e2.setText(result);
return false;
}
if((s1.equals(null) && !s2.equals(null))|| (s1.equals("") && !s2.equals("")) ){

String result = "Please enter value 1";


e1.setText(result);
return false;
}
if((s1.equals(null) && s2.equals(null))|| (s1.equals("") && s2.equals("")) ){

String result1 = "Please enter value 1";


e1.setText(result1);
String result2 = "Please enter value 2";
e2.setText(result2);
return false;
}

else {

num1 = Integer.parseInt(s1);
num2 = Integer.parseInt(s2);

return true;
}

public void doSum() {

if (getNumbers()) {
int sum = num1 + num2;
t1.setText(Integer.toString(sum));
}
else
{
t1.setText("Error Please enter Required Values");
}

public void clearTextNum1(View v) {

e1.getText().clear();
}
public void clearTextNum2(View v) {

e2.getText().clear();
}

public void doPow() {

if (getNumbers()) {
double sum = Math.pow(num1, num2);
t1.setText(Double.toString(sum));
}
else
{
t1.setText("Error Please enter Required Values");
}
}

public void doSub() {

if (getNumbers()) {
int sum = num1 - num2;
t1.setText(Integer.toString(sum));
}
else
{
t1.setText("Error Please enter Required Values");
}
}

public void doMul() {

if (getNumbers()) {
int sum = num1 * num2;
t1.setText(Integer.toString(sum));
}
else
{
t1.setText("Error Please enter Required Values");
}
}

public void doDiv() {

if (getNumbers()) {

double sum = num1 / (num2 * 1.0);


t1.setText(Double.toString(sum));
}
else
{
t1.setText("Error Please enter Required Values");
}
}

public void doMod() {

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;

public class MainActivity extends AppCompatActivity {

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

boolean flag = false;


for (int i = 2; i <= n / 2; ++i) {
// condition for nonprime number
if (n % i == 0) {
flag = true;
break;
}
}

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

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

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

public class MainActivity extends AppCompatActivity {

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;

aa= new ArrayAdapter<>(this,


androidx.appcompat.R.layout.support_simple_spinner_dropdown_item,sub);
lv.setAdapter(aa);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i,
long l) {

String ps =aa.getItem(i);
Toast.makeText(getApplicationContext(), ps,
Toast.LENGTH_LONG).show();
}
});

}
}

screenshot

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy