0% found this document useful (0 votes)
2 views

Android_assignments

The document contains multiple Android application assignments by Rasal Pratiksha Ganesh, including a login form with validation, phone number format checking, a quiz application, and a simple calculator. Each assignment includes XML layout files and Java code for functionality. The applications demonstrate various features such as user input handling, validation, and basic UI components.

Uploaded by

Vaibhavi Shelke
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Android_assignments

The document contains multiple Android application assignments by Rasal Pratiksha Ganesh, including a login form with validation, phone number format checking, a quiz application, and a simple calculator. Each assignment includes XML layout files and Java code for functionality. The applications demonstrate various features such as user input handling, validation, and basic UI components.

Uploaded by

Vaibhavi Shelke
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 44

NAME:- Rasal Pratiksha Ganesh

ROLL NO. :-7402


ASSIGNMENT NO.:-1
ASSIGNMENT NAME:- Java Android Program to demonstrate Login form with
validation.
*************************************************************************************
//activity_main.xml

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


<RelativeLayout xmlns:android =
"http://schemas.android.com/apk/res/android"
xmlns:tools = "http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height = "match_parent"
tools:context = ".MainActivity">

<TextView
android:text = "Login Page"
android:layout_width="wrap_content"
android:layout_height = "wrap_content"
android:id = "@+id/textView1"
android:textSize = "35dp"
android:layout_marginTop="20dp"
android:layout_alignParentTop = "true"
android:layout_centerHorizontal = "true" />

<EditText
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:id = "@+id/editText1"
android:hint = "Enter Username"
android:focusable = "true"
android:textColorHighlight = "#ff7eff15"
android:textColorHint = "#ffff25e6"
android:layout_marginTop = "46dp"
android:layout_below = "@+id/textView1"
android:layout_alignParentLeft = "true"
android:layout_alignParentStart = "true"
android:layout_alignParentRight = "true"
android:layout_alignParentEnd = "true" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/editText2"
android:layout_marginTop = "30dp"
android:layout_below="@+id/editText1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/editText1"
android:layout_alignEnd="@+id/editText1"
android:textColorHint="#ffff299f"
android:hint="Enter Password" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Attempts Left:"
android:id="@+id/textView2"
android:layout_below="@+id/editText2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="25dp" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:id="@+id/button"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="@+id/textView1"
android:layout_toStartOf="@+id/textView1" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:id="@+id/button2"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@+id/textView1"
android:layout_toEndOf="@+id/textView1" />

</RelativeLayout>

//MainActivity.java

package com.example.login_page;

import android.app.Activity;
import android.graphics.Color;
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 Activity {


Button login,cancel;
EditText username,password;

TextView attempts;
int counter = 3;

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

login = (Button)findViewById(R.id.button);
username = (EditText)findViewById(R.id.editText1);
password = (EditText)findViewById(R.id.editText2);

cancel = (Button)findViewById(R.id.button2);
attempts = (TextView)findViewById(R.id.textView2);

login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(username.getText().toString().equals("admin") &&
password.getText().toString().equals("admin")) {
Toast.makeText(getApplicationContext(),
"Welcome",Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getApplicationContext(), "Something Went
Wrong",Toast.LENGTH_SHORT).show();

attempts.setVisibility(View.VISIBLE);
attempts.setBackgroundColor(Color.RED);
counter--;
attempts.setText(Integer.toString(counter));

if (counter == 0) {
login.setEnabled(false);
}
}
}
});

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

/******************************* Output ***********************************

*******************************************************************************/
NAME:- Rasal Pratiksha Ganesh
ROLL NO. :-7402
ASSIGNMENT NO.:-4
ASSIGNMENT NAME:- Create an Android application which examine, that a phone
number, which a user has entered is in the given format. * Area
code should be one of the following: 040, 041, 050, 0400, 044 *
There should 6- 8 numbers in telephone number (+ area code).
*************************************************************************************

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

<EditText
android:id="@+id/phone_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Phone Number"
android:padding="50dp"
android:textSize="15dp"
android:layout_marginTop="100dp" />

