0% found this document useful (0 votes)
36 views81 pages

Manual1 1

The document provides information about the vision, mission and objectives of P.A College of Engineering and its Computer Science and Engineering department. It discusses the vision of the institute which is to provide excellence in technological, management and research education. The mission is to provide career-oriented education to produce competent engineers and managers with strong ethical values. It also outlines the vision, mission, program education objectives and program outcomes of the CSE department. Finally, it provides steps to create a basic visiting card application in Android Studio.

Uploaded by

farishf24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views81 pages

Manual1 1

The document provides information about the vision, mission and objectives of P.A College of Engineering and its Computer Science and Engineering department. It discusses the vision of the institute which is to provide excellence in technological, management and research education. The mission is to provide career-oriented education to produce competent engineers and managers with strong ethical values. It also outlines the vision, mission, program education objectives and program outcomes of the CSE department. Finally, it provides steps to create a basic visiting card application in Android Studio.

Uploaded by

farishf24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 81

P.

A College of Engineering

Mobile Application Development Manual


[18CSMP68]
Prof. Tuba Nageen Nakhuda
Vision and Mission of the Institute
Vision:
PACE is envisaged as a centre of evolution for excellence in Technological,
Management and Research Education. The institution aspires to spread universal knowledge
through villages and cities enabling expansion of human resources.

Mission:
To provide career-oriented professional education to produce technically competent
engineers and managers with moral and ethical values.
To foster and promote an effective learning environment in the campus to be recognized
as place that encourages excellence and diversity in thoughts and endeavour.
To provide research and intellectual resources to address problems facing the industry
and the society while advancing the scopes of multidisciplinary applications.

Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 2


Vision and Mission of the Department

Vision:
To be a center of excellence in providing quality education of global standards and be
a pioneer in spreading the knowledge and innovative ideas of Computer Science and
Engineering for catering the industrial demands and societal needs.

Mission:
 To develop competent innovative professionals having effective communication skills
with proficiency in real world problem solving with leadership traits.
 To impart quality and value based education to reach the expectations of all the
stakeholders through an ever evolving, effective and creative teaching-learning process.
 To undertake salient research works for the establishment of center of excellence in the
field of Computer Science and Engineering and multi-disciplinary areas.

Program Education Objectives (PEO’s):


 PEO1.Graduates of the program will analyze, design and solve problems related to
Computer Science and Engineering and possess adaptability to changes in technology
by self-learning.
 PEO2.Graduates are provided with an educational foundation that prepares them for
leadership roles in their diverse career paths and to pursue higher education.
 PEO3. Graduates of the program are capable to deliver the software product for various
real life problems within the scheduled time.
 PEO4. Graduates must develop professional and communication skills for their
successful professional career.

Program Outcomes (PO’s):


Engineering Graduates will be able to:
 PO1. Engineering Knowledge
 PO2. Problem Analysis
 PO3. Design/Development of Solutions
 PO4. Conduct investigations of complex problems
 PO5. Modern tool usage

Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 3


 PO6. The engineer and society
 PO7. Environment and sustainability
 PO8. Ethics
 PO9. Individual and team work
 PO10. Communication
 PO11. Project management and finance
 PO12. Life-long learning

Program Specific Outcomes (PSO’s):


 PSO1. Analyze, design, develop and optimize solutions in Java, C++, .NET
Technology and Android based applications.
 PSO2. Apply concepts in core areas of Computer Science – Algorithms and Data
Structures, Operating Systems, Database Management Systems, Computer Networks,
Computer Architecture and Software Engineering to solve technical issues.

Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 4


Steps to open Android Studio:
Select Android Studio from start.
File-> New-> New Project -> Select Empty Activity/ Empty View Activity -> Click on
Next -> Give an application name -> Select language ‘Java’ and Tick the Tick the ‘Use
legacy android.studio Libraries’ checkbox -> Select the minimum SDK as Android 4.0
or 5.1 -> Finish.
Select an emulator and click on Run ‘Green Play Button’ to check if the Emulator is
working.

Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 5


I. Visiting Card
Program: 1
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.

Layout:
1. Contents in “RelativeLayout”
Company Name
Image
2. Contents in “LinearLayout”
Name:
Job Title:
Phone Number:
Address:
Email id:

Steps to create Visiting Card:


1. Firstly, Create an Application by Name “VisitingCardApp”
2. Go to xml code of design change the layout to “LinearLayout” and “RelativeLayout”
inside “LinearLayout”.
3. Height of Relative Layout be 200dp.

Note: Drag Company Name (TextView) and Image (ImageView) in RelativeLayout i.e,
layout appearing at the top.

Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 6


Name, Job Title, Address, Email id, Mobile Number in Linear Layout i.e, layout
appearing at the bottom

4. Add TextView component in “RelativeLayout” and change the following properties:


 Size: 38dp
 Text: PACE
 Align left top
