MAD LabManual ISE
MAD LabManual ISE
MAD LabManual ISE
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.
In this android program XML layout code has to be edited according to the user need and
JAVA code doesn’t require any changes.
To create an android application of visiting card in xml layout add on TextViews(for
displaying Name,title,phone number, address, email/fax) and ImageView (logo) from the
Layout Palette.
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"
android:background="@color/teal_200"
tools:context=".MainActivity">
<TextView
android:id="@+id/CardName"
android:layout_width="261dp"
android:layout_height="59dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="32dp"
android:layout_marginLeft="32dp"
android:layout_marginTop="28dp"
android:layout_marginEnd="118dp"
android:layout_marginRight="118dp"
android:layout_marginBottom="516dp"
android:text="MIT Mysore"
android:textSize="36sp" />
<ImageView
android:id="@+id/imageView"
android:layout_width="157dp"
android:layout_height="110dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="9dp"
android:layout_marginRight="9dp"
android:layout_marginBottom="484dp"
app:srcCompat="@drawable/mitlogo" />
<View
android:id="@+id/view"
android:layout_width="wrap_content"
android:layout_height="4dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="466dp"
android:background="#4444" />
<TextView
android:id="@+id/Name"
android:layout_width="221dp"
android:layout_height="32dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="142dp"
android:layout_marginLeft="142dp"
android:layout_marginTop="202dp"
android:layout_marginEnd="48dp"
android:layout_marginRight="48dp"
android:layout_marginBottom="369dp"
android:text="@string/chitra_c"
android:textSize="30sp" />
<TextView
android:id="@+id/Designation"
android:layout_width="326dp"
android:layout_height="41dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="77dp"
android:layout_marginLeft="77dp"
android:layout_marginTop="277dp"
android:layout_marginEnd="9dp"
android:layout_marginRight="9dp"
android:layout_marginBottom="286dp"
android:text="@string/assistant_professor"
android:textSize="30sp" />
<TextView
android:id="@+id/Branch"
android:layout_width="404dp"
android:layout_height="39dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="3dp"
android:layout_marginLeft="3dp"
android:layout_marginTop="358dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="206dp"
android:text="@string/information_science_and_engineering"
android:textSize="24sp" />
<TextView
android:id="@+id/Email"
android:layout_width="364dp"
android:layout_height="46dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="26dp"
android:layout_marginLeft="26dp"
android:layout_marginTop="423dp"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="135dp"
android:text="@string/emailid_chitra_ise_mitmysore_in"
android:textSize="24sp" />
<TextView
android:id="@+id/Phoneno"
android:layout_width="353dp"
android:layout_height="45dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="45dp"
android:layout_marginLeft="45dp"
android:layout_marginTop="503dp"
android:layout_marginEnd="13dp"
android:layout_marginRight="13dp"
android:layout_marginBottom="55dp"
android:text="Phone Number: 9845454545"
android:textSize="24sp" />
</RelativeLayout>
JAVA-CODE(No Change Required)
package com.example.lab1program;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
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.
To create an Android application of Simple Calculator use input controls like
Button,TextView and EditText Views.
XML-CODE:
<?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"
android:background="#8BC34A"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/simple_calculator"
android:textColor="#E91E63"
android:textColorHighlight="#CDDC39"
android:textSize="36sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.457"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.091" />
<EditText
android:id="@+id/edit1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autofillHints=""
android:ems="10"
android:hint="@string/enter_first_number"
android:inputType="number"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.411"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.227" />
<EditText
android:id="@+id/edit2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autofillHints=""
android:ems="10"
android:hint="@string/enter_second_number"
android:inputType="number"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.411"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.365" />
<Button
android:id="@+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/add"
android:onClick="toAdd"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.182"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.534" />
<Button
android:id="@+id/btnSub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/subtract"
android:onClick="toSub"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.64"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.534" />
<Button
android:id="@+id/btnMul"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/multiply"
android:onClick="toMultiply"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.182"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.705" />
<Button
android:id="@+id/btnDiv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/division"
android:onClick="toDivide"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.674"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.689" />
<TextView
android:id="@+id/tvAnswer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Result: "
android:textColor="#E91E63"
android:textSize="40sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.451"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.858" />
</androidx.constraintlayout.widget.ConstraintLayout>
JAVA-CODE:
package com.example.lab2calciprogram;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
EditText n1,n2;
TextView t1;
Button b1,b2,b3,b4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
n1=findViewById(R.id.edit1);
n2=findViewById(R.id.edit2);
t1=findViewById(R.id.tvAnswer);
}
public void toAdd(View view) {
int a1=Integer.parseInt(n1.getText().toString());
int a2=Integer.parseInt(n2.getText().toString());
int res=a1+a2;
t1.setText("Result="+res);
}
<TextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sign_up_activity"
android:textColor="#FF5722"
android:textSize="46sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.071" />
<EditText
android:id="@+id/etUsername1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autofillHints=""
android:ems="10"
android:gravity="center_horizontal"
android:hint="@string/enter_your_email_id"
android:inputType="textPersonName"
android:textAlignment="center"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.408"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.292" />
<EditText
android:id="@+id/etPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autofillHints=""
android:ems="10"
android:gravity="center_horizontal"
android:hint="@string/enter_your_password"
android:inputType="textPassword"
android:textAlignment="center"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.408"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.439" />
<Button
android:id="@+id/btnSignup"
android:layout_width="102dp"
android:layout_height="36dp"
android:background="@drawable/btn_signup"
android:text="@string/sign_up"
android:textSize="18sp"
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/etUsername1"
app:layout_constraintVertical_bias="0.398" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.lab3loginactivity;
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.TextView;
import android.widget.Toast;
import java.util.regex.Pattern;
EditText eUsername;
EditText ePassword;
Button btnSignUP;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
eUsername=findViewById(R.id.etUsername1);
ePassword=findViewById(R.id.etPassword);
btnSignUP=findViewById(R.id.btnSignup);
btnSignUP.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email=eUsername.getText().toString();
String password=ePassword.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);
startActivity(intent);
}
});
}
Pattern lowerCase = Pattern.compile("^.*[a-z].*$");
Pattern upperCase = Pattern.compile("^.*[A-Z].*$");
Pattern number = Pattern.compile("^.*[0-9].*$");
Pattern specialCharacter = Pattern.compile("^.*[^a-zA-Z0-9].*$");
private Boolean isValidPassword(String password) {
// Checks if password length is less than 8
if (password.length() < 8) {
return false;
}
// Returns false if password doesn't contain a lower case character
if (!lowerCase.matcher(password).matches()) {
return false;
}
// Returns false if password doesn't contain an upper case character
if (!upperCase.matcher(password).matches()) {
return false;
}
// Returns false if password doesn't contain a number
if (!number.matcher(password).matches()) {
return false;
}
// Returns false if password doesn't contain a special character
if (!specialCharacter.matcher(password).matches()) {
return false;
}
return true;
}
}
Activity_login.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"
android:background="@color/white"
tools:context=".LoginActivity">
<TextView
android:id="@+id/tvLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/login_activity"
android:textSize="36sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.099" />
<EditText
android:id="@+id/etUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/enter_the_username"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.525"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvLogin"
app:layout_constraintVertical_bias="0.169"
android:autofillHints="" />
<EditText
android:id="@+id/etPassword1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/enter_password"
android:inputType="textPassword"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.525"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/etUsername"
app:layout_constraintVertical_bias="0.175"
android:autofillHints="" />
<Button
android:id="@+id/etbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sign_in"
android:textSize="24sp"
android:textStyle="bold"
app:backgroundTint="#3F51B5"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/etPassword1"
app:layout_constraintVertical_bias="0.289" />
<TextView
android:id="@+id/tvAttemptInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Number of Attempts: 2"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.869" />
</androidx.constraintlayout.widget.ConstraintLayout>
LoginActivity.java
package com.example.lab3loginactivity;
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.TextView;
import android.widget.Toast;
TextView etvAttemptInfo;
EditText Username;
EditText Password;
Button login;
private int counter=2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Username=findViewById(R.id.etUsername);
Password=findViewById(R.id.etPassword1);
login=findViewById(R.id.etbtn);
etvAttemptInfo=findViewById(R.id.tvAttemptInfo);
String registeredEmail=getIntent().getStringExtra("email");
String registeredPassword=getIntent().getStringExtra("password");
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String eName=Username.getText().toString();
String ePassword=Password.getText().toString();
if(eName.isEmpty() ||ePassword.isEmpty())
{
Toast.makeText(LoginActivity.this, "Please enter all
the details correctly",Toast.LENGTH_SHORT).show();
}
else
{
if((registeredEmail.equals(eName) &&
registeredPassword.equals(ePassword)))
{
Toast.makeText(LoginActivity.this, "Login
successful!",Toast.LENGTH_SHORT).show();
//Add New Activity
Intent intent= new Intent(LoginActivity.this,
HomePageActivity.class);
startActivity(intent);
}
else{
counter--;
Toast.makeText(LoginActivity.this, "Incorrect
credentials entered/Login Failed",Toast.LENGTH_SHORT).show();
etvAttemptInfo.setText("No of Attempts:"+counter);
if(counter==0)
{
login.setEnabled(false);
}
}
}
}
});
}
}
Activity_login_success.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"
android:background="@color/teal_200"
tools:context=".HomePageActivity">
<TextView
android:id="@+id/tvLoginSuccess"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/login_succesfull"
android:textColor="#0E5A53"
android:textSize="36sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
LoginSuccessActivity.java
ackage com.example.lab3loginactivity;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
TextView tv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
tv1=findViewById(R.id.tvLoginSuccess);
}
}
OUTPUT
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.
First, create the android application as discussed in “Create your First Android
Application”. Copy the images and save the images in the drawable folder.
Following is the content of the modified res/layout/activity_main.xml.
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"
android:background="#C5D8D6"
tools:context=".MainActivity">
<TextView
android:id="@+id/tvWallpaper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/changing_wallpaper_application"
android:textColor="#496726"
android:textSize="28sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.242"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.126" />
<Button
android:id="@+id/btnChangeWallpaper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/click_here_to_change_wallpaer"
android:textColor="#092523"
android:textColorHighlight="#689535"
android:textSize="18sp"
app:backgroundTint="@color/teal_200"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.467"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvWallpaper"
app:layout_constraintVertical_bias="0.282" />
</androidx.constraintlayout.widget.ConstraintLayout>
Save five images (jpg format) in the drawable folder. In this example one.jpg,
two.jpg,three.jpg, four.jpg and five.jpg images are saved in drawable folder.
package com.example.lab4;
import androidx.appcompat.app.AppCompatActivity;
import android.app.WallpaperManager;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
Button btn;
WallpaperManager wpm;
Timer mytimer;
Drawable drawable;
int prev=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = findViewById(R.id.btnChangeWallpaper);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
setWallPaper();
}
});
}
OUTPUT
5. Write a program to create an activity with two buttons START and
STOP. On pressing of the START button, the activity must start the
counter by displaying the numbers from One and the counter must keep on
counting until the STOP button is pressed. Display the counter value in a
TextView control.
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"
android:background="@color/teal_200"
tools:context=".MainActivity">
<TextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Counter Application"
android:textSize="36sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.471"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.104" />
<TextView
android:id="@+id/tvValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Counter Value:"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.401"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.361" />
<Button
android:id="@+id/btnstart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.207"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.571" />
<Button
android:id="@+id/btnstop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.773"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.572" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.lab5;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bstart = findViewById(R.id.btnstart);
bstop = findViewById(R.id.btnstop);
txtcounter = findViewById(R.id.tvValue);
bstart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
customHandler.postDelayed(updateTimerThread, 0);
}
});
bstop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
customHandler.removeCallbacks(updateTimerThread);
}
});
}
};
}
OUTPUT:
Program 6: Create two files of XML and JSON type with values for City
Name, Latitude, Longitude, Temperature, and Humidity. Develop an
application to create an activity with two buttons to parse the XML and
JSON files which when clicked should display the data in their respective
layouts side by side.
XML CODE:
<?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"
android:background="#9BED3C"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#4CAF50"
android:text="Parsing Data Application"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.121" />
<Button
android:id="@+id/btnJSON"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Parse JSON data"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.347" />
<Button
android:id="@+id/btnXML"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Parse XMLdata"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.672" />
</androidx.constraintlayout.widget.ConstraintLayout>
JAVA CODE:
package com.example.lab6parsedata;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
Button btnxml;
Button btnJSON;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnxml=findViewById(R.id.btnXML);
btnJSON=findViewById(R.id.btnJSON);
btnxml.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,
ViewActivity.class);
intent.putExtra("mode",1);
startActivity(intent);
}
});
btnJSON.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,
ViewActivity.class);
intent.putExtra("mode",2);
startActivity(intent);
}
});
}
}
}
}
<TextView
android:id="@+id/ParseJSONdata"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.232"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.464" />
<TextView
android:id="@+id/Parsexmldata"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.783"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.462" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="JSON Data"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.173"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.152" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="XML Data"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.775"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.152" />
</androidx.constraintlayout.widget.ConstraintLayout>
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
int mode;
TextView JSONdata,XMLdata;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);
JSONdata=findViewById(R.id.ParseJSONdata);
XMLdata=findViewById(R.id.Parsexmldata);
mode=getIntent().getIntExtra("mode",0);
if(mode==1)
{
parseXML();
}
if(mode==2){
parseJSON();
}
}
public void parseXML(){
try {
InputStream in=getAssets().open("input.xml");
DocumentBuilderFactory
documentBuilderFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder
documentBuilder=documentBuilderFactory.newDocumentBuilder();
Document document=documentBuilder.parse(in);
NodeList cityList=document.getElementsByTagName("City");
for(int i=0; i<cityList.getLength();i++)
{
Node c=cityList.item(i);
if(c.getNodeType()==Node.ELEMENT_NODE)
{
Element city=(Element)c;
NodeList cityDetailList=city.getChildNodes();
for(int j=0;j<cityDetailList.getLength();j++){
Node n=cityDetailList.item(j);
if(n.getNodeType()==Node.ELEMENT_NODE){
Element cityDetail=(Element)n;
String tagValue=cityDetail.getTagName();
String value=cityDetail.getTextContent();
Log.e("data","parseXML:"+value);
XMLdata.append(tagValue+"-"+value+"\n");
XMLdata.append("\n");
}
}
; }
}
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
}
try{
InputStream in=getAssets().open("input.json");
int size=in.available();
byte buffer[]=new byte[size];
in.read(buffer);
strData=new String(buffer);
Log.e("data","parseJSON"+strData);
JSONObject cityObject=jsonObject.getJSONObject("City");
String cityName=cityObject.getString("City-Name");
String latitude=cityObject.getString("Latitude");
String longitude=cityObject.getString("Longitude");
String temperature=cityObject.getString("Temperature");
String humidity=cityObject.getString("Humidity");
Log.e("data","parseJSON"+cityName);
JSONdata.setText(cityName+"\n");
JSONdata.append(latitude+"\n");
JSONdata.append(longitude+"\n");
JSONdata.append(temperature+"\n");
JSONdata.append(humidity+"\n");
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
OUTPUT:
Program-7: Develop a simple application with one EditText so that the user
can write some text in it. Create a button called “Convert Text to Speech”
that converts the user input text into voice.
XML-CODE:
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text_to_speech_application"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.186" />
<EditText
android:id="@+id/edit1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autofillHints=""
android:ems="10"
android:hint="@string/enter_text"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.41" />
<Button
android:id="@+id/btnClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="ToConvet"
android:text="@string/covert_text_to_speech"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.677" />
</androidx.constraintlayout.widget.ConstraintLayout>
JAVA CODE:
package com.example.lab7;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.Locale;
EditText et1;
Button btn;
TextToSpeech t1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1=findViewById(R.id.edit1);
btn=findViewById(R.id.btnClick);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String s=et1.getText().toString();
Toast.makeText(getBaseContext(),s,Toast.LENGTH_LONG).show();
int speech=t1.speak(s,TextToSpeech.QUEUE_FLUSH,null);
}
});
}
}
OUTPUT:
8. Create an activity like a phone dialer with CALL and SAVE buttons. On pressing the
CALL button, it must call the phone number and on pressing the SAVE button it must
save the number to the phone contacts.
XML CODE:
<EditText
android:id="@+id/display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="phone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.22" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.418" />
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.095"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.418" />
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.86"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.418" />
<Button
android:id="@+id/btn5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="136dp"
android:text="5"
app:layout_constraintEnd_toStartOf="@+id/btn6"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/btn4"
app:layout_constraintTop_toBottomOf="@+id/display" />
<Button
android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="136dp"
android:text="4"
app:layout_constraintEnd_toStartOf="@+id/btn5"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/display" />
<Button
android:id="@+id/btn6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="136dp"
android:text="6"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/btn5"
app:layout_constraintTop_toBottomOf="@+id/display" />
<Button
android:id="@+id/btn7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="216dp"
android:text="7"
app:layout_constraintEnd_toStartOf="@+id/btn8"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/display" />
<Button
android:id="@+id/btn8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="216dp"
android:text="8"
app:layout_constraintEnd_toStartOf="@+id/btn9"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/btn7"
app:layout_constraintTop_toBottomOf="@+id/display" />
<Button
android:id="@+id/btn9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="216dp"
android:text="9"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/btn8"
app:layout_constraintTop_toBottomOf="@+id/display" />
<Button
android:id="@+id/btnstar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="292dp"
android:text="*"
app:layout_constraintEnd_toStartOf="@+id/btn0"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/display" />
<Button
android:id="@+id/btn0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="292dp"
android:text="0"
app:layout_constraintEnd_toStartOf="@+id/btnhash"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/btnstar"
app:layout_constraintTop_toBottomOf="@+id/display" />
<Button
android:id="@+id/btnhash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="292dp"
android:text="#"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/btn0"
app:layout_constraintTop_toBottomOf="@+id/display" />
<Button
android:id="@+id/btnSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="364dp"
android:text="Save"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.114"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/display"
app:layout_constraintVertical_bias="0.05" />
<Button
android:id="@+id/btnCall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="200dp"
android:text="Call"
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/display"
app:layout_constraintVertical_bias="0.896" />
<Button
android:id="@+id/btnClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="364dp"
android:text="X"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.86"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/display"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Phone Dailer"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/btnCall"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.057" />
</androidx.constraintlayout.widget.ConstraintLayout>
JAVA CODE
package com.example.lab8;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
EditText display;
Button btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn0;
Button btnSave,btnCall,btnStar,btnHash,btnRemove;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn0=findViewById(R.id.btn0);
btn1=findViewById(R.id.btn1);
btn2=findViewById(R.id.btn2);
btn3=findViewById(R.id.btn3);
btn4=findViewById(R.id.btn4);
btn5=findViewById(R.id.btn5);
btn6=findViewById(R.id.btn6);
btn7=findViewById(R.id.btn7);
btn8=findViewById(R.id.btn8);
btn9=findViewById(R.id.btn9);
btnSave=findViewById(R.id.btnSave);
btnCall=findViewById(R.id.btnCall);
btnStar=findViewById(R.id.btnstar);
btnHash=findViewById(R.id.btnhash);
btnRemove=findViewById(R.id.btnClear);
display=findViewById(R.id.display);
btn0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
display.append("0");
}
});
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
display.append("1");
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
display.append("2");
}
});
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
display.append("3");
}
});
btn4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
display.append("4");
}
});
btn5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
display.append("5");
}
});
btn6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
display.append("6");
}
});
btn7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
display.append("7");
}
});
btn8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
display.append("8");
}
});
btn9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
display.append("9");
}
});
btnStar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
display.append("*");
}
});
btnHash.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
display.append("#");
}
});
btnCall.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String data=display.getText().toString();
Intent intent= new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:"+data));
startActivity(intent);
}
});
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String data = display.getText().toString();
intent.setType(ContactsContract.RawContacts.CONTENT_TYPE);
intent.putExtra(ContactsContract.Intents.Insert.NAME,"Unknown");
intent.putExtra(ContactsContract.Intents.Insert.PHONE,data);
startActivity(intent);
}
});
btnRemove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String data = display.getText().toString();
if(data.length()>0){
String val = data.substring(0,data.length()-1);
display.setText(val);
}
else{
display.setText("");
}
}
});
}
}
OUTPUT:
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="152dp"
android:layout_marginBottom="564dp"
android:text="ClipBoard"
android:textSize="36sp" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="127dp"
android:layout_marginBottom="496dp"
android:ems="10"
android:hint="Enter the text here"
android:inputType="textPersonName"
android:text="" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="122dp"
android:layout_marginBottom="411dp"
android:ems="10"
android:hint="Copied Text"
android:inputType="textPersonName"
android:text="" />
<Button
android:id="@+id/copy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="279dp"
android:onClick="copy"
android:layout_marginBottom="312dp"
android:text="Copy" />
<Button
android:id="@+id/paste"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="92dp"
android:onClick="paste"
android:layout_marginBottom="313dp"
android:text="Paste" />
</RelativeLayout>
JAVA-CODE:
import androidx.appcompat.app.AppCompatActivity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
ClipboardManager cbm;
ClipData cd;
EditText e1,e2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1 = (EditText)findViewById(R.id.editText1);
e2 = (EditText)findViewById(R.id.editText2);
cbm =(ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
}
public void copy(View V){
String text = e1.getText().toString();
cd = ClipData.newPlainText("text",text);
cbm.setPrimaryClip(cd);
}
public void paste(View V){
ClipData cd2 = cbm.getPrimaryClip();
ClipData.Item item = cd2.getItemAt(0);
String copied = item.getText().toString();
e2.setText(copied);
}
}
OUTPUT:
<SeekBar
android:id="@+id/seekBar"
android:layout_width="255dp"
android:layout_height="28dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="66dp"
android:layout_marginBottom="311dp" />
<ImageButton
android:id="@+id/rewind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="256dp"
android:layout_marginBottom="219dp"
app:srcCompat="@android:drawable/ic_media_rew" />
<ImageButton
android:id="@+id/playButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="168dp"
android:layout_marginBottom="223dp"
app:srcCompat="@android:drawable/ic_lock_power_off" />
<ImageButton
android:id="@+id/forward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="79dp"
android:layout_marginBottom="220dp"
app:srcCompat="@android:drawable/ic_media_ff" />
</RelativeLayout>
Java CODE:
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
playButton = findViewById(R.id.playButton);
forward = findViewById(R.id.forward);
rewind = findViewById(R.id.rewind);
seekbar = findViewById(R.id.seekBar);
prepareMediaPlayer();
seekbar.setMax(100);
playButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mediaPlayer.isPlaying()){
handler.removeCallbacks(updater);
mediaPlayer.pause();
}else {
mediaPlayer.start();
updateSeekBar();
}
}
});
forward.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mediaPlayer.getDuration()>mediaPlayer.getCurrentPosition() +
10000){
mediaPlayer.seekTo(mediaPlayer.getCurrentPosition() + 10000);
updateSeekBar();
}
}
});
rewind.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mediaPlayer.getCurrentPosition()>10000){
mediaPlayer.seekTo(mediaPlayer.getCurrentPosition() - 10000);
updateSeekBar();
}
}
});
seekbar.setOnTouchListener((v, event) -> {
SeekBar s = (SeekBar) v;
int position = (mediaPlayer.getDuration()/100)*s.getProgress();
mediaPlayer.seekTo(position);
return false;
});
mediaPlayer.setOnCompletionListener(new
MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
seekbar.setProgress(0);
mediaPlayer.reset();
prepareMediaPlayer();
}
});
}
private void prepareMediaPlayer (){
try {
mediaPlayer =MediaPlayer.create(this,R.raw.poc);
}catch (Exception e){
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
private Runnable updater = new Runnable() {
@Override
public void run() {
updateSeekBar();
}
};
private void updateSeekBar(){
if(mediaPlayer.isPlaying()){
seekbar.setProgress((int)((float)mediaPlayer.getCurrentPosition()/
mediaPlayer.getDuration()*100));
handler.postDelayed(updater,1000);
}
}
}