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

EX.

NO:1
GREATEST AMONG THREE NUMBERS
DATE:

Aim:
To develop an Android application to find greatest among three numbers using
GUI Components.

Algorithm:
Step 1: Start.
Step 2: Open a new Android Project from File Menu.
Step 3: Choose Empty Activity Window to design your Application.
Step 4: Design the application in Design Editor Window with required tools.
Step 5: Open MainActivity.java file and type the code inside the class to find
greatest among three numbers.
Step 6: Get three numbers from user using GUI Components.
Step 7: Run the Application.
Step 8: Stop.
Program Code
activity_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">

<TextView
android:id="@+id/textView"
android:layout_width="265dp"
android:layout_height="42dp"
android:text="Greatest among 3 nos"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="48dp"
tools:ignore="MissingConstraints" />

<EditText
android:id="@+id/t1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:ems="10"
android:inputType="number"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />

<EditText
android:id="@+id/t2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="56dp"
android:ems="10"
android:inputType="number"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/t1" />
<EditText
android:id="@+id/t3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="100dp"
android:layout_marginTop="60dp"
android:ems="10"
android:inputType="number"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/t2"
tools:ignore="MissingConstraints" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="56dp"
android:text="Find"
android:onClick="compute"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/t3" />

<TextView
android:id="@+id/res"
android:layout_width="173dp"
android:layout_height="31dp"
android:layout_marginTop="52dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java
package com.example.greatestamong3nos;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.example.greatestamong3nos.R;
public class MainActivity extends AppCompatActivity
{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
int readNumber(int id){
return Integer.parseInt(((EditText)findViewById(id)).getText().toString());
}
public void compute(View view){
int num1=readNumber(R.id.t1);
int num2=readNumber(R.id.t2);
int num3=readNumber(R.id.t3);
int result;
if(num1>num2&& num1>num3)
result=num1;
else if(num2>num1&&num2>num3)
result=num2;
else
result=num3;
((TextView)findViewById(R.id.res)).setText(Integer.toString(result));
}
}
Output:
Result:
Thus the application has been developed successfully.
EX.NO:2
PERSONAL DETAILS APPLICATION
DATE:

Aim:

To develop an application to display personal details using GUI Components.

Algorithm:

Step 1: Start.

Step 2: Open a new Android Project from File Menu.

Step 3: Choose Empty Activity Window to design your Application.

Step 4: Design the application in Design Editor Window with required tools.

Step 5: Open MainActivity.java file and type the code inside the class for

collecting personal details.

Step 6: Get personal details from user.

Step 7: Run the application.

Step 8: Stop.
Program Code:
activity_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"
android:padding="10dp"
android:gravity="center">

<TextView
android:id="@+id/tvInfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Personal Information of Student"
android:textColor="@android:color/holo_red_light"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.157"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.663" />

<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tvInfo"
android:ems="10"
android:hint="Enter you name"
android:inputType="textPersonName"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="133dp" />
<EditText
android:id="@+id/dob"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/name"
android:layout_marginTop="44dp"
android:ems="10"
android:hint="Enter Date of Birth"
android:inputType="date"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/name" />

<EditText
android:id="@+id/city"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/dob"
android:layout_marginTop="44dp"
android:ems="10"
android:hint="Enter your City"
android:inputType="textCapCharacters"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/dob" />

<EditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/city"
android:layout_marginTop="44dp"
android:ems="10"
android:hint="Enter Email ID"
android:inputType="textEmailAddress"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/city" />

<EditText
android:id="@+id/contact"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/email"
android:layout_marginTop="44dp"
android:ems="10"
android:hint="Enter Contact number"
android:inputType="date"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/email" />

<Button
android:id="@+id/btnSubmit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/contact"
android:layout_marginTop="48dp"
android:onClick="displayData"
android:text="Submit"
android:textSize="18sp"
app:layout_constraintTop_toBottomOf="@+id/contact"
tools:layout_editor_absoluteX="16dp" />