5. Download the logo and copy the same in res->drawable folder, name should be in small
letters with no special characters and to be placed in “RelativeLayout”
6. In xml code of imageview change srcCompat=”@drawable/logo”
• Align right top
7. Add View component in “LinearLayout” and change the following properties:
• Height: 4dp
• Background: “#4444” (black color)
8. Add TextView component in “LinearLayout” and change the following properties:
• Size: 20dp
• Text: Tuba (Name)
• Style: Bold
• Align center
9. Add TextView component in “LinearLayout” and change the following properties:
• Size: 24sp
• Text: Asst. Prof. (Job Title)
• Align center
10. Add TextView component in “LinearLayout” and change the following properties:
• Size: 24sp
• Text: Phone-123456789 (Mobile Number)
11. Add TextView component in “LinearLayout” and change the following properties:
• Size: 24dp
• Text: Near Mangalore University, Kudla, Konaje Proper, Karnataka 574153
• Align: center
12. Add TextView component in “LinearLayout” and change the following properties:
• Size: 24sp
• Text: [email protected] (Email id)

Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 7


• Align: center
13. Click on Run (Green Colour Play Button) to start the emulator.

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

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="200dp">

<TextView
android:id="@+id/textView"
android:layout_width="200dp"
android:layout_height="71dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="false"
android:layout_marginLeft="31dp"
android:layout_marginTop="85dp"
android:layout_marginEnd="180dp"
android:layout_marginRight="180dp"
android:layout_marginBottom="39dp"
android:text="P.A. COLLEGE OF ENGINEERING"
Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 8
android:textAlignment="center"
android:textColor="#020202"
android:textSize="24sp"
android:textStyle="bold" />

<ImageView
android:id="@+id/imageView"
android:layout_width="148dp"
android:layout_height="113dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="29dp"
android:layout_marginRight="29dp"
android:layout_marginBottom="24dp"
app:srcCompat="@drawable/pacelogo" />

</RelativeLayout>

<View
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="@color/black" />

<TextView
android:id="@+id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="20dp"
android:text="Tuba Nageen Nakhuda"
android:textAlignment="center"
android:textColor="#C68A8A"
android:textSize="16sp"
Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 9
android:textStyle="normal|bold" />

<TextView
android:id="@+id/textView6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="20dp"
android:text="Asst. Prof. "
android:textColor="#BD9393"
android:textSize="16sp"
android:textStyle="normal|bold"
android:textAlignment="center"
/>

<TextView
android:id="@+id/textView7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="20dp"
android:text="+9113456789"
android:textColor="#BD9393"
android:textSize="16sp"
android:textStyle="normal|bold"
android:textAlignment="center"/>

<TextView
android:id="@+id/textView8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="20dp"
android:text="Near Mangalore University, Kudla, Konaje Proper, Karnataka
574153"
android:textAlignment="center"
android:textColor="#BD9393"
Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 10
android:textSize="16sp"
android:textStyle="normal|bold" />

<TextView
android:id="@+id/textView9"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="20dp"
android:text="[email protected]"
android:textAlignment="center"
android:textColor="#BD9393"
android:textSize="16sp"
android:textStyle="normal|bold" />
</LinearLayout>

MainActivity.java

package com.example.visitingcard;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

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

Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 11


Output:

Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 12


II. Simple Calculator
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.

Layout:
1. Contents in “ConstraintLayout”
 Text: Simple Calculator
 Text: Result
 PlainText: Result
 17 Buttons

Steps to create Simple Calculator:


1. Firstly, Create an Application by Name “Simple Calculator”
2. Go to xml code of design check for “Constraint Layout”
3. Add TextView and write text as “Simple Calculator”, place it at the top and change the
text colour.
4. Add another TextView and write text as “Result” on left below the text “Simple
Calculator” and change the text colour.
5. Add Plaintext to the layout below the result and middle of the screen and write the text
as “Output”.

Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 13


6. Add a button and place it below the plaintext as shown in the above figure, change the
button name as 7 and button id as “btn_7” also change the colour of the button.
7. Add multiple buttons numbering from 0 to 9 and Divide, Float, Multiplication, Addition
and Subtraction, Equals and C to clear the screen.
8. Give colours to the buttons and resize it, so that there are four buttons in one row.
9. After designing xmlns, java file has to be coded to run the buttons and display the
output.

XML File:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
android:id="@+id/btn_clear"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="#9C27B0"
android:text="C"
android:textColor="#502C2C"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.903"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.864" />

Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 14


<Button
android:id="@+id/btn_0"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="#9C27B0"
android:text="0"
android:textColor="#502C2C"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.353"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.74" />

<Button
android:id="@+id/btn_dot"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="#9C27B0"
android:text="."
android:textColor="#502C2C"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.079"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.74" />

<Button
android:id="@+id/btn_add"
android:layout_width="60dp"
Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 15
android:layout_height="60dp"
android:background="#9C27B0"
android:text="+"
android:textColor="#502C2C"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.903"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.74" />

<Button
android:id="@+id/btn_equal"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="#9C27B0"
android:text="="
android:textColor="#502C2C"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.615"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.74" />

<Button
android:id="@+id/btn_5"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="#9C27B0"
android:text="5"
android:textColor="#502C2C"
Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 16
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.353"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.475" />

<Button
android:id="@+id/btn_2"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="#9C27B0"
android:text="2"
android:textColor="#502C2C"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.353"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.609" />