<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Check"
android:textSize="25dp" />

</LinearLayout>

//MainActivity.java

package com.example.examine_phone_number;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MainActivity extends AppCompatActivity {

EditText phone_number;
Button button;
Matcher m;

String pattern="^(040|041|050|0400|044)([0-9]{6,8})$";

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

phone_number = findViewById(R.id.phone_number);
button = findViewById(R.id.button);

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Pattern r = Pattern.compile(pattern);

if (!phone_number.getText().toString().isEmpty()) {
m = r.matcher(phone_number.getText().toString().trim());
} else {
Toast t = Toast.makeText(getApplicationContext(), "Please Enter Mobile
Number", Toast.LENGTH_SHORT);
t.show();
}

if (m.find()) {
Toast t = Toast.makeText(getApplicationContext(), "Match",
Toast.LENGTH_SHORT);
t.show();
} else {
Toast t = Toast.makeText(getApplicationContext(), "No match",
Toast.LENGTH_SHORT);
t.show();
}
}
});
}
}

/********************************** Output **************************************

********************************************************************************/
NAME:- Rasal Pratiksha Ganesh
ROLL NO. :-7402
ASSIGNMENT NO.:-6
ASSIGNMENT NAME:- Create an Android application, which show to the user 5-10
quiz questions. All questions have 4 possible options and one
right option exactly. Application counts and shows to the user
how many right answers were right and shows the result to user.

*************************************************************************************
//activity_main.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/score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:textSize="25dp"
android:gravity="center"/>

<TextView
android:id="@+id/question"
android:layout_width="match_parent"
android:layout_height="235dp"
android:textSize="20dp"
android:gravity="center" />

<Button
android:id="@+id/answer1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15dp" />

<Button
android:id="@+id/answer2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15dp" />

<Button
android:id="@+id/answer3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15dp" />

<Button
android:id="@+id/answer4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15dp" />

</LinearLayout>

//Questions.java

package com.example.quiz;

public class Questions {

public String questions[] = {"Who developed C?", "Who developed PHP?", "Who
developed Java?", "Who developed Linux?", "Who developed C++?"};

private String choices[][] = {


{"Dennis Ritchie", "James Gosling", "Rasmus Lardorf", "Bjarne Stroustrup"},
{"Tim Berners Lee", "Rasmus Lardorf", "James Gosling", "Linus Torvalds"},
{"Allen Turing", "Dennis Ritchie", "James Gosling", "Bjarne Stroustrup"},
{"Tim Berners Lee", "Rasmus Lardorf", "James Gosling", "Linus Torvalds"},
{"Tim Berners Lee", "Rasmus Lardorf", "Bjarne Stroustrup", "James
Gosling"}
};

private String correct_answer[] = {"Dennis Ritchie", "Rasmus Lardorf", "James


Gosling", "Linus Torvalds", "Bjarne Stroustrup"};

public String getQuestion(int q)


{
String question=questions[q];
return question;
}

public String getChoice1(int c)


{
String choice=choices[c][0];
return choice;
}

public String getChoice2(int c)


{
String choice=choices[c][1];
return choice;
}

public String getChoice3(int c)


{
String choice=choices[c][2];
return choice;
}

public String getChoice4(int c)


{
String choice=choices[c][3];
return choice;
}

public String getCorrectAnswer(int ca)


{
String answer=correct_answer[ca];
return answer;
}
}

//MainActivity.java

package com.example.quiz;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

TextView score,question;
Button answer1,answer2,answer3,answer4;
private Questions questions=new Questions();
private String canswer;
private int score1=0;
private int question_length=questions.questions.length;
Random r;

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

score = findViewById(R.id.score);
question = findViewById(R.id.question);
answer1 = findViewById(R.id.answer1);
answer2 = findViewById(R.id.answer2);
answer3 = findViewById(R.id.answer3);
answer4 = findViewById(R.id.answer4);

r = new Random();
score.setText("Score:"+score1);
update_question(r.nextInt(question_length));

answer1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(answer1.getText()==canswer)
{
score1++;
score.setText("Score:"+score1);
update_question(r.nextInt(question_length));
}
else
{
gameOver();
}
}
});