<TextView
android:id="@+id/res"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnSubmit"
app:layout_constraintVertical_bias="0.1" />
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java
package com.example.myapplicationpersonaldetails;
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 name, dob, city, email, contact;
Button submit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = findViewById(R.id.name);
dob = findViewById(R.id.dob);
city = findViewById(R.id.city);
email = findViewById(R.id.email);
contact = findViewById(R.id.contact);
submit = findViewById(R.id.btnSubmit);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String n = name.getText().toString();
String d = dob.getText().toString();
String ci = city.getText().toString();
String e = email.getText().toString();
String c = contact.getText().toString();

if(n.isEmpty() || d.isEmpty() || ci.isEmpty() || e.isEmpty() || c.isEmpty())


{

((TextView) findViewById(R.id.res)).setText("Enter data");


}
else
{
((TextView) findViewById(R.id.res)).setText(n + "\n" + d + "\n" + ci +
"\n" + e + "\n" + c);
}
}
});
}
}
Output:
Result:
Thus the application has been developed successfully.
EX.NO:3
APPLICATION USING RADIO BUTTON
DATE:

Aim:

To develop an android application using radio button.

Algorithm:

Step 1: Start.

Step 2: Open a new Android Project from File Menu.

Step 3: Choose Empty Activity Window to design your Application.

Step 4: Design the application in Design Editor Window with required tools.

Step 5: Open MainActivity.java file and type the code inside the class to work

with radio button.

Step 6: Get choice from user.

Step 7: Run the Application.

Step 8: Stop.
Program Code
activity_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">

<TextView
android:id="@+id/textView"
android:layout_width="237dp"
android:layout_height="75dp"
android:text="Application Using Radio Button "
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteY="69dp" />

<TextView
android:id="@+id/textView2"
android:layout_width="267dp"
android:layout_height="89dp"
android:layout_marginTop="76dp"
android:text="Choose Your Department"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />

<RadioGroup
android:id="@+id/r"
android:layout_width="120dp"
android:layout_height="94dp"
android:layout_marginStart="8dp"
android:layout_marginTop="40dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2">

<RadioButton
android:id="@+id/r2"
android:layout_width="139dp"
android:layout_height="wrap_content"
android:text="BSC" />
<RadioButton
android:id="@+id/r1"
android:layout_width="139dp"
android:layout_height="wrap_content"
android:text="BCA" />
</RadioGroup>

<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="56dp"
android:text="enter"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/r"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>

Main Activity.java
package com.example.myapplicationradiobutton;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


private Button b2;
String ans1;
String ans2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RadioGroup b1 = (RadioGroup) findViewById(R.id.r);
Button add = (Button) findViewById(R.id.b1);
b1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId) {
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
switch (checkedId) {
case R.id.r1:
ans1 = "BCA";
Toast.makeText(getApplicationContext(), "BCA",
Toast.LENGTH_LONG).show();
break;
case R.id.r2:
ans2 = "BSC";
Toast.makeText(getApplicationContext(), "BSC",
Toast.LENGTH_LONG).show();
break;

}
}
});
}
});
}
}
Output:
Result:
Thus the application has been developed successfully.
EX.NO:4
APPLICATION USING IMAGE BUTTON
DATE:

Aim:
To develop an Android Application using Image Button.

Algorithm:
Step 1: Start.

Step 2: Open a new Android Project from File Menu.

Step 3: Choose Empty Activity Window to design your Application.

Step 4: Design the application in Design Editor Window with required tools.
Step 5: Open MainActivity.java file and type the code inside the class to

perform addition using image button.

Step 6: Get two numbers from user using GUI Components.

Step 7: Run the Application.

Step 8: Stop.
Program Code
activity_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">

<TextView
android:id="@+id/textView2"
android:layout_width="258dp"
android:layout_height="58dp"
android:text="Add Two Numbers"
tools:layout_editor_absoluteX="92dp"
tools:layout_editor_absoluteY="35dp"
tools:ignore="MissingConstraints" />

<EditText
android:id="@+id/t1"
android:layout_width="262dp"
android:layout_height="46dp"
android:layout_marginTop="64dp"
android:ems="10"
android:inputType="number"
app:layout_constraintTop_toBottomOf="@+id/textView2"
tools:layout_editor_absoluteX="92dp"
tools:ignore="MissingConstraints" />

