Program 1&2 Merge
Program 1&2 Merge
Program 1&2 Merge
Create an application to design a Visiting Card. The Visiting card should have a
company logo at the top right corner. The company name should be displayed in Capital
letters, aligned to the center. Information like the name of the employee, job title,
phone number, address, email, fax and the website address is to be displayed. Insert
a horizontal line between the job title and the phone number.
Design
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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="59dp">
<TextView
android:id="@+id/textView"
android:layout_width="79dp"
android:layout_height="42dp"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="113dp"
android:layout_marginLeft="20dp"
android:layout_marginBottom="12dp"
android:gravity="center"
android:text="RNS IT"
android:textColor="#E61717"
android:textSize="20sp" />
<ImageView
android:id="@+id/imageView4"
android:layout_width="48dp"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_marginLeft="88dp"
android:layout_marginBottom="1dp"
android:layout_toRightOf="@id/textView"
app:srcCompat="@drawable/rnsit" />
</RelativeLayout>
<View android:layout_width="match_parent"
android:layout_height="2dp" android:background="#000000" />
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Shwetha G N"
android:textSize="16dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:textColor="#000000"
android:gravity="center" />
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Asst prof"
android:textSize="16dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:textColor="#000000"
android:gravity="center" />
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#000000" />
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="+91-9108245345"
android:textSize="16dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:textColor="#000000"
android:gravity="center" />
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Bangalore"
android:textSize="16dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:textColor="#000000" android:gravity="center" />
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content" android:text="Email:rnsit.ac.in"
android:textSize="16dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:textColor="#000000"
android:gravity="center" />
</LinearLayout>
Sample Output:
Program 2
Develop an Android application using controls like Button, TextView, EditText for
designing a Calculator having basic functionality like Addition, Subtraction,
Multiplication, and Division.
Design
activity_main.xml
<Button
android:id="@+id/button_nine"
android:layout_width="62dp"
android:layout_height="48dp"
android:text="9"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.123"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.467" />
<Button
android:id="@+id/button_zero"
android:layout_width="63dp"
android:layout_height="46dp"
android:text="0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.39"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.465"
tools:ignore="TouchTargetSizeCheck" />
<Button
android:id="@+id/button_sub"
android:layout_width="69dp"
android:layout_height="49dp"
android:text="-"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.953"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.463" />
<Button
android:id="@+id/button_add"
android:layout_width="65dp"
android:layout_height="44dp"
android:text="+"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.656"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.467"
tools:ignore="TouchTargetSizeCheck" />
<Button
android:id="@+id/button_equal"
android:layout_width="62dp"
android:layout_height="48dp"
android:text="="
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.163"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.689" />
<Button
android:id="@+id/button_mul"
android:layout_width="62dp"
android:layout_height="48dp"
android:text="*"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.123"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.568" />
<Button
android:id="@+id/button_div"
android:layout_width="63dp"
android:layout_height="46dp"
android:text="/"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.39"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.566"
tools:ignore="TouchTargetSizeCheck" />
<Button
android:id="@+id/button_clear"
android:layout_width="69dp"
android:layout_height="49dp"
android:text="C"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.953"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.564" />
<Button
android:id="@+id/button_dot"
android:layout_width="65dp"
android:layout_height="44dp"
android:text="."
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.656"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.567"
tools:ignore="TouchTargetSizeCheck" />
<Button
android:id="@+id/button_five"
android:layout_width="62dp"
android:layout_height="48dp"
android:text="5"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.123"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.377" />
<Button
android:id="@+id/button_six"
android:layout_width="63dp"
android:layout_height="46dp"
android:text="6"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.39"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.376"
tools:ignore="TouchTargetSizeCheck" />
<Button
android:id="@+id/button_eight"
android:layout_width="69dp"
android:layout_height="49dp"
android:text="8"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.953"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.373" />
<Button
android:id="@+id/button_seven"
android:layout_width="65dp"
android:layout_height="44dp"
android:text="7"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.656"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.378"
tools:ignore="TouchTargetSizeCheck" />
<Button
android:id="@+id/button_one"
android:layout_width="62dp"
android:layout_height="48dp"
android:text="1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.123"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.281" />
<Button
android:id="@+id/button_two"
android:layout_width="63dp"
android:layout_height="46dp"
android:text="2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.39"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.28"
tools:ignore="TouchTargetSizeCheck" />
<Button
android:id="@+id/button_three"
android:layout_width="65dp"
android:layout_height="44dp"
android:text="3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.656"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.282"
tools:ignore="TouchTargetSizeCheck" />
<TextView
android:id="@+id/textView"
android:layout_width="310dp"
android:layout_height="46dp"
android:text="Simple Calculator"
android:textColor="#C2185B"
android:textSize="25dp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.422"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.058" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Result"
android:textColor="#00796B"
android:textSize="20dp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.07"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.176" />
<EditText
android:id="@+id/txt_result"
android:layout_width="249dp"
android:layout_height="40dp"
android:layout_marginTop="20dp"
android:ems="10"
android:inputType="textPersonName"
android:text=""
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.839"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.135"
tools:ignore="SpeakableTextPresentCheck,TouchTargetSizeCheck" />
<Button
android:id="@+id/button_four"
android:layout_width="69dp"
android:layout_height="49dp"
android:text="4"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.952"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.277" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.program2;
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.Pattern;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnOne = (Button) findViewById(R.id.button_one);
btnOne.setOnClickListener(this);
@Override
public void onClick(View v)
{
if (v.equals(btnOne))
txtResult.append("1");
if (v.equals(btnTwo))
txtResult.append("2");
if (v.equals(btnThree))
txtResult.append("3");
if (v.equals(btnFour))
txtResult.append("4");
if (v.equals(btnFive))
txtResult.append("5");
if (v.equals(btnSix))
txtResult.append("6");
if (v.equals(btnSeven))
txtResult.append("7");
if (v.equals(btnEight))
txtResult.append("8");
if (v.equals(btnNine))
txtResult.append("9");
if (v.equals(btnZero))
txtResult.append("0");
if (v.equals(btnDot))
txtResult.append(".");
if (v.equals(btnClear))
txtResult.setText("");
if (v.equals(btnAdd))
txtResult.append("+");
if (v.equals(btnSub))
txtResult.append("-");
if (v.equals(btnMul))
txtResult.append("*");
if (v.equals(btnDiv))
txtResult.append("/");
if (v.equals(btnEqual))
{
try {
String data = txtResult.getText().toString();
if (data.contains("/")) {
String[] operands = data.split("/");
if (operands.length == 2) {
double operand1 = Double.parseDouble(operands[0]);
double operand2 = Double.parseDouble(operands[1]);
double result = operand1 / operand2;
txtResult.setText(String.valueOf(result));
} else {
Toast.makeText(getBaseContext(), "Invalid Input",
Toast.LENGTH_LONG).show();
}
} else if (data.contains("*")) {
String[] operands = data.split(Pattern.quote("*"));
if (operands.length == 2) {
double operand1 = Double.parseDouble(operands[0]);
double operand2 = Double.parseDouble(operands[1]);
double result = operand1 * operand2;
txtResult.setText(String.valueOf(result));
} else {
Toast.makeText(getBaseContext(), "Invalid Input",
Toast.LENGTH_LONG).show();
}
} else if (data.contains("+"))
{
String[] operands = data.split(Pattern.quote("+"));
if (operands.length == 2) {
double operand1 = Double.parseDouble(operands[0]);
double operand2 = Double.parseDouble(operands[1]);
double result = operand1 + operand2;
txtResult.setText(String.valueOf(result));
}
else {
Toast.makeText(getBaseContext(), "Invalid Input",
Toast.LENGTH_LONG).show();
}
}
else if (data.contains("-"))
{
String[] operands = data.split("-");
if (operands.length == 2) {
double operand1 =
Double.parseDouble(operands[0]); double
operand2 = Double.parseDouble(operands[1]);
double result = operand1 - operand2;
txtResult.setText(String.valueOf(result));
}
else
{
Toast.makeText(getBaseContext(), "Invalid Input",
Toast.LENGTH_LONG).show();
}
}
}
catch (Exception e)
{
Toast.makeText(getBaseContext(), "Invalid Input",
Toast.LENGTH_LONG).show();
}
}
}
}
Sample Output
1 Mobile Application Development
Program-3:
Create a SIGNUp activity with Username and Password. Validation of password
should happen based on the following rules:
Password should contain uppercase and lowercase letters.
Password should contain letters and numbers.
Password should contain special characters.
Minimum length of the password (the default value is 8).
On successful SIGN UP proceed to the next Login activity. Here the user should SIGN
IN using the Username and Password created during signup activity. If the Username
and Password are matched then navigate to the next activity which displays a message
saying “Successful Login” or else display a toast message saying “Login Failed”. The
user is given only two attempts and after that display a toast message saying “Failed
Login Attempts” and disable the SIGN IN button. Use Bundle to transfer information
from one activity to another.
1) Firstly Create an Application by Name “SignUpApplication”
2) Go to xml code of design change the layout to “RelativeLayout”
3) Add TextView component & change the following properties:
Size: 24sp
Text: “Sign Up”
Center-Align
4) Add Email (EditText) component & change the following properties in XML Code:
Hint: “Email-ID”
id: “@+id/emailEditText”
5) Add Password (EditText) component & change the following properties in XML
Code:
Hint: “Password”
id: “@+id/passwordEditText”
6) Add Button component & change the following properties in XML
Id: “@+id/signBtn”
Text: “Sign Up”
XML-CODE
<?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"
tools:context=".MainActivity">
<TextView
android:layout_width="129dp"
android:layout_height="45dp"
android:layout_alignParentEnd="true"
1
2 Mobile Application Development
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="168dp"
android:layout_marginRight="168dp"
android:layout_marginBottom="596dp"
android:text="Sign Up"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/SignUp_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="114dp"
android:layout_marginRight="114dp"
android:layout_marginBottom="464dp"
android:ems="10"
android:hint="EmailId"
android:inputType="textPersonName" />
<Button
android:id="@+id/signUpBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="168dp"
android:layout_marginRight="168dp"
android:layout_marginBottom="245dp"
android:text="Sign Up" />
<EditText
android:id="@+id/SignUp_Password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
2
3 Mobile Application Development
android:layout_marginEnd="101dp"
android:layout_marginRight="101dp"
android:layout_marginBottom="385dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword" />
</RelativeLayout>
JAVA-CODE
package com.example.signupapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.regex.Pattern;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
email_Sign=(EditText)findViewById(R.id.SignUp_email);
password_Sign=(EditText)findViewById(R.id.SignUp_Password);
signUp_btn =(Button)findViewById(R.id.signUpBtn);
signUp_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = email_Sign.getText().toString();
String password = password_Sign.getText().toString();
if(!isValidPassword(password)) {
Toast.makeText(MainActivity.this,"Password doesn't match
rules",Toast.LENGTH_SHORT).show();
return;
}
Intent intent = new Intent(MainActivity.this,loginActivity.class);
intent.putExtra("email",email);
intent.putExtra("password",password);
3
4 Mobile Application Development
startActivity(intent);
}
});
}
Pattern lowerCase= Pattern.compile("^.*[a-z].*$");
Pattern upperCase=Pattern.compile("^.*[A-Z].*$");
Pattern number = Pattern.compile("^.*[0-9].*$");
Pattern special_Chara = Pattern.compile("^.*[^a-zA-Z0-9].*$");
private Boolean isValidPassword(String password){
if(password.length()<8) {
return false;
}
if(!lowerCase.matcher(password).matches()) {
return false;
}
if(!upperCase.matcher(password).matches()) {
return false;
}
if(!number.matcher(password).matches()) {
return false;
}
if(!special_Chara.matcher(password).matches()) {
return false;
}
return true;
}
}
4
5 Mobile Application Development
XML-CODE
<?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"
tools:context=".loginActivity">
<TextView
android:id="@+id/loginTextView"
android:layout_width="225dp"
android:layout_height="45dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="113dp"
android:layout_marginRight="113dp"
android:layout_marginBottom="544dp"
android:text="Login"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
tools:layout_editor_absoluteX="143dp" />
<EditText
android:id="@+id/passEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="124dp"
android:layout_marginRight="124dp"
android:layout_marginBottom="380dp"
android:ems="10"
android:hint="password"
android:inputType="textPassword" />
<Button
android:id="@+id/loginBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
5
6 Mobile Application Development
android:layout_alignParentBottom="true"
android:layout_marginEnd="218dp"
android:layout_marginRight="218dp"
android:layout_marginBottom="263dp"
android:text="Login" />
<EditText
android:id="@+id/EmaileditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="127dp"
android:layout_marginRight="127dp"
android:layout_marginBottom="455dp"
android:ems="10"
android:hint="Email ID"
android:inputType="textPersonName" />
</RelativeLayout>
JAVA-CODE
package com.example.signupapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class loginActivity extends AppCompatActivity {
EditText emailEditText,passwordEditText;
Button login_btn;
int counter=2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login2);
emailEditText=(EditText)findViewById(R.id.EmaileditText);
passwordEditText=(EditText)findViewById(R.id.passEditText);
login_btn=(Button)findViewById(R.id.loginBtn);
String registeredEmail = getIntent().getStringExtra("email");
String registeredPassword= getIntent().getStringExtra("password");
login_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
6
7 Mobile Application Development
JAVA-CODE
package com.example.signupapplication;
import
androidx.appcompat.app.AppCompatActivit
y; import android.os.Bundle;
public class loginsuccessActivity extends
AppCompatActivity {@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loginsuc
cess);
}
}
Program 3
Create a SIGN Up activity with Username and Password. Validation of password
should happen based on the following rules:
1. Password should contain uppercase and lowercase letters.
2. Password should contain letters and numbers.
3. Password should contain special characters.
4. Minimum length of the password (the default value is 8).
On successful SIGN UP proceed to the next Login activity. Here the user should
SIGN IN using the Username and Password created during signup activity. If the
Username and Password are matched then navigate to the next activity which
displays a message saying “Successful Login” or else display a toast message saying
“Login Failed”. The user is given only two attempts and after that display a toast
message saying “Failed Login Attempts” and disable the SIGN IN button. Use Bundle
to transfer information from one activity to another.
Activity_main.xml
<TextView android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:text="SIGN UP"
android:textColor="@android:color/background_dark"
android:textSize="22dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginTop="50dp"
android:text="USERNAME"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<TextView android:id="@+id/textView4"
android:layout_width="82dp" android:layout_height="34dp"
android:layout_marginTop="50dp"
android:text="PASSWORD"
app:layout_constraintStart_toStartOf="@+id/textView3"
app:layout_constraintTop_toBottomOf="@+id/textView3" />
<EditText android:id="@+id/txt_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_marginEnd="10dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="@+id/textView3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView3"
app:layout_constraintTop_toTopOf="@+id/textView3" />
<EditText
android:id="@+id/txt_password"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_marginTop="26dp"
android:ems="10"
android:inputType="textPassword"
app:layout_constraintEnd_toEndOf="@+id/txt_username"
app:layout_constraintStart_toStartOf="@+id/txt_username"
app:layout_constraintTop_toBottomOf="@+id/txt_username" />
<Button android:id="@+id/btn_signup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="Sign Up"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txt_password" />
</androidx.constraintlayout.widget.ConstraintLayout>
Activity_login.xml
<TextView android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="Login" android:textSize="22dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginTop="50dp"
android:text="Username"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView7" />
<EditText
android:id="@+id/txt_login_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="@+id/textView9"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView9"
app:layout_constraintTop_toTopOf="@+id/textView9" />
<TextView android:id="@+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginTop="50dp"
android:text="PASSWORD"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView9" />
<EditText android:id="@+id/txt_login_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword"
app:layout_constraintEnd_toEndOf="@+id/txt_login_username"
app:layout_constraintStart_toStartOf="@+id/txt_login_username"
app:layout_constraintTop_toTopOf="@+id/textView10" />
<Button android:id="@+id/btn_login_signin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="Login"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txt_login_password" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.parta.program3;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
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 MainActivityextends AppCompatActivity implements View.OnClickListener
{
EditTexttxt Username;
EditTexttxt Password;
Button btnSignup;
String regularExpression="^(?=.*[A-Z])(?=.*[a-z])(?=.*\\d)(?=.*[@$!])[A-
Za-z\\d@$!]{8,}$";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtUsername=(EditText)findViewById(R.id.txt_username);
txtPassword=(EditText)findViewById(R.id.txt_password);
btnSignup=(Button)findViewById(R.id.btn_signup);
btnSignup.setOnClickListener(this);
}
public void onClick(View v)
{
String username=txtUsername.getText().toString();
String password=txtPassword.getText().toString();
if(validatePassword(password)) {
Bundle bundle = new Bundle();
bundle.putString("user", username);
bundle.putString("Lab@2020", password);
Intent it = new Intent(this, LoginActivity.class);
it.putExtra("data", bundle);
startActivity(it); }
}
}
LoginActivity.java
package com.example.parta.program3;
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;
public class LoginActivityextends AppCompatActivity implements
View.OnClickListener
{
EditTexttxt LoginUsername;
EditTexttxt LoginPassword;
Button btnLogin;
String user,pass;
int count=0;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
txtLoginUsername=(EditText) findViewById(R.id.txt_login_username);
txtLoginPassword=(EditText) findViewById(R.id.txt_login_password);
btnLogin=(Button)findViewById(R.id.btn_login_signin);
btnLogin.setOnClickListener(this);
Bundle bundle=getIntent().getBundleExtra("data");
user=bundle.getString("rnsit");
pass=bundle.getString("Lab@2020");
}
if(user.equals(user1)&&pass.equals(pass1))
{
Toast.makeText(this,"Login Successful" ,Toast.LENGTH_LONG).show();
}
else
{
count++;
if(count==3)
{
btnLogin.setEnabled(false);Toast.makeText(this, "Failed Login Attempts"
,Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(this,"Login Failed "+count ,Toast.LENGTH_LONG).show();
}
}
}
}
Sample Output
Program 4
Develop an application to set an image as wallpaper. On click of a button,
the wallpaper image should start to change randomly every 30 seconds.
Activity_main.xml
<Button
android:id="@+id/btn_start_change_wallpaper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click here to Change Wallpaper" />
</LinearLayout>
MainActivity.java
package com.example.program5;
import androidx.appcompat.app.AppCompatActivity;
import android.app.WallpaperManager;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.util.Timer;
import java.util.TimerTask;
};
int i=0;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main)
btnChangeWallpaper=(Button) findViewById(R.id.btn_start_change_wallpaper);
btnChangeWallpaper.setOnClickListener(this);
}
@Override
public void onClick(View view)
{
if(!running)
{
new Timer().schedule (new MyTimer(),0,3000);
running=true;
}
}
class MyTimer extends TimerTask {
if(i==12) i=1;
if(i==11) i=2;
if(i==10) i=3;
if(i==9) i=4;
if(i==8) i=5;
if(i==7) i=6;
if(i==6) i=7;
if(i==5) i=8;
if(i==4) i=9;
if(i==3) i=10;
wallpaperManager.setBitmap(BitmapFactory.decodeResource(getResources(),
imagesArray[i]));
i++;
} catch (Exception e)
{ }
}
}}
AndriodManifest.xml