answer2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(answer2.getText()==canswer)
{
score1++;
score.setText("Score:"+score1);
update_question(r.nextInt(question_length));
}
else
{
gameOver();
}
}
});

answer3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(answer3.getText()==canswer)
{
score1++;
score.setText("Score:"+score1);
update_question(r.nextInt(question_length));
}
else
{
gameOver();
}
}
});

answer4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(answer4.getText()==canswer)
{
score1++;
score.setText("Score:"+score1);
update_question(r.nextInt(question_length));
}
else
{
gameOver();
}
}
});
}

private void update_question(int num)


{
question.setText(questions.getQuestion(num));
answer1.setText(questions.getChoice1(num));
answer2.setText(questions.getChoice2(num));
answer3.setText(questions.getChoice3(num));
answer4.setText(questions.getChoice4(num));
canswer=questions.getCorrectAnswer(num);
}

private void gameOver()


{
AlertDialog.Builder alertDialogBuilder=new
AlertDialog.Builder(MainActivity.this);
alertDialogBuilder
.setMessage("Game Over...Your score is:"+score1)
.setCancelable(false)
.setPositiveButton("New Game", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(getApplicationContext(),MainActivity.class));
finish();
}
})
.setNegativeButton("Exit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
AlertDialog alertDialog=alertDialogBuilder.create();
alertDialog.show();
}
}

/********************************** Output **************************************


********************************************************************************/
NAME:- Rasal Pratiksha Ganesh
ROLL NO. :-7402
ASSIGNMENT NO.:-3
ASSIGNMENT NAME:- Create the simple calculator shown below also perform
appropriate operation.

*************************************************************************************
//activity_main.xml

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


<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relative1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<EditText
android:id="@+id/edt1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/button4"
android:layout_alignRight="@+id/button4"
android:layout_below="@+id/edt1"
android:layout_marginTop="94dp"
android:text="1" />

<Button
android:id="@+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button1"
android:layout_toLeftOf="@+id/button3"
android:layout_toStartOf="@+id/button3"
android:text="2" />

<Button
android:id="@+id/button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button2"
android:layout_centerHorizontal="true"
android:text="3" />

<Button
android:id="@+id/button4"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_toLeftOf="@+id/button2"
android:text="4" />

<Button
android:id="@+id/button5"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button4"
android:layout_alignLeft="@+id/button2"
android:layout_alignStart="@+id/button2"
android:text="5" />

<Button
android:id="@+id/button6"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button3"
android:layout_alignStart="@+id/button3"
android:layout_below="@+id/button3"
android:text="6" />

<Button
android:id="@+id/button7"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button4"
android:layout_toLeftOf="@+id/button2"
android:text="7" />

<Button
android:id="@+id/button8"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button5"
android:layout_alignStart="@+id/button5"
android:layout_below="@+id/button5"
android:text="8" />

<Button
android:id="@+id/button9"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button6"
android:layout_alignStart="@+id/button6"
android:layout_below="@+id/button6"
android:text="9" />

<Button
android:id="@+id/buttonadd"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/edt1"
android:layout_alignRight="@+id/edt1"
android:layout_alignTop="@+id/button3"
android:layout_marginLeft="46dp"
android:layout_marginStart="46dp"
android:layout_toRightOf="@+id/button3"
android:text="+" />

<Button
android:id="@+id/buttonsub"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/buttonadd"
android:layout_alignLeft="@+id/buttonadd"
android:layout_alignRight="@+id/buttonadd"
android:layout_alignStart="@+id/buttonadd"
android:layout_below="@+id/buttonadd"
android:text="-" />

<Button
android:id="@+id/buttonmul"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/buttonsub"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignStart="@+id/buttonsub"
android:layout_below="@+id/buttonsub"
android:text="*" />

<Button
android:id="@+id/button10"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button7"
android:layout_toLeftOf="@+id/button2"
android:text="." />

<Button
android:id="@+id/button0"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button8"
android:layout_alignStart="@+id/button8"
android:layout_below="@+id/button8"
android:text="0" />