<EditText
android:id="@+id/t2"
android:layout_width="257dp"
android:layout_height="42dp"
android:layout_marginTop="64dp"
android:ems="10"
android:inputType="number"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.597"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/t1" />
<ImageButton
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="46dp"
app:layout_constraintTop_toBottomOf="@+id/t2"
app:srcCompat="@mipmap/ic_launcher"
tools:layout_editor_absoluteX="168dp"
tools:ignore="MissingConstraints" />

<TextView
android:id="@+id/res"
android:layout_width="288dp"
android:layout_height="74dp"
android:layout_marginTop="60dp"
android:text="TextView"
app:layout_constraintTop_toBottomOf="@+id/b1"
tools:layout_editor_absoluteX="74dp"
tools:ignore="MissingConstraints" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java
package com.example.myapplicationimagebutton;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
ImageButton imageButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
private void addListenerOnButton() {
imageButton = (ImageButton) findViewById(R.id.b1);
imageButton.setOnClickListener(new View.OnClickListener() {
int readNumber(int id) {
return Integer.parseInt(((EditText) findViewById(id)).getText().toString());
}

public void onClick(View view) {


int num1 = readNumber(R.id.t1);
int num2 = readNumber(R.id.t2);
((TextView) findViewById(R.id.res)).setText(Integer.toString(num1 +
num2));

}
});
}
}
Result:
Thus the application has been developed successfully.
EX.NO:5
APPLICATION USING ALERT DIALOG BOX
DATE:

Aim:
To develop an android application using Alert Dialog Box.

Algorithm:
Step 1: Start.
Step 2: Open a new Android Project from File Menu.

Step 3: Choose Empty Activity Window to design your Application.

Step 4: Design the application in Design Editor Window with required tools.

Step 5: Open MainActivity.java file and type the code inside the class
to work with Alert Dialog Box.

Step 6: Press Back Button of android device to get Alert Dialog box.

Step 7: Run the Application.

Step 8: Stop.
Program Code:
activity_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">

<TextView
android:id="@+id/textView2"
android:layout_width="151dp"
android:layout_height="42dp"
android:layout_marginTop="132dp"
android:text="USER NAME"
app:layout_constraintTop_toBottomOf="@+id/textView4"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="29dp" />

<TextView
android:id="@+id/textView3"
android:layout_width="149dp"
android:layout_height="38dp"
android:layout_marginTop="104dp"
android:text="PASSWORD"
app:layout_constraintTop_toBottomOf="@+id/textView2"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="29dp" />

<EditText
android:id="@+id/editTextTextPersonName"
android:layout_width="145dp"
android:layout_height="43dp"
android:layout_marginStart="40dp"
android:layout_marginTop="133dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Name"
app:layout_constraintStart_toEndOf="@+id/textView2"
app:layout_constraintTop_toBottomOf="@+id/textView4"
tools:ignore="MissingConstraints" />
<EditText
android:id="@+id/editTextTextPassword"
android:layout_width="143dp"
android:layout_height="36dp"
android:layout_marginStart="42dp"
android:layout_marginTop="104dp"
android:ems="10"
android:inputType="textPassword"
app:layout_constraintStart_toEndOf="@+id/textView3"
app:layout_constraintTop_toBottomOf="@+id/editTextTextPersonName"
tools:ignore="MissingConstraints" />

<TextView
android:id="@+id/textView4"
android:layout_width="278dp"
android:layout_height="40dp"
android:text="Application Using Alert Dialog Box"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="46dp"
tools:ignore="MissingConstraints" />

<TextView
android:id="@+id/textView5"
android:layout_width="232dp"
android:layout_height="41dp"
android:text="Press the Back Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextTextPassword"
app:layout_constraintVertical_bias="0.697" />
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java
package com.example.myapplicationalertdialogbox;
import android.content.DialogInterface;
import android.os.Bundle;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onBackPressed() {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setMessage("Do you want to exit ?");

builder.setTitle("Alert !");

builder.setCancelable(false);
builder.setPositiveButton("Yes", (DialogInterface.OnClickListener) (dialog,
which) -> {
finish();
});
builder.setNegativeButton("No", (DialogInterface.OnClickListener) (dialog,
which) -> {
dialog.cancel();
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}
Output:
Result:
Thus the application has been developed successfully.

You might also like