Mysuru Royal Institute of Technology: "Mobile Application Development Lab Manual" (18CSMP68)
Mysuru Royal Institute of Technology: "Mobile Application Development Lab Manual" (18CSMP68)
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="17dp"
android:layout_marginLeft="17dp"
android:layout_marginTop="17dp"
android:layout_marginEnd="244dp"
android:layout_marginRight="244dp"
android:layout_marginBottom="486dp"
android:text="XYZ"
android:textSize="38dp" />
<ImageView android:id="@+id/imageView"
android:layout_width="231dp"
android:layout_height="174dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="-14dp"
android:layout_marginRight="-14dp"
android:layout_marginBottom="481dp"
app:srcCompat="@drawable/logo" />
<View
android:id="@+id/view"
android:layout_width="wrap_content"
android:layout_height="4dp"
android:layout_alignParentBottom="true"
android:background="#4444"
android:layout_marginBottom="466dp" />
<TextView android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="117dp"
android:layout_marginRight="117dp"
android:layout_marginBottom="394dp"
android:text="ABC"
android:textSize="30dp"
android:textStyle="bold" />
<TextView android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="64dp"
android:layout_marginRight="64dp"
android:layout_marginBottom="343dp"
android:text="Assistant Professor-CSE"
android:textSize="25dp" />
<TextView android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="127dp"
android:layout_marginRight="127dp"
android:layout_marginBottom="294dp"
android:text="Ph No: 8050462225"
android:textSize="20dp" />
<TextView android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="229dp"
android:text="Kannada Sahitya Parishath Road Gokulam 3rd Stage Mysuru-02"
android:textSize="20dp" />
<TextView android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="44dp"
android:layout_marginRight="44dp"
android:layout_marginBottom="189dp"
android:text="Email: [email protected]”
android:textSize="20dp" />
</RelativeLayout>
JAVA-CODE
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
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.
5) Add PlainText(EditText) component & change the following properties in XML Code:
• Text: “”
• Hint: “Enter the second number”
• id: “@+id/editText2”
6) Add TextView component to display result & change the following properties:
• Size: 40dp
• Text: “0”
• Center-Align
• id: “@+id/textView1”
7) Add 4 Buttons & rename the four buttons “Add”, “Sub”,”Mul” and “div” with
following addition:
• Onclick: “doAdd”(Add Button)
• Onclick: “doSub”(Sub Button)
• Onclick: “doMul”(Mul Button)
• Onclick: “doDiv”(Div Button)
XML-CODE:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="98dp"
android:layout_marginBottom="653dp"
android:text="SIMPLE CALCI"
android:textSize="38dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.042" />
<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="115dp"
android:layout_marginBottom="547dp"
android:ems="10"
android:hint="Enter the First Number"
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="111dp"
android:layout_marginBottom="455dp"
android:ems="10"
android:inputType="textPersonName"
android:hint="Enter the Second Number"
android:text="" />
<TextView android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="203dp"
android:layout_marginBottom="350dp"
android:text="0" android:textSize="40dp"
/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="274dp"
android:layout_marginBottom="237dp"
android:onClick="doAdd"
android:text="ADD" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="68dp"
android:layout_marginBottom="233dp"
android:onClick="doSub"
android:text="SUB" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="277dp"
android:layout_marginBottom="115dp"
android:onClick="doMul"
android:text="MUL" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="63dp"
android:layout_marginBottom="104dp"
android:onClick="doDiv"
android:text="DIV" />
</RelativeLayout>
JAVA-CODE:
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View; import
android.widget.EditText; import
android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText e1,e2;
TextView tv1;
@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); tv1 =
(TextView)findViewById(R.id.textView1);
}
public void doAdd(View V){
int a1 = Integer.parseInt(e1.getText().toString()); int
a2 = Integer.parseInt(e2.getText().toString()); int
result= a1+a2;
tv1.setText(""+result);
}
public void doSub(View V){
int a1 = Integer.parseInt(e1.getText().toString()); int
a2 = Integer.parseInt(e2.getText().toString()); int
result= a1-a2;
tv1.setText(""+result);
}
public void doMul(View V){
int a1 = Integer.parseInt(e1.getText().toString()); int
a2 = Integer.parseInt(e2.getText().toString()); int
result= a1*a2;
tv1.setText(""+result);
}
public void doDiv(View V){
int a1 = Integer.parseInt(e1.getText().toString()); int
a2 = Integer.parseInt(e2.getText().toString()); float
result= a1/a2;
tv1.setText(""+result);
}
}
Program-3: Create a SIGN Up activity with Username and Password. Validation of
password should happen based on the following rules:
• Password should contain uppercase and lowercase letters.
• Password should contain letters and numbers.
• Password should contain special characters.
• Minimum length of the password (the default value is 8).
On successful SIGN UP proceed to the next Login activity. Here the user should SIGN IN
using the Username and Password created during signup activity. If the Username and
Password are matched then navigate to the next activity which displays a message
saying “Successful Login” or else display a toast message saying “Login Failed”. The
user is given only two attempts and after that display a toast message saying “Failed
Login Attempts” and disable the SIGN IN button. Use Bundle to transfer information
from one activity to another.
1) Firstly Create an Application by Name “SignUpActivity”
2) Go to xml code of design change the layout to “RelativeLayout”
3) Add TextView component & change the following properties:
• Size: 38dp
• Text: “Sign Up”
• Center-Align
4) Add Email (EditText) component & change the following properties in XML Code:
• Hint: “Email ID”
• id: “@+id/emailEditText”
5) Add Password (EditText) component & change the following properties in XML
Code:
• Hint: “Password”
• id: “@+id/passwordEditText”
6) Add Button component & change the following properties in XML
• Id: “@+id/signBtn”
• Text: “Sign Up”
XML-CODE
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView android:layout_width="160dp"
android:layout_height="42dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="112dp"
android:layout_marginBottom="573dp"
android:text="Sign Up"
android:textSize="28dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText android:id="@+id/emailEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="29dp"
android:layout_marginBottom="431dp"
android:ems="10"
android:hint="Email ID"
android:inputType="textEmailAddress"
android:textSize="28dp" />
<EditText
android:id="@+id/passwordEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="34dp"
android:layout_marginBottom="345dp"
android:ems="10" android:hint="Password"
android:inputType="textPassword"
android:textSize="28dp" />
<Button
android:id="@+id/signUpBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="106dp"
android:layout_marginBottom="226dp"
android:text="Sign Up"
android:textSize="28dp" />
</RelativeLayout>
JAVA-CODE
package com.example.loginapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle; import
android.view.View; import
android.widget.Button; import
android.widget.EditText; import
android.widget.Toast;
import java.util.regex.Pattern;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
emailEditText = findViewById(R.id.emailEditText);
passwordEditText = findViewById(R.id.passwordEditText);
signUpBtn = findViewById(R.id.signUpBtn);
signUpBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = emailEditText.getText().toString();
String password = passwordEditText.getText().toString(); if
(!isValidPassword(password)) {
Toast.makeText(MainActivity.this, "Password Does not match the rules",
Toast.LENGTH_LONG).show();
return;
}
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
intent.putExtra("email", email);
intent.putExtra("password", password);
startActivity(intent);
}
});
}
11) Add Password (EditText) component & change the following properties in XML
Code:
• Hint: “Password”
• id: “@+id/passwordEditText”
12) Add Button component & change the following properties in XML
• Id: “@+id/loginBtn”
• Text: “Login”
XML-CODE
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LoginActivity">
<TextView android:id="@+id/textView"
android:layout_width="210dp"
android:layout_height="54dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="120dp"
android:layout_marginBottom="576dp"
android:text="Login Activity"
android:textSize="28dp" />
<EditText android:id="@+id/emailEditText"
android:layout_width="222dp"
android:layout_height="80dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="108dp"
android:layout_marginBottom="424dp"
android:ems="10"
android:hint="Email ID"
android:inputType="textEmailAddress"
android:textSize="28dp" />
<EditText
android:id="@+id/passwordEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="40dp"
android:layout_marginBottom="299dp"
android:ems="10" android:hint="Password"
android:inputType="textPassword"
android:textSize="28dp" />
<Button
android:id="@+id/loginBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="173dp"
android:layout_marginBottom="189dp"
android:text="login"
android:textSize="26dp" />
</RelativeLayout>
JAVA-CODE
package com.example.loginapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View; import
android.widget.Button; import
android.widget.EditText; import
android.widget.Toast;
<TextView android:id="@+id/textView2"
android:layout_width="297dp"
android:layout_height="190dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="42dp"
android:layout_marginBottom="400dp"
android:text="Login Successful"
android:textSize="38dp" />
</RelativeLayout>
JAVA-CODE
package com.example.loginapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class LoginSuccessActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_success);
}
}
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.
1) Firstly Create an Application by Name “WallpaperActivity”
2) Go to xml code of design change the layout to “RelativeLayout”
3) Add TextView component & change the following properties:
• Size: 38dp
• Text: Wall Paper Change Application
• Center-Align
4) Add Button component & change the following properties:
• Size: 38dp
• Text: Click Here To Change Wall Paper
5) 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.
XML-CODE
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView android:id="@+id/textView"
android:layout_width="210dp"
android:layout_height="54dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="120dp"
android:layout_marginBottom="576dp"
android:text="Wall Paper Change Application"
android:textSize="28dp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="173dp"
android:layout_marginBottom="189dp"
android:text="Click Here To Change Wall Paper"
android:textSize="26dp" />
</RelativeLayout>
JAVA-CODE
import androidx.appcompat.app.AppCompatActivity;
import android.app.WallpaperManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.BitmapDrawable; import
android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
Button changewallpaper;
Timer mytimer; Drawable
drawable;
WallpaperManager wpm;
int prev=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mytimer = new Timer();
wpm = WallpaperManager.getInstance(this);
changewallpaper = findViewById(R.id.button);
changewallpaper.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
setWallpaper();
}
});
}
private void setWallpaper() {
mytimer.schedule(new TimerTask() {
@Override
public void run() {
if(prev==1) {
drawable = getResources().getDrawable(R.drawable.one);
prev = 2;
}
else if(prev==2) {
drawable = getResources().getDrawable(R.drawable.two);
prev=3;
}
else if(prev==3) {
drawable = getResources().getDrawable(R.drawable.three);
prev=4;
}
else if(prev==4) {
drawable = getResources().getDrawable(R.drawable.four);
prev=5;
}
else if(prev==5) {
drawable = getResources().getDrawable(R.drawable.five);
prev=1;
}
Bitmap wallpaper = ((BitmapDrawable)drawable).getBitmap(); try
{
wpm.setBitmap(wallpaper);
} catch (IOException e) {
e.printStackTrace();
}
}
},0,30000); } }
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 TextViewcontrol.
1) Firstly Create an Application by Name “CounterActivity”
2) Go to xml code of design change the layout to “RelativeLayout”
3) Add TextView component & change the following properties:
• Size: 38dp
• Text: “Counter Application”
• Center-Align
4) Add TextView component & change the following properties:
• Text: “Counter Value”
5) Add Button components & change the following properties:
• Size: 38dp
• Text: Start
• id: “@+id/btn_start”
6) Add Button components & change the following properties:
• Size: 38dp
• Text: Stop
• id: “@+id/btn_stop”
XML-CODE
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView android:layout_width="378dp"
android:layout_height="68dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="18dp"
android:layout_marginBottom="602dp"
android:text="Counter Application"
android:textSize="38dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView"
android:layout_width="121dp"
android:layout_height="32dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="145dp"
android:layout_marginBottom="478dp"
android:text="Counter Value" />
<Button
android:id="@+id/btn_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="297dp"
android:layout_marginBottom="295dp"
android:text="Start" />
<Button
android:id="@+id/btn_stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="74dp"
android:layout_marginBottom="292dp"
android:text="Stop" />
</RelativeLayout>
JAVA-CODE
package com.example.counterapplication;
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;
7) Inside the assets folder create new files of xml and json using the following hierarchy:
new->file->city.xml
new->file->city.json
once created place the following details inside the “city.xml” and “city.json”
city.xml:
<?xml version="1.0"?>
<records>
<place>
<name>Mysore</name>
<lat>12.295</lat>
<long>76.639</long>
<temperature>22</temperature>
<humidity>90 %</humidity>
</place>
<place>
<name>Bangalore</name>
<lat>12.97165</lat>
<long>77.5946</long>
<temperature>25</temperature>
<humidity>74 %</humidity>
</place>
</records>
city.json:
[
{
"name": "HASSAN",
"lat": "12.295",
"long": "76.639",
"temperature": "22",
"humidity": "92 %"
},
{
"name": "MANDYA",
"lat": "12.97165",
"long": "77.5946",
"temperature": "25",
"humidity": "74 %"
}
]
XML-CODE:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="10dp"
android:layout_marginBottom="634dp"
android:text="Parsing XML and JSON"
android:textSize="36sp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="153dp"
android:layout_marginBottom="484dp"
android:onClick="parsexml"
android:text="ParseXML" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="151dp"
android:layout_marginBottom="364dp"
android:onClick="parsejson"
android:text="ParseJSON" />
<TextView
android:id="@+id/display"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="3dp"
android:layout_marginBottom="68dp"
android:text=""
android:textAlignment="center" />
</RelativeLayout>
JAVA-CODE:
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.style.TabStopSpan;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONArray;
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
java.io.InputStream;
import java.nio.charset.StandardCharsets;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
public class MainActivity extends AppCompatActivity {
TextView display;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
display = (TextView)findViewById(R.id.display);
}
public void parsexml(View V)
{
try {
InputStream is = getAssets().open("city.xml");
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(is);
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("XML DATA");
stringBuilder.append("\n ----------- ");
NodeList nodeList = document.getElementsByTagName("place");
for (int i = 0; i < nodeList.getLength(); i++)
{
Node node = nodeList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE)
{ Element element = (Element) node;
stringBuilder.append("\nName: ").append(getValue("name", element));
stringBuilder.append("\nLatitude: ").append(getValue("lat", element));
stringBuilder.append("\nLongitude: ").append(getValue("long", element));
stringBuilder.append("\nTemperature: ").append(getValue("temperature", element));
stringBuilder.append("\nHumidity: ").append(getValue("humidity", element));
stringBuilder.append("\n ------------ ");
}
}
display.setText(stringBuilder.toString());
}catch (Exception e){
e.printStackTrace();
Toast.makeText(MainActivity.this,"Error Parsing XML",Toast.LENGTH_LONG).show();
}
}
public void parsejson(View V){
String json;
StringBuilder stringBuilder = new StringBuilder();
try {
InputStream is = getAssets().open("city.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
json = new String(buffer, StandardCharsets.UTF_8);
JSONArray jsonArray = new JSONArray(json);
stringBuilder.append("JSON DATA");
stringBuilder.append("\n ---------- ");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
stringBuilder.append("\nName: ").append(jsonObject.getString("name"));
stringBuilder.append("\nLatitude: ").append(jsonObject.getString("lat"));
stringBuilder.append("\nLongitude: ").append(jsonObject.getString("long"));
stringBuilder.append("\nTemperature: ").append(jsonObject.getString("temperature"));
stringBuilder.append("\nHumidity: ").append(jsonObject.getString("humidity"));
stringBuilder.append("\n ------------ ");
}
display.setText(stringBuilder.toString());
is.close();
}
catch (Exception e){
e.printStackTrace();
Toast.makeText(MainActivity.this,"Error in reading",Toast.LENGTH_LONG).show();
}
}
private String getValue(String tag, Element element)
{
return element.getElementsByTagName(tag).item(0).getChildNodes().item(0).getNodeValue();
}
}
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.
1) Firstly Create an Application by Name “TextToSpeech”
2) Go to xml code of design change the layout to “RelativeLayout”
3) Add TextView component & change the following properties:
4) Size: 38dp
5) Text: Text2Speech App
6) Center-Align
7) Add PlainText(EditText) component & change the following properties in XML
Code:
• Text: “”
• Hint: “Enter the text to be converted”
• id: “@+id/editText”
8) Add Button component & change the following properties in XML Code:
• Name: Convert
• onClick: convert
XML-CODE:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="59dp"
android:layout_marginRight="59dp"
android:layout_marginBottom="649dp"
android:text="Text2SpeechApp"
android:textSize="40dp" />
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="101dp"
android:layout_marginRight="101dp"
android:layout_marginBottom="514dp"
android:ems="10"
android:hint="Enter the text to be converted"
android:inputType="textPersonName"
android:text="" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="162dp"
android:onClick="convert"
android:layout_marginRight="162dp"
android:layout_marginBottom="329dp"
android:text="Convert" />
</RelativeLayout>
JAVA-CODE:
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import java.util.Locale;
5) Add PlainText(EditText) component & change the following properties in XML Code:
• Text: “”
• Hint: “Copied Text”
• id: “@+id/editText2”
6) Add three buttons to the design & change the text of the Buttons to “Clear”, “Call”, “Save” and
change the id as follows:
• id:”@+id/clearBtn”
• id:”@+id/callBtn”
• id:”@+id/saveBtn”
7) Add twelve buttons to the design & change the text of the Buttons as 1,2,3,4,5,6,7,8,9,0,*,#
XML-CODE
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView android:layout_width="298dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="54dp"
android:layout_marginBottom="575dp"
android:text="Call Application"
android:textSize="36dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/phoneNumberEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="176dp"
android:layout_marginBottom="462dp"
android:ems="10"
android:inputType="phone" />
<Button
android:id="@+id/clearBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="52dp"
android:layout_marginBottom="459dp"
android:text="Clear" />
<Button
android:id="@+id/button2"
android:layout_width="76dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="311dp"
android:onClick="inputNumber"
android:layout_marginBottom="341dp"
android:text="1" />
<Button
android:id="@+id/button3"
android:layout_width="76dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:onClick="inputNumber"
android:layout_alignParentBottom="true"
android:layout_marginEnd="170dp"
android:layout_marginBottom="341dp"
android:text="2" />
<Button
android:id="@+id/button4"
android:layout_width="76dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="32dp"
android:onClick="inputNumber"
android:layout_marginBottom="343dp"
android:text="3" />
<Button
android:id="@+id/button5"
android:layout_width="76dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:onClick="inputNumber"
android:layout_alignParentBottom="true"
android:layout_marginEnd="311dp"
android:layout_marginBottom="241dp"
android:text="4" />
<Button
android:id="@+id/button6"
android:layout_width="76dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="175dp"
android:onClick="inputNumber"
android:layout_marginBottom="239dp"
android:text="5" />
<Button android:id="@+id/button7"
android:layout_width="76dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="32dp"
android:onClick="inputNumber"
android:layout_marginBottom="239dp"
android:text="6" />
<Button
android:id="@+id/button8"
android:layout_width="76dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="313dp"
android:onClick="inputNumber"
android:layout_marginBottom="142dp"
android:text="7" />
<Button
android:id="@+id/button9"
android:layout_width="76dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="171dp"
android:onClick="inputNumber"
android:layout_marginBottom="147dp"
android:text="8" />
<Button
android:id="@+id/button10"
android:layout_width="76dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="34dp"
android:onClick="inputNumber"
android:layout_marginBottom="152dp"
android:text="9" />
<Button
android:id="@+id/button11"
android:layout_width="76dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="309dp"
android:onClick="inputNumber"
android:layout_marginBottom="80dp"
android:text="#" />
<Button
android:id="@+id/button12"
android:layout_width="76dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="169dp"
android:onClick="inputNumber"
android:layout_marginBottom="78dp"
android:text="0" />
<Button
android:id="@+id/button13"
android:layout_width="76dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="34dp"
android:onClick="inputNumber"
android:layout_marginBottom="88dp"
android:text="*" />
<Button
android:id="@+id/callBtn"
android:layout_width="76dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="284dp"
android:layout_marginBottom="17dp"
android:text="Call" />
<Button
android:id="@+id/saveBtn"
android:layout_width="76dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="60dp"
android:layout_marginBottom="17dp"
android:text="Save" />
</RelativeLayout>
JAVA-CODE
package com.example.callingapplication;
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;