<Button
android:id="@+id/buttonC"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button9"
android:layout_alignStart="@+id/button9"
android:layout_below="@+id/button9"
android:text="C" />

<Button
android:id="@+id/buttondiv"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/buttonmul"
android:layout_alignLeft="@+id/buttonmul"
android:layout_alignRight="@+id/buttonmul"
android:layout_alignStart="@+id/buttonmul"
android:layout_below="@+id/buttonmul"
android:text="/" />

<Button
android:id="@+id/buttoneql"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/buttondiv"
android:layout_alignLeft="@+id/button10"
android:layout_alignRight="@+id/buttondiv"
android:layout_alignStart="@+id/button10"
android:layout_below="@+id/button0"
android:layout_marginTop="37dp"
android:text="=" />
</RelativeLayout>

//MainActivity.java

package com.example.calculator;

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

public class MainActivity extends AppCompatActivity {

Button button0, button1, button2, button3, button4, button5, button6,


button7, button8, button9, buttonAdd, buttonSub, buttonDivision,
buttonMul, button10, buttonC, buttonEqual;
EditText crunchifyEditText;

float mValueOne, mValueTwo;

boolean crunchifyAddition, mSubtract, crunchifyMultiplication,


crunchifyDivision;

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

button0 = (Button) findViewById(R.id.button0);


button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
button5 = (Button) findViewById(R.id.button5);
button6 = (Button) findViewById(R.id.button6);
button7 = (Button) findViewById(R.id.button7);
button8 = (Button) findViewById(R.id.button8);
button9 = (Button) findViewById(R.id.button9);
button10 = (Button) findViewById(R.id.button10);
buttonAdd = (Button) findViewById(R.id.buttonadd);
buttonSub = (Button) findViewById(R.id.buttonsub);
buttonMul = (Button) findViewById(R.id.buttonmul);
buttonDivision = (Button) findViewById(R.id.buttondiv);
buttonC = (Button) findViewById(R.id.buttonC);
buttonEqual = (Button) findViewById(R.id.buttoneql);
crunchifyEditText = (EditText) findViewById(R.id.edt1);

button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
crunchifyEditText.setText(crunchifyEditText.getText() + "1");
}
});

button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
crunchifyEditText.setText(crunchifyEditText.getText() + "2");
}
});

button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
crunchifyEditText.setText(crunchifyEditText.getText() + "3");
}
});

button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
crunchifyEditText.setText(crunchifyEditText.getText() + "4");
}
});

button5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
crunchifyEditText.setText(crunchifyEditText.getText() + "5");
}
});

button6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
crunchifyEditText.setText(crunchifyEditText.getText() + "6");
}
});

button7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
crunchifyEditText.setText(crunchifyEditText.getText() + "7");
}
});

button8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
crunchifyEditText.setText(crunchifyEditText.getText() + "8");
}
});

button9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
crunchifyEditText.setText(crunchifyEditText.getText() + "9");
}
});

button0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
crunchifyEditText.setText(crunchifyEditText.getText() + "0");
}
});

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

if (crunchifyEditText == null) {
crunchifyEditText.setText("");
} else {
mValueOne = Float.parseFloat(crunchifyEditText.getText() + "");
crunchifyAddition = true;
crunchifyEditText.setText(null);
}
}
});

buttonSub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mValueOne = Float.parseFloat(crunchifyEditText.getText() + "");
mSubtract = true;
crunchifyEditText.setText(null);
}
});

buttonMul.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mValueOne = Float.parseFloat(crunchifyEditText.getText() + "");
crunchifyMultiplication = true;
crunchifyEditText.setText(null);
}
});

buttonDivision.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mValueOne = Float.parseFloat(crunchifyEditText.getText() + "");
crunchifyDivision = true;
crunchifyEditText.setText(null);
}
});

buttonEqual.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mValueTwo = Float.parseFloat(crunchifyEditText.getText() + "");

if (crunchifyAddition == true) {
crunchifyEditText.setText(mValueOne + mValueTwo + "");
crunchifyAddition = false;
}