<Button
android:id="@+id/btn_1"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="#9C27B0"
android:text="1"
android:textColor="#502C2C"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.079"
Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 17
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.609" />

<Button
android:id="@+id/btn_sub"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="#9C27B0"
android:text="-"
android:textColor="#502C2C"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.903"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.609" />

<Button
android:id="@+id/btn_3"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="#9C27B0"
android:text="3"
android:textColor="#502C2C"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.615"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.609" />

Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 18


<Button
android:id="@+id/btn_4"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="#9C27B0"
android:text="4"
android:textColor="#502C2C"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.079"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.475" />

<Button
android:id="@+id/btn_mul"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="#9C27B0"
android:text="*"
android:textColor="#502C2C"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.903"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.475" />

<Button
android:id="@+id/btn_6"
android:layout_width="60dp"
android:layout_height="60dp"
Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 19
android:background="#9C27B0"
android:text="6"
android:textColor="#502C2C"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.615"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.475" />

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Simple Calculator"
android:textColor="#E91E63"
android:textSize="24sp"
android:textStyle="bold|italic"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.551"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.022" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Result"
android:textColor="#3F51B5"
android:textSize="20sp"
android:textStyle="bold"
Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 20
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.186"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.116" />

<Button
android:id="@+id/btn_7"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="#9C27B0"
android:text="7"
android:textColor="#502C2C"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.08"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.333" />

<Button
android:id="@+id/btn_8"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="#9C27B0"
android:text="8"
android:textColor="#502C2C"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.353"
app:layout_constraintStart_toStartOf="parent"
Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 21
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.333" />

<Button
android:id="@+id/btn_9"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="#9C27B0"
android:text="9"
android:textColor="#502C2C"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.615"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.333" />

<Button
android:id="@+id/btn_div"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="#9C27B0"
android:text="/"
android:textColor="#502C2C"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.903"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.333" />

<EditText
Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 22
android:id="@+id/TextInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="100dp"
android:layout_marginRight="100dp"
android:layout_marginBottom="548dp"
android:ems="10"
android:inputType="text"
android:text="Input"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</android.support.constraint.ConstraintLayout>

Java File:
MainActivity.java
package com.example.simplecalculator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


Button btn_1,btn_2,btn_3,btn_4,btn_5,btn_6,btn_7,btn_8,btn_9,btn_0;
Button btn_dot,btn_clear,btn_add,btn_mul,btn_sub,btn_div,btn_equal;
EditText tres;

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

Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 23


btn_0=(Button)findViewById(R.id.btn_0);
btn_0.setOnClickListener(this);
btn_1=(Button)findViewById(R.id.btn_1);
btn_1.setOnClickListener(this);
btn_2=(Button)findViewById(R.id.btn_2);
btn_2.setOnClickListener(this);
btn_3=(Button)findViewById(R.id.btn_3);
btn_3.setOnClickListener(this);
btn_4=(Button)findViewById(R.id.btn_4);
btn_4.setOnClickListener(this);
btn_5=(Button)findViewById(R.id.btn_5);
btn_5.setOnClickListener(this);
btn_6=(Button)findViewById(R.id.btn_6);
btn_6.setOnClickListener(this);
btn_7=(Button)findViewById(R.id.btn_7);
btn_7.setOnClickListener(this);
btn_8=(Button)findViewById(R.id.btn_8);
btn_8.setOnClickListener(this);
btn_9=(Button)findViewById(R.id.btn_9);
btn_9.setOnClickListener(this);
btn_dot=(Button)findViewById(R.id.btn_dot);
btn_dot.setOnClickListener(this);
btn_div=(Button)findViewById(R.id.btn_div);
btn_div.setOnClickListener(this);
btn_mul=(Button)findViewById(R.id.btn_mul);
btn_mul.setOnClickListener(this);
btn_add=(Button)findViewById(R.id.btn_add);
btn_add.setOnClickListener(this);
btn_sub=(Button)findViewById(R.id.btn_sub);
btn_sub.setOnClickListener(this);
btn_clear=(Button)findViewById(R.id.btn_clear);
btn_clear.setOnClickListener(this);
btn_equal=(Button)findViewById(R.id.btn_equal);
Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 24
btn_equal.setOnClickListener(this);

tres=(EditText)findViewById(R.id.TextInput);
tres.setText("");
}
@Override
public void onClick(View v) {
if(v.equals(btn_0))
tres.append("0");

if(v.equals(btn_1))
tres.append("1");

if(v.equals(btn_2))
tres.append("2");

if(v.equals(btn_3))
tres.append("3");

if(v.equals(btn_4))
tres.append("4");

if(v.equals(btn_5))
tres.append("5");

if(v.equals(btn_6))
tres.append("6");

if(v.equals(btn_7))
tres.append("7");
if(v.equals(btn_8))
tres.append("8");

if(v.equals(btn_9))
Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 25
tres.append("9");

if(v.equals(btn_dot))
tres.append(".");

if(v.equals(btn_add))
tres.append("+");

if(v.equals(btn_sub))
tres.append("-");

if(v.equals(btn_mul))
tres.append("*");

if(v.equals(btn_div))
tres.append("/");

if(v.equals(btn_clear))
tres.setText("");

if(v.equals(btn_equal)) {
try {
String data = tres.getText().toString();
if (data.contains("/")) {
String[] operands = data.split("/");
if(operands.length==2){
double op1 = Double.parseDouble(operands[0]);
double op2 = Double.parseDouble(operands[1]);
double result = op1/op2;
tres.setText(String.valueOf(result));
}
}
if (data.contains("*")) {
String[] operands = data.split("\\*");
Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 26
if(operands.length==2){
double op1 = Double.parseDouble(operands[0]);
double op2 = Double.parseDouble(operands[1]);
double result = op1*op2;
tres.setText(String.valueOf(result));
}
}
if (data.contains("+")) {
String[] operands = data.split("\\+");
if(operands.length==2){
double op1 = Double.parseDouble(operands[0]);
double op2 = Double.parseDouble(operands[1]);

double result = op1+op2;


tres.setText(String.valueOf(result));
}
}
if (data.contains("-")) {
String[] operands = data.split("-");
if(operands.length==2){
double op1 = Double.parseDouble(operands[0]);
double op2 = Double.parseDouble(operands[1]);
double result = op1-op2;
tres.setText(String.valueOf(result));
}
}
} catch (NumberFormatException e) {
Toast.makeText(getApplicationContext(),"Input Error",
Toast.LENGTH_LONG).show();
}
}
}
}

Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 27


Output:

Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 28


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.

Layout:
1. Two layouts: Sign Up Activity and Sign In Activity
2. Sign Up Activity
 Constraint Layout
 Two TextView: Username and Password (OR) write it in the hint of
Plaintext and Password
 One Plaintext: Username
 One Password: Password
 One Button: Sign Up
3. Sign In Activity
 Constraint Layout
 Two Textview: Username and Password (OR) write it in the hint of Plaintext
and Password

Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 29


 One Plaintext: Username
 One Password: Password
 One Button: Sign In

Steps to Create SignUp Activity:


1. Create a New Android Project with Empty Activity/Empty View Activity.
2. Open activity_main.xml file from res-> layout folder, check/add Constraint Layout
as the root view.
3. Create Signup Layout using Drag and Drop framework design the layout.
4. Create One more Empty Activity LoginActivity by clicking on Project-> app ->
Right-click -> New -> Activity -> Empty Activity/ Empty View Activity.
5. Open activity_login.xml file from res-> layout folder, check/add Constraint Layout
as the root view.
6. Create Login Layout using Drag and Drop framework.
7. Add Listeners to Button Click Event:
8. Create a class which implements OnClickListener interface.
9. Override onClick() method of OnClickListener Interface.
10. Register the button for click event by calling setOnClickListener() method of View
class and pass the object of the class that implemented OnClickListener Interface.
11. Use Regular Expression: "^(?=.*[A-Z])(?=.*[a-z])(?=.*\\d)(?=.*[@$!])[A-
Za z\\d@$!]{8,}$” to validate the password

Xml Code:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 30


<EditText
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:hint="UserName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.316" />

<EditText
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword"
android:hint="Password"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/signup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sign Up"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 31
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.717" />

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sign Up Page"
android:textColor="@color/black"
android:textSize="30dp"
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.11" />

</android.support.constraint.ConstraintLayout>

activity_sign_in.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".sign_in">

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 32
android:text="Sign In Page"
android:textColor="@color/black"
android:textSize="30dp"
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.143" />

<EditText
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:hint="UserName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.38" />

<EditText
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword"
android:hint="Password"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 33
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.581" />

<Button
android:id="@+id/signin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sign In"
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.8" />

</android.support.constraint.ConstraintLayout>

Java Code:
MainActivity.java

package com.example.signup;

import android.content.Intent;
import android.support.v7.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 {


Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 34
EditText username,password;
Button signUpBtn;

String regularExpr="^(?=.*[A-Z])(?=.*[a-z])(?=.*\\d)(?=.*[@$!])[A-Za-z\\d@$!]{8,}$";

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

username = findViewById(R.id.username);
password = findViewById(R.id.password);
signUpBtn = findViewById(R.id.signup);

signUpBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String uname = username.getText().toString();
String pwd = password.getText().toString();

if(validatePassword(pwd)){

Bundle bundle = new Bundle();

bundle.putString("username",uname);
bundle.putString("password",pwd);

Intent intent = new Intent(MainActivity.this,sign_in.class);

intent.putExtras(bundle);

startActivity(intent);
}
Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 35
else{
Toast.makeText(MainActivity.this, "Invaild Password",
Toast.LENGTH_SHORT).show();
}
}
});
}
public boolean validatePassword(String pwd){
Pattern pattern = Pattern.compile(regularExpr);
Matcher matcher = pattern.matcher(pwd);
return matcher.matches();
}
}

sign_in.java