if (mSubtract == true) {
crunchifyEditText.setText(mValueOne - mValueTwo + "");
mSubtract = false;
}
if (crunchifyMultiplication == true) {
crunchifyEditText.setText(mValueOne * mValueTwo + "");
crunchifyMultiplication = false;
}

if (crunchifyDivision == true) {
crunchifyEditText.setText(mValueOne / mValueTwo + "");
crunchifyDivision = false;
}
}
});

buttonC.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
crunchifyEditText.setText("");
}
});

button10.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
crunchifyEditText.setText(crunchifyEditText.getText() + ".");
}
});
}
}

/********************************** Output **************************************


********************************************************************************/

NAME:- Rasal Pratiksha Ganesh


ROLL NO. :-7402
ASSIGNMENT NO.:-13
ASSIGNMENT NAME:- Create table Customer(id,name,address,phone). Create an
android application for performing the following oeration on
the table.(using SQLite database)
i) Insert new customer details
ii) Show all the customer details
*************************************************************************************
//activity_main.xml

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


<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Customer Details"
android:textSize="25dp"
android:layout_gravity="center" />

<!--Edit text to enter course name-->


<EditText
android:id="@+id/custId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_margin="10dp"
android:hint="Enter Customer Id" />

<!--edit text to enter course duration-->


<EditText
android:id="@+id/custName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="Enter Customer Name" />

<!--edit text to display course tracks-->


<EditText
android:id="@+id/custAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="Enter Customer Address" />

<!--edit text for course description-->


<EditText
android:id="@+id/custPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="Enter Customer Phone No." />

<!--button for adding new course-->


<Button
android:id="@+id/add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Add Customer"
android:textAllCaps="false" />

<Button
android:id="@+id/show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Show All Customers"
android:textAllCaps="false" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text1"
android:hint="Data" />

</LinearLayout>

//DBHandler.java

package com.example.customerdetailstable;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHandler extends SQLiteOpenHelper {

// creating a constant variables for our database.


// below variable is for our database name.
private static final String DB_NAME = "customerDB";

// below int is our database version


private static final int DB_VERSION = 1;

public DBHandler(Context context) {


super(context, DB_NAME, null, DB_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {

String sql = "CREATE TABLE CUSTOMER(CUST_ID INTEGER PRIMARY KEY


AUTOINCREMENT, CUST_NAME TEXT, CUST_ADDRESS TEXT, CUST_PHONE
NUMBER)";
db.execSQL(sql);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}
}

//MainActivity.java

package com.example.customerdetailstable;

import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.text.TextUtils;
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 {

// creating variables for our edittext, button and dbhandler


private EditText id, name, address, phone;
private Button addCustomer, showCustomer;
private TextView textdata;
private SQLiteOpenHelper helper;
private SQLiteDatabase database;

boolean isEmpty(EditText text){


CharSequence str = text.getText().toString();
return TextUtils.isEmpty(str);
}

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

// initializing all our variables.


id = findViewById(R.id.custId);
name = findViewById(R.id.custName);
address = findViewById(R.id.custAddress);
phone = findViewById(R.id.custPhone);
addCustomer = findViewById(R.id.add);
showCustomer = findViewById(R.id.show);

helper = new DBHandler(this);

// below line is to add on click listener for our add course button.
addCustomer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

// below line is to get data from all edit text fields.


String custId = id.getText().toString();
String custName = name.getText().toString();
String custAddress = address.getText().toString();
String custPhone = phone.getText().toString();

// validating if the text fields are empty or not.


if (custId.isEmpty() && custName.isEmpty() && custAddress.isEmpty()
&& custPhone.isEmpty()) {
Toast.makeText(MainActivity.this, "Please Enter All The Data..",
Toast.LENGTH_SHORT).show();
return;
} else {
insert_customer(custId, custName, custAddress, custPhone);
id.setText("");
name.setText("");
address.setText("");
phone.setText("");

Toast.makeText(MainActivity.this, "Customer has been added.",


Toast.LENGTH_SHORT).show();
}
}
});

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

database = helper.getWritableDatabase();

Cursor cursor = database.rawQuery("SELECT CUST_ID, CUST_NAME,


CUST_ADDRESS, CUST_PHONE FROM CUSTOMER", new String[]{});

if(cursor != null){
cursor.moveToFirst();
}

StringBuilder builder = new StringBuilder();


do{
String cust_id = cursor.getString(0);
String cust_name = cursor.getString(1);
String cust_address = cursor.getString(2);
String cust_phone = cursor.getString(3);

builder.append("Customer Id :" + cust_id + "Customer Name :" +


cust_name + "Customer Address :" + cust_address + "Customer Phone No. :" +
cust_phone);
builder.append("\n");
}while(cursor.moveToNext());
textdata = findViewById(R.id.text1);
textdata.setText(builder.toString());
}
});
}

public void insert_customer(String cust_id, String cust_name, String


cust_address, String cust_phone)
{
ContentValues values = new ContentValues();

values.put("CUST_ID", cust_id);
values.put("CUST_NAME", cust_name);
values.put("CUST_ADDRESS", cust_address);
values.put("CUST_PHONE", cust_phone);
database.insert("CUSTOMER", null, values);
}
}

/******************************** Output **********************************


***********************************************************************************/
NAME:- Rasal Pratiksha Ganesh
ROLL NO. :-7402
ASSIGNMENT NO.:-17
ASSIGNMENT NAME:- Java Android Program to Change the Image Displayed on the
Screen
OR
Construct image switcher using setFactory().
*************************************************************************************
//activity_main.xml

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

<RelativeLayout
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:paddingTop="16dp"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
tools:context=".MainActivity">

<ImageSwitcher
android:id="@+id/image1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true">
</ImageSwitcher>

<Button
android:id="@+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="Previous" />

<Button
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="300dp"
android:layout_marginLeft="300dp"
android:layout_marginBottom="-7dp"
android:text="Next" />

</RelativeLayout>

//MainActivity.java

package com.example.imageswitcher;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;

public class MainActivity extends AppCompatActivity {


private ImageSwitcher sw;
private Button previous,next;

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

sw = findViewById(R.id.image1);
previous = findViewById(R.id.previous);
next = findViewById(R.id.next);

sw.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
ImageView imageView = new ImageView(getApplicationContext());
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
return imageView;
}
});

previous.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sw.setImageResource(R.drawable.android);
sw.setImageResource(R.drawable.welcome);
}
});

next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sw.setImageResource(R.drawable.android);
}
});
}
}

/*********************************** Output *************************************

********************************************************************************/
NAME:- Rasal Pratiksha Ganesh
ROLL NO. :-7402
ASSIGNMENT NO.:-12
ASSIGNMENT NAME:- Write an application to accept two numbers from the user, and
displays them, but reject input if both numbers are greater
than 10 and asks for two new numbers.

*************************************************************************************
//activity_main.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"
android:paddingTop="250dp"
tools:context=".MainActivity">

<EditText
android:id="@+id/num1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter First Number" />

<EditText
android:id="@+id/num2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Second Number" />

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

<TextView
android:id="@+id/n1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/n2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/textdata"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>

//MainActivity.java

package com.example.twonumbers;

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 num1,num2;
Button check_num;
TextView textdata,num1_show,num2_show;

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

num1 = findViewById(R.id.num1);
num2 = findViewById(R.id.num2);
check_num = findViewById(R.id.check_num);
textdata = findViewById(R.id.textdata);
num1_show = findViewById(R.id.n1);
num2_show = findViewById(R.id.n2);

check_num.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int n1=Integer.parseInt(num1.getText().toString());
int n2=Integer.parseInt(num2.getText().toString());
if(n1>10 && n2>10)
{
Toast.makeText(getApplicationContext(),"Please Enter Numbers Smaller
Than 10",Toast.LENGTH_SHORT).show();
num1.setText("");
num2.setText("");
}
else
{
num1_show.setText("Number 1 = "+n1+"\n");
num2_show.setText("Number 2 = "+n2+"\n");
textdata.setText("Both Numbers Are Smaller Than 10");
num1.setText("");
num2.setText("");
}
}
});
}
}

/*********************************** Output *************************************


**************************************************************************************/
NAME:- Rasal Pratiksha Ganesh
ROLL NO. :-7402
ASSIGNMENT NO.:-2
ASSIGNMENT NAME:- Java Android Program to demonstrate Registration form with
validation.

********************************************************************************
//activity_main.xml

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.registration.MainActivity">

<EditText
android:id="@+id/editTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:ems="10"
android:hint="Enter Name"
android:inputType="textPersonName" />

<EditText
android:id="@+id/editTextEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="Enter Email"
android:inputType="textPersonName" />

<EditText
android:id="@+id/editTextMobile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="Enter Mobile Number"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/editTextDob"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="Enter Date of Birth (DD/MM/YYYY)"
android:inputType="date" />

<EditText
android:id="@+id/editTextAge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="Enter Age"
android:inputType="number" />

<Button
android:id="@+id/buttonSubmit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Submit" />

</LinearLayout>

//MainActivity.java

package com.example.registration;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.text.SimpleDateFormat;
public class MainActivity extends Activity implements View.OnClickListener {

//The view objects


private EditText editTextName, editTextEmail, editTextMobile,
editTextDob, editTextAge;
private Button buttonSubmit;

//defining AwesomeValidation object

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//initializing view objects


editTextName = (EditText) findViewById(R.id.editTextName);
editTextEmail = (EditText) findViewById(R.id.editTextEmail);
editTextMobile = (EditText) findViewById(R.id.editTextMobile);
editTextDob = (EditText) findViewById(R.id.editTextDob);
editTextAge = (EditText) findViewById(R.id.editTextAge);

buttonSubmit = (Button) findViewById(R.id.buttonSubmit);

//adding validation to edittexts

buttonSubmit.setOnClickListener(this);
}

private void submitForm() {


try{
//first validate the form then move ahead
//if this becomes true that means validation is successfull
String name=editTextName.getText().toString();
int age=Integer.parseInt(editTextAge.getText().toString());
if(editTextName.length()==0)
{
editTextName.requestFocus();
editTextName.setError("FIELD CANNOT BE EMPTY");
}
else if(!name.matches("[a-zA-Z ]+"))
{
editTextName.requestFocus();
editTextName.setError("ENTER ONLY ALPHABETICAL CHARACTER");
}
else if(age<18)
{
editTextAge.requestFocus();
editTextAge.setError("ONLY 18 year olds are allowed");
}
else if(!isEditTextContainEmail(editTextEmail.getText().toString())) {
editTextEmail.requestFocus();
editTextEmail.setError("InValid EMail address");
}
else if(!validCellPhone(editTextMobile.getText().toString())) {
editTextMobile.requestFocus();
editTextMobile.setError("InValid Mobile Number");
}
else if(!checkDateFormat(editTextDob.getText().toString())) {
editTextDob.requestFocus();
editTextDob.setError("InValid Date of Birth");
}
else {

Toast.makeText(MainActivity.this, "Validation Successful",


Toast.LENGTH_LONG).show();
}
} catch (Exception e)
{

Toast.makeText(MainActivity.this, "Problem : "+e.toString(),


Toast.LENGTH_LONG).show();
}
}
public static boolean isEditTextContainEmail(String argEditText) {

try {
Pattern pattern = Pattern.compile("^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]
+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");
Matcher matcher = pattern.matcher(argEditText);
return matcher.matches();
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public boolean validCellPhone(String number)
{
return android.util.Patterns.PHONE.matcher(number).matches();
}
public boolean checkDateFormat(String date) {
if (date == null || !date.matches("^(1[0-9]|0[1-9]|3[0-1]|2[1-9])/(0[1-9]|1[0-
2])/[0-9]{4}$"))
return false;
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
try {
format.parse(date);
return true;
} catch (Exception e) {
return false;
}
}
@Override
public void onClick(View view) {
if (view == buttonSubmit) {
submitForm();
}
}
}

/*********************************** Output *************************************


************************************************************************************/

You might also like