package com.example.signup;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class sign_in extends AppCompatActivity {

EditText username,password;
Button signInBtn;

int count=0;

@Override
protected void onCreate(Bundle savedInstanceState) {
Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 36
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_in);

username = findViewById(R.id.username);
password = findViewById(R.id.password);
signInBtn = findViewById(R.id.signin);

Bundle bundle = getIntent().getExtras();

String uname = bundle.getString("username");


String pwd = bundle.getString("password");

signInBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String user = username.getText().toString();
String pass = password.getText().toString();

if(user.equals(uname) && pass.equals(pwd)){


Toast.makeText(sign_in.this, "Success", Toast.LENGTH_SHORT).show();
}
else {
count++;
if (count >= 3) {
signInBtn.setEnabled(false);
} else {
Toast.makeText(sign_in.this, "Failed", Toast.LENGTH_SHORT).show();
}
}
}
});
}
}

Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 37


Output:

Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 38


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.

Layout:
1. Contents in “ConstraintLayout”
 Text: Changing Wallpaper Application
 Button: Click Here To Change Wallpaper
Steps to Create Wallpaper Changing Application:
1. Create a New Android Project with Empty Activity.
2. Open activity_main.xml file from res layout folder, check/add LinearLayout as the
root view.
3. Create the layout
4. Add Button component & change the following properties:
 Size: 38dp
 Text: Click Here to Change Wall Paper
5. Add 3 or More images to drawable folder (res->drawable)
6. Declare uses permission android.permission.SET_WALLPAPPER in the
AndroidManifest.xml file
7. Schedule Timer task to change the wallpaper on every 30 seconds interval.
8. Initialize and use WallpaperManager.setBitmap() method to change the wallpaper.
Xml 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"
android:gravity="center"

Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 39


tools:context=".MainActivity">

<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FFEB3B"
android:text="CHANGE WALLPAPER" />

</LinearLayout>

Java Code:
MainActivity.java
package com.example.wallpaper;

import android.app.WallpaperManager;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {

int images[] = new int[]{


R.drawable.bts,
R.drawable.enhypen,
R.drawable.ateez,
R.drawable.seventeen
Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 40
};

Button btn;
int i = 0;

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

btn = findViewById(R.id.btn);

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

new Timer().schedule(new ChangeWallpaper(), 0, 30000);

WallpaperManager wallpapermanager =
WallpaperManager.getInstance(getBaseContext());

try {
wallpapermanager.setBitmap(BitmapFactory.decodeResource(getResources(),
images[i]));
i++;
if (i == 4) {
i = 0;
}
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 41
class ChangeWallpaper extends TimerTask {
@Override
public void run() {
WallpaperManager wallpapermanager =
WallpaperManager.getInstance(getBaseContext());

try {
wallpapermanager.setBitmap(BitmapFactory.decodeResource(getResources(),
images[i]));
i++;
if (i == 4) {
i = 0;
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}

Manifest Code:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.wallpaper">
<uses-permission android:name="android.permission.SET_WALLPAPER">
</uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 42
android:supportsRtl="true"
android:theme="@style/Theme.Wallpaper">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

Output:

Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 43


Program: 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.

Layout:
1. Contents in “ConstraintLayout”
 TextView: Counter Application
 PlainText: Counter Value
 2 Buttons: Start and Stop
Steps to Create Counter Application:
1. Create a New Android Project with Empty Activity.
2. Open activity_main.xml file from res  layout folder, check/add ConstraintLayout as
the root view.
3. Create the layout design using Drag and Drop framework.
4. Add Listeners to Button Click Event:
 Create a class which implements OnClickListener interface.
 Override onClick() method of OnClickListener Interface.
 Register the button for click event by calling setOnClickListener() method of
View class and pass the object of the class that implemented OnClickListener
Interface.
5. Create a Thread to start the counter logic.
6. Steps to Create a Thread
 Create a class that extends Thread Class.
 Override run method of Thread Class.
 Use start() method of thread class to start the thread.

Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 44


7. Create Handler class to receive message from child thread, Handler executes in Main
Thread.
8. Steps to Create Handler
 Create Object of type Handler.
 OverridhandleMessage() of handler class.
9. Pass the counter value to be displayed to the handler.
10. Update the UI to display the counter value received from thread.

Xml Code:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/countervalue1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="COUNTER APPLICATION"
android:textColor="#E91E63"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.533"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.109" />

Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 45


<Button
android:id="@+id/startbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#9C27B0"
android:text="START"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.504"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.499" />

<Button
android:id="@+id/stopbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#9C27B0"
android:text="STOP"
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.723" />

<TextView
android:id="@+id/countervalue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Counter Value"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 46
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.227" />

</android.support.constraint.ConstraintLayout>

Java Code:
MainActivity.java
package com.example.counter;

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {

private static final String TAG = "thread";


Handler mainHandler = new Handler();
int count, i;
boolean running = false;
Button startbtn, stopbtn;
TextView countervalue;
void startThread()
{
NewThread nObj = new NewThread();
nObj.start();
}
@Override
protected void onCreate(Bundle savedInstanceState) {

Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 47


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

countervalue = findViewById(R.id.countervalue);
startbtn = findViewById(R.id.startbtn);

startbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
count = 0;
running = true;
startThread();
}
});

stopbtn = findViewById(R.id.stopbtn);
stopbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
running = false;
}
});
}
class NewThread extends Thread{
@Override
public void run(){
while(running)
{
count++;
mainHandler.post(new Runnable() {
@Override
public void run() {
countervalue.setText(String.valueOf(count));
}
Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 48
});
Log.d(TAG, "startThread" +i);
try{
Thread.sleep(2000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
}

Output:

Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 49


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.

Layout:
1. Contents in “ConstraintLayout”
 TextView: Counter Application
 PlainText: Counter Value
 2 Buttons: Start and Stop
Steps to Create XML and JSON Data Parser Application:
1. Create a New Android Project with Empty Activity.
2. Open activity_main.xml file from res-> layout folder, check/add ConstraintLayout
as the root view.
3. Create the layout design using Drag and Drop framework.
4. Add Listeners to Button Click Event:
 Create a class which implements OnClickListener interface.
 Override onClick() method of OnClickListener Interface.
 Register the button for click event by calling setOnClickListener() method
of View class and pass the object of the class that implemented
OnClickListener Interface.
5. Create viewActivity
 File -> New - > Activity - > Empty Activity - > Empty View Activity
6. Design activity_view.xml and code viewActivity.java
7. Create assets folder

Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 50


 Project -> App -> Right click -> New - > Folder -> Assets Folder
8. Create input.xml and input.json file inside assets folder and paste the below Xml
Data
 Right click on asset folder - > File -> give File name with extensions

Xml Code:
activity_main.xml

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


<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
android:id="@+id/xmlbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="226dp"
android:background="#9C27B0"
android:text="Parse XML"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />

<Button
android:id="@+id/jsonbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="59dp"

Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 51


android:background="#9C27B0"
android:text="Parse JSON"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/xmlbtn" />

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Parse Your Data"
android:textColor="#E91E63"
android:textSize="24sp"
android:textStyle="bold"
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.148" />
</android.support.constraint.ConstraintLayout>

activity_view.xml

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


<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".viewActivity">

<TextView
Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 52
android:id="@+id/jsonPlaceHolder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="#2196F3"
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.482"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.555" />

<TextView
android:id="@+id/xmlPlaceHolder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="#FFEB3B"
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.482"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.394" />

<TextView
android:id="@+id/xmlHeading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="XML Data"
android:textColor="#F44336"
android:textStyle="bold"
Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 53
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.478"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.213" />

<TextView
android:id="@+id/jsonHeading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="JSON Data"
android:textColor="#F44336"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.488"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.268" />

<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Parse Data"
android:textColor="#009688"
android:textSize="34sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.473"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 54
app:layout_constraintVertical_bias="0.066" />
</android.support.constraint.ConstraintLayout>

Java Code:
MainActivity.java

package com.example.dataparser1;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

Button xmlbtn, jsonbtn;

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

xmlbtn = findViewById(R.id.xmlbtn);
jsonbtn = findViewById(R.id.jsonbtn);

xmlbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,viewActivity.class);
intent.putExtra("mode",1);
startActivity(intent);
}

Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 55


});

jsonbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,viewActivity.class);
intent.putExtra("mode",2);
startActivity(intent);
}
});
}
}

viewActivity.java

package com.example.dataparser1;

import android.support.v7.app.AppCompatActivity;
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;
Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 56
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

public class viewActivity extends AppCompatActivity {

int mode;

TextView xmlPlaceHolder, jsonPlaceholder,xmlHeading,jsonHeading;

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

mode = getIntent().getIntExtra("mode",0);

xmlPlaceHolder = findViewById(R.id.xmlPlaceHolder);
jsonPlaceholder = findViewById(R.id.jsonPlaceHolder);

xmlHeading = findViewById(R.id.xmlHeading);
jsonHeading = findViewById(R.id.jsonHeading);

if(mode==1)
{
parseXML();
}

if(mode==2)
{
parseJSON();
}
}

//Read the file --> inputStream


Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 57
//array = inputStream

//String(array)

public void parseJSON()


{
xmlPlaceHolder.setText("");
xmlHeading.setText("");

String stringData = null;

try{
InputStream inputStream = getAssets().open("input.json");

//2nd step

int size = inputStream.available(); //get the file

byte buffer[] = new byte[size];

inputStream.read(buffer);

stringData = new String(buffer);

//3rd step

Log.e("data","parseJSON: " +stringData);

JSONObject jsonObject = new JSONObject(stringData);

Log.e("data","parseJSON"+jsonObject);

Log.e("data", "parseJSON:"+jsonObject.getClass().getName());
Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 58
JSONObject cityObject = jsonObject.getJSONObject("City"); //for single city value

String cityName = cityObject.getString("City-Name");


String longitude = cityObject.getString("Longitude");
String latitude = cityObject.getString("Latitude");
String temperature = cityObject.getString("Temperature");
String humidity = cityObject.getString("Humidity");

Log.e("data","parseJSON:"+cityName);

jsonPlaceholder.setText("City-Name - "+cityName+"\n");
jsonPlaceholder.append("Longitude - "+longitude+"\n");
jsonPlaceholder.append("Latitude - "+latitude+"\n");
jsonPlaceholder.append("Temperature - "+temperature+"\n");
jsonPlaceholder.append("Humidity - "+humidity+"\n");
}
catch (IOException e)
{
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}

public void parseXML()


{
xmlPlaceHolder.setText("");

jsonPlaceholder.setText("");

jsonHeading.setText("");

try{
Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 59
InputStream inputStream = getAssets().open("input.xml");

DocumentBuilderFactory documentBuilderFactory =
DocumentBuilderFactory.newInstance();

DocumentBuilder documentBuilder =
documentBuilderFactory.newDocumentBuilder();

Document document = documentBuilder.parse(inputStream);

//document.normalize(); //if there are multiple data value

NodeList cityList = document.getElementsByTagName("City"); //get element who


has city as tag name

for(int i=0;i<cityList.getLength();i++)
{
Node c = cityList.item(i);

if(c.getNodeType() == Node.ELEMENT_NODE)
{
Element city = (Element) c;

//String id = city.getAttribute("id"); //We have tags not attributes so change in


gettextcontent
// Log.e("data","parseXML:" +id);

NodeList cityDetailList = city.getChildNodes();

for(int j=0;j<cityDetailList.getLength();j++)
{
Node n = cityDetailList.item(j);

if(n.getNodeType() == Node.ELEMENT_NODE)
Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 60
{
Element cityDetail = (Element) n;

String tagValue = cityDetail.getTagName();


String value = cityDetail.getTextContent();

Log.e("data","parseXML"+value); //to display on console

xmlPlaceHolder.append(tagValue+" - "+value);
xmlPlaceHolder.append("\n");
}
}
}
}
}
catch (IOException e)
{
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
}
}

input.xml

<?xml version="1.0"?>
<Records>
<City>
<City-Name>Mangalore</City-Name>
<Longitude>78</Longitude>
<Latitude>13</Latitude>
Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 61
<Temperature>30</Temperature>
<Humidity>90%</Humidity>
</City>
</Records>

input.json

"City":
{
"City-Name": "Bengaluru",
"Longitude": "78,",
"Latitude": "13",
"Temperature": "30",
"Humidity": "90%"
}
}

Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 62


Output:

Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 63


Program: 7
Develop a simple application with one Edit Text 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.

Layout:
1. Contents in “ConstraintLayout”
 TextView: Text To Speech Application
 PlainText: Type
 Button: Convert Text to Speech
Steps to Create Text To Speech Application:
1. Create a New Android Project with Empty Activity.
2. Open activity_main.xml file from res  layout folder, check/add ConstraintLayout as
the root view.
3. Create the layout design using Drag and Drop framework.
4. Add Listeners to Button Click Event:
 Create a class which implments OnClickListener interface.
 Override onClick() method of OnClickListener Interface.
 Register the button for click event by calling setOnClickListener() method of
View class and pass the object of the class that implemented OnClickListener
Interface.
5. Initialize TextToSpeech Engine and the Language to Speak using setLanguage()
method
6. Use Speak() method to speak the text passed to it.
Xml Code:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"

Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 64


android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text To Speech Application"
android:textSize="24sp"
android:textStyle="bold"
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.15" />

<EditText
android:id="@+id/EnterValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="128dp"
android:ems="10"
android:inputType="textPersonName"
android:hint="Enter Your Text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />

<Button
android:id="@+id/btnspeak"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 65
android:layout_marginTop="92dp"
android:background="#FFEB3B"
android:text="Convert The Text To Speech"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/EnterValue" />
</android.support.constraint.ConstraintLayout>

Java Code:
MainActivity.java
package com.example.texttospeech;

import android.speech.tts.TextToSpeech;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.Locale;

public class MainActivity extends AppCompatActivity {

EditText EnterValue;
Button btnspeak;
TextToSpeech textToSpeech;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 66


setContentView(R.layout.activity_main);

btnspeak = findViewById(R.id.btnspeak);
EnterValue = findViewById(R.id.EnterValue);

textToSpeech = new TextToSpeech(MainActivity.this, new


TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {

if(status == TextToSpeech.SUCCESS) {
textToSpeech.setLanguage(Locale.ENGLISH);
}
else
{
Log.e("Failed", "onInit : Failed");
}
}
});

btnspeak.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String text = EnterValue.getText().toString();
Toast.makeText(getBaseContext(),text,Toast.LENGTH_LONG).show();
textToSpeech.speak(text,TextToSpeech.QUEUE_FLUSH,null);
}
});
}
}

Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 67


Output:

Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 68


Program: 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.

Layout:
1. Contents in “ConstraintLayout”
 TextView: Call and Save Application
 PlainText: To display the numbers
 15 Buttons: 0-9, Delete, Call and Save
Steps to Create Call and Save Application:
1. Create a New Android Project with Empty Activity.
2. Open activity_main.xml file from res layout folder, check/add ConstraintLayout as
the
3. root view.
4. Create the layout design using Drag and Drop framework.
5. Add Listeners to Button Click Event:
6. Create a class which implments OnClickListener interface.
7. Override onClick() method of OnClickListener Interface.
8. Register the button for click event by calling setOnClickListener() method of View
class and pass the object of the class that implemented OnClickListener Interface.
9. Declare uses permission android.permission.CALL_PHONE in the manifest file.
10. Use ACTION_CALL intent name and pass the “tel:<phone-number> as URI in intent
data and start the call activity.
11. Use intent name and pass the “Telephone Number” and “unknown” as name as intent
data call Contacts Save Activity

Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 69


Xml Code:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
android:id="@+id/btnCall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:background="#9C27B0"
android:text="Call"
app:layout_constraintEnd_toStartOf="@+id/btn3"
app:layout_constraintHorizontal_bias="0.506"
app:layout_constraintStart_toEndOf="@+id/btn1"
app:layout_constraintTop_toBottomOf="@+id/btn0" />

<Button
android:id="@+id/btnSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:background="#9C27B0"
android:text="Save"
app:layout_constraintEnd_toStartOf="@+id/btn2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnstar" />

Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 70


<Button
android:id="@+id/btnRemove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:background="#9C27B0"
android:text="X"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.493"
app:layout_constraintStart_toEndOf="@+id/btn2"
app:layout_constraintTop_toBottomOf="@+id/btnHash" />

<Button
android:id="@+id/btn0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:background="#9C27B0"
android:text="0"
app:layout_constraintEnd_toStartOf="@+id/btn3"
app:layout_constraintHorizontal_bias="0.506"
app:layout_constraintStart_toEndOf="@+id/btn1"
app:layout_constraintTop_toBottomOf="@+id/btn8" />

<Button
android:id="@+id/btn9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:background="#9C27B0"
android:text="9"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.493"
app:layout_constraintStart_toEndOf="@+id/btn2"
Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 71
app:layout_constraintTop_toBottomOf="@+id/btn6" />

<Button
android:id="@+id/btnstar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:background="#9C27B0"
android:text="*"
app:layout_constraintEnd_toStartOf="@+id/btn2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn7" />

<Button
android:id="@+id/btnHash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:background="#9C27B0"
android:text="#"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.493"
app:layout_constraintStart_toEndOf="@+id/btn2"
app:layout_constraintTop_toBottomOf="@+id/btn9" />

<Button
android:id="@+id/btn8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:background="#9C27B0"
android:text="8"
app:layout_constraintEnd_toStartOf="@+id/btn3"
app:layout_constraintHorizontal_bias="0.506"
Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 72
app:layout_constraintStart_toEndOf="@+id/btn1"
app:layout_constraintTop_toBottomOf="@+id/btn5" />

<Button
android:id="@+id/btn7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:background="#9C27B0"
android:text="7"
app:layout_constraintEnd_toStartOf="@+id/btn2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn4" />

<Button
android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="31dp"
android:background="#9C27B0"
android:text="4"
app:layout_constraintEnd_toStartOf="@+id/btn2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn1" />

<Button
android:id="@+id/btn6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="31dp"
android:background="#9C27B0"
android:text="6"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.493"
Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 73
app:layout_constraintStart_toEndOf="@+id/btn2"
app:layout_constraintTop_toBottomOf="@+id/btn3" />

<Button
android:id="@+id/btn5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="31dp"
android:background="#9C27B0"
android:text="5"
app:layout_constraintEnd_toStartOf="@+id/btn3"
app:layout_constraintHorizontal_bias="0.506"
app:layout_constraintStart_toEndOf="@+id/btn1"
app:layout_constraintTop_toBottomOf="@+id/btn2" />

<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_constraintHorizontal_bias="0.577"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.153" />

<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="85dp"
android:background="#9C27B0"
Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 74
android:text="1"
app:layout_constraintEnd_toStartOf="@+id/btn2"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/display" />

<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="85dp"
android:background="#9C27B0"
android:text="2"
app:layout_constraintEnd_toStartOf="@+id/btn3"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/btn1"
app:layout_constraintTop_toBottomOf="@+id/display" />

<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="85dp"
android:background="#9C27B0"
android:text="3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/btn2"
app:layout_constraintTop_toBottomOf="@+id/display" />
</android.support.constraint.ConstraintLayout>

Java Code:
MainActivity.java

Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 75


package com.example.phonedialer;

import android.content.Intent;
import android.net.Uri;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {


EditText display;
Button btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9;
Button btnCall, btnRemove, btnHash, btnStar, btnSave;

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

btnCall = findViewById(R.id.btnCall);
btnStar = findViewById(R.id.btnstar);
Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 76
btnHash = findViewById(R.id.btnHash);
btnRemove = findViewById(R.id.btnRemove);
btnSave = findViewById(R.id.btnSave);

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

Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 77


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");
}
});
Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 78
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("#");
}
});

btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String data = display.getText().toString();
Intent intent = new Intent(ContactsContract.Intents.Insert.ACTION);
intent.setType(ContactsContract.RawContacts.CONTENT_TYPE);
intent.putExtra(ContactsContract.Intents.Insert.NAME,"Unknown");
intent.putExtra(ContactsContract.Intents.Insert.PHONE, data);
startActivity(intent);
}
});

Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 79


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("");
}
});

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

Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 80


Output:

Prof. Tuba Nageen Nakhuda Dept. of CSE, PACE, Mangaluru 81

You might also like