Sie sind auf Seite 1von 64

ONE DAY WORKSHOP ON ANDROID APPLICATION DEVELOPMENT

ONE DAY WORKSHOP ON ANDROID


APPLICATION DEVELOPMENT
Resource Person
Mr.D.Kesavaraja M.E , MBA, (PhD) , MISTE
Assistant Professor,
Department of Computer Science and Engineering,
Dr.Sivanthi Aditanar College of Engineering
Tiruchendur - 628215

Mobile Application Development Laboratory

Basic steps to start a new project:


• Start  Android Studio  Start a new android studio project
• Give the Application Name say “ExNo1”, Click Next
• Tick Phone and Tablet check box, Choose Minimum SDK as “API:15 Android 4.0.3 (Ice Cream
Sandwich), Click Next
• Choose “Blank Activity” Click Next
• Customize the activity and Click Finish.

To create virtual device:


• Select ‘AVD Manager’ button from the tool bar (or)
• Tools  Android  AVD Manager
• Click ‘Create Virtual Device’
• To select Hardware, Choose the required phone (Prefer Nexus 5). Click Next
• Choose the system image and Click Next
• Give a name for AVD (AVD name should start with an alphabet)
• Click ‘Show Advanced Settings’ Give the RAM as 512. Click Finish.
• Choose your virtual device and Click OK

To execute the program:


From Menu bar, select RunRun
From Tool bar, select Run button

Presented By D.Kesavaraja www.k7cloud.in 1|Page


ONE DAY WORKSHOP ON ANDROID APPLICATION DEVELOPMENT

Ex: No: 1a APPLICATION USING GUI COMPONENTS FONTS AND COLORS

AIM:
To create an application using GUI components and to change the font and color of the text in its
properties.

ALGORITHM:

Step 1. Open a new project


Step 2. Configure your project, select Minimum SDK, add an activity and customize the activity
Step 3. In ‘Design View’ of ‘activity_main.xml’, design a page to get Student details like name,
date of birth, gender, mobile number, email id, etc..
Step 4. Use the GUI components like
i. Layout  Linear Layout(Vertical)
ii. Widgets  Plain TextView (or) LargeText (or) MediumText (or) SmallText,
Radio button, Check Box to display the information.
iii. Text Fields Plain Text, Person Name, Date, Phone, E-mail to get the user
information.
Step 5. Click a component, its properties will be displayed on the right hand side. Change its
background, Text Color and Typeface.
Step 6. Execute the program

Note: Drag the GUI component from the Palette and place it in the design view
Right click on the GUI component and change the text and id (if required)
(Drag 6 large texts from the Palette and place it
in the design view, Right click on it and change
the text as ‘Student Name’, ‘DOB’, ‘Gender’,
‘Mobile Number’, ‘Email Id’, Social Media you
like’ respectively.

Drag and place the Text Fields Plain Text,


Person Name, Date, Phone, E-mail to get the
user information.

While adding Radio buttons group the similar


radio buttons using ‘RadioGroup’)
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RadioButton
.../>
</RadioGroup>

PROGRAM:

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
Presented By D.Kesavaraja www.k7cloud.in 2|Page
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Student Details"
android:id="@+id/textView"
android:layout_gravity="center_horizontal"
android:background="#ff070208"
android:textColor="#fffffafa" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="StudentName"
android:id="@+id/textView2"
android:typeface="serif"
android:textColor="#ffff2372" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Name"
android:ems="10"
android:id="@+id/editText"
android:layout_gravity="center_horizontal"
android:textColor="#ff1b39ff" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="DOB"
android:id="@+id/textView3"
android:typeface="serif"
android:textColor="#ffff2372" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
3
android:inputType="date"
android:ems="10"
android:id="@+id/editText2"
android:textColor="#ff1b39ff" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Gender"
android:id="@+id/textView4"
android:typeface="serif"
android:textColor="#ffff2372" />

<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male"
android:id="@+id/radioButton"
android:checked="false"
android:textColor="#ff1b39ff" />

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female"
android:id="@+id/radioButton2"
android:checked="false"
android:textColor="#ff1b39ff" />
</RadioGroup>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Mobile Number"
android:id="@+id/textView6"
android:typeface="serif"
android:textColor="#ffff2372" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="@+id/editText3"
android:layout_gravity="center_horizontal"
android:textColor="#ff1b39ff" />

<TextView
4
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Email Id"
android:id="@+id/textView5"
android:typeface="serif"
android:textColor="#ffff2372" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="@+id/editText4"
android:textColor="#ff1b39ff" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Social Media You Like"
android:id="@+id/textView7"
android:typeface="serif"
android:textColor="#ffff2372" />

<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FaceBook"
android:id="@+id/checkBox"
android:checked="false"
android:textColor="#ff1b39ff" />

<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="WhatsApp"
android:id="@+id/checkBox2"
android:checked="false"
android:textColor="#ff1b39ff" />

<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hike"
android:id="@+id/checkBox3"
android:checked="false"
android:textColor="#ff1b39ff" />

</LinearLayout>
</RelativeLayout>

ActivityMain.java
5
package com.example.adminpc.exno1;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;

public class MainActivity extends ActionBarActivity {


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

OUTPUT

6
RESULT:
Thus to create an application using GUI components and to change the font and color of the text
has been developed successfully.

Ex: No: 1b APPLICATION USING GUI COMPONENTS FONTS AND COLORS

AIM:
7
To create an application using GUI components and to change the font and color of the text when
a button is clicked.

ALGORITHM:

Step 1. Open a new project


Step 2. Configure your project, select Minimum SDK, add an activity and customize the activity
Step 3. In ‘Design View’ of ‘activity_main.xml’, design a page to display the message “Have a
nice day” and change the size and color of the text when the button is pressed.
Step 4. Use the GUI components like
i. Layout  Linear Layout(Vertical)
ii. Widgets  Plain TextView (or) LargeText (or) MediumText (or) SmallText,
Button.
Step 5. Go to project explorer and select java folder com.example.cse.ExNo1b. Now
select MainActivity.java file and write OnClickListener() function for both the buttons.
Step 6. Execute the program

Note: Drag the GUI component from the Palette and place it in the design view
Right click on the GUI component and change the text and id (if required)

(Drag large text from the Palette and place it in


the design view, Right click on it and change the
text as ‘HAVE A NICE DAY’.

Drag 2 button from the Palette and place it in


the design view, Right click on it and change the
text as ‘CHANGE FONT SIZE’, ‘CHANGE
COLOR’)

PROGRAM:

activity_main.xml

8
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="HAVE A NICE DAY"
android:id="@+id/textView"
android:layout_gravity="center_horizontal" />

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Click Me to Change the FontSize"
android:id="@+id/button" />

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Click Me to change the FontColor"
android:id="@+id/button2" />
</LinearLayout>
</RelativeLayout>

ActivityMain.java

package com.example.adminpc.ann1b;

import android.graphics.Color;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

9
float font=25;
int i=1;

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

final TextView t1 = (TextView) findViewById(R.id.textView);

Button b1 = (Button) findViewById(R.id.button);


b1.setOnClickListener(new View.OnClickListener() {

/* A notification red symbol(blub) will be displayed on the LHS, click on it and Choose
OnClickListener() it will “Create ‘OnClick (View v)’ in ‘MainActicity’ */
@Override
public void onClick(View v) {
t1.setTextSize(font);
font = font + 5;
if (font == 50)
font = 20;
}
});

Button b2 = (Button) findViewById(R.id.button2);


b2.setOnClickListener(new View.OnClickListener() {
/* A notification red symbol(blub) will be displayed on the LHS, click on it and Choose
OnClickListener() it will “Create ‘OnClick (View v)’ in ‘MainActicity’ */

@Override
public void onClick(View v) {
switch (i) {
case 1:
t1.setTextColor(Color.parseColor("Red"));
break;
case 2:
t1.setTextColor(Color.parseColor("Black"));
break;
case 3:
t1.setTextColor(Color.parseColor("Yellow"));
break;
case 4:
t1.setTextColor(Color.parseColor("Green"));
break;
}
i++;
if (i == 5)
i = 1;
}

10
});
}
}

OUTPUT

RESULT:
Thus to create an application using GUI components and to change the font and color of the text
has been developed successfully.

11
Ex: No: 2 APPLICATION USING LAYOUT MANAGER AND EVENT LISTENERS

AIM:
To create an application using layout manager and event listeners.

ALGORITHM:

Step 1. Open a new project


Step 2. Configure your project, select Minimum SDK, add an activity and customize the activity.
Step 3. In ‘Design View’ of ‘activity_main.xml’, design a page to get User’s first and last name.
Step 4. Use the GUI components like
i. Layout  Linear Layout(Vertical)
ii. Widgets  Plain TextView (or) LargeText (or) MediumText (or) SmallText,
Button to display the information.
iii. Text Fields Plain Text or Person Name to get the user information.

(Drag 2 large text from the Palette and place it in


the design view, Right click on it and change the
text as ‘First Name’ and ‘Last Name’
respectively
Drag 2 plain text from the Palette and place it in
the design view, Right click on it make the text
empty ‘’ and id as ‘idfname’ and ‘idlname’
respectively
Drag a button from the Palette and place it in the
design view, Right click on it and change the
text as ‘ClickMe’)

Step 5. Under ‘Package Explorer’ (On the left hand side) double click on java and expand it.
Right click on ‘com.eg.adminpc.exno2’
Choose NewActivityBlankActivity.
Give the Activity name as ‘Welcome Activity’
Step 6. In ‘Design View’ of ‘activity_welcome.xml’, design a page to Welcome the user
Step 7. Use the GUI components like
i. Layout  Linear Layout(Horizontal)
ii. Widgets  LargeText to display the information.

12
(Drag a LargeText from the Palette and place it
in the design view, Right click on it and change
the text as ‘Welcome’
Drag a LargeText from the Palette and place it in
the design view, Right click on it make the text
empty ‘’ and id as ‘idflname’)

Step 8. For the <Button> element, add the android:onClick attribute. (Under content_main.xml)
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
android:id="@+id/button"
android:layout_gravity="center_horizontal"
android:onClick="WelcomeScreen"/>
Step 9. A notification symbol(blub) will be displayed on the LHS, click on it and Choose
“Create ‘WelcomeScreen(View)’ in ‘MainActicity’”
Step 10. Under MainActivity.java add the following code
public void WelcomeScreen(View view) {
Intent ws=new Intent(this,WelcomeActivity.class);
EditText E1=(EditText)findViewById(R.id.idfname);
EditText E2=(EditText)findViewById(R.id.idlname);
ws.putExtra("fname",E1.getText().toString());
ws.putExtra("lname",E2.getText().toString());
startActivity(ws);
}
Step 11. Under WelcomeActivity.java add the following code
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
Intent nn=getIntent();
String fname=nn.getStringExtra("fname");
String lname=nn.getStringExtra("lname");
TextView tv=(TextView)findViewById(R.id.idflname);
tv.setText(fname+lname);
}

13
PROGRAM:
Activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="First Name"
android:id="@+id/textView" />

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

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Last Name"
android:id="@+id/textView2" />

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

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
android:id="@+id/button"
android:layout_gravity="center_horizontal"
android:onClick="WelcomeScreen"/>
</LinearLayout>
</RelativeLayout>

14
MainActivity.java

package com.example.adminpc.exno2;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends ActionBarActivity {

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

public void WelcomeScreen(View view) {


Intent ws=new Intent(this,WelcomeActivity.class);
EditText E1=(EditText)findViewById(R.id.idfname);
EditText E2=(EditText)findViewById(R.id.idlname);
ws.putExtra("fname",E1.getText().toString());
ws.putExtra("lname",E2.getText().toString());
startActivity(ws);
}
}

Activity_welcome.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.adminpc.exno2.WelcomeActivity">

<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Welcome"
android:id="@+id/textView3" />

15
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/idflname" />
</LinearLayout>
</RelativeLayout>

WelcomeActivity.java
package com.example.adminpc.exno2;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.widget.TextView;

public class WelcomeActivity extends ActionBarActivity {

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

Intent nn=getIntent();
String fname=nn.getStringExtra("fname");
String lname=nn.getStringExtra("lname");
TextView tv=(TextView)findViewById(R.id.idflname);
tv.setText(fname+lname);
}
}
OUTPUT:

RESULT:
Thus to create an application using layout manager and event listeners has been developed
successfully.
16
Ex: No: 3 APPLICATION TO CREATE NATIVE CALCULATOR

AIM:
To create a native calculator application.
ALGORITHM:
Step 1. Open a new project
Step 2. Configure your project, select Minimum SDK, add an activity and customize the activity.
Step 3. In ‘Design View’ of ‘activity_main.xml’, design a calculator page
Step 4. Use the GUI components like
i. Widgets  Button to display the number and operators.
ii. Text Fields Plain Text to get the user data.

(Drag a plain text from the Palette and place it in


the design view, Right click on it make the text
empty ‘’ and id as ‘result_id’

Drag required number of buttons from the


Palette and place it in the design view, Right
click on it and change the text and give a
meaningful id for each button)

Step 5. Under ‘Package Explorer’ (On the left hand side) double click on java and expand it.
Right click on ‘com.eg.adminpc.exno3’
Step 6. Define the functions insert(), perform(), calculate() and reset().
Step 7. For <button> “0 to 9” call the insert() function
Step 8. For <button> “+, -, *, /” call the perform() function
Step 9. For <button> “=” call the calculate() function
Step 10. For <button> “CLEAR” call the reset() function
Step 11. Execute the program.

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
17
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="CALCULATOR"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#ff0d0404"
android:textColor="#ffffffff"
android:typeface="serif"
android:textAlignment="center" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/result_id"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:id="@+id/button"
android:layout_below="@+id/result_id"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="one"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:id="@+id/two"
android:layout_alignTop="@+id/button"
android:layout_toRightOf="@+id/button"
android:layout_toEndOf="@+id/button"
android:onClick="two"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:id="@+id/three"
android:layout_alignTop="@+id/two"
android:layout_toRightOf="@+id/two"
android:layout_toEndOf="@+id/two"
android:onClick="three"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:id="@+id/plus"
18
android:layout_alignTop="@+id/two"
android:layout_alignRight="@+id/result_id"
android:layout_alignEnd="@+id/result_id"
android:onClick="ButtonPlus"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:id="@+id/four"
android:layout_below="@+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="four"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5"
android:id="@+id/five"
android:layout_below="@+id/button"
android:layout_toRightOf="@+id/button"
android:layout_toEndOf="@+id/button"
android:onClick="five"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6"
android:id="@+id/six"
android:layout_below="@+id/two"
android:layout_toRightOf="@+id/two"
android:layout_toEndOf="@+id/two"
android:onClick="six"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:id="@+id/minus"
android:layout_alignTop="@+id/six"
android:layout_alignRight="@+id/plus"
android:layout_alignEnd="@+id/plus"
android:onClick="ButtonMinus"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7"
android:id="@+id/seven"
android:layout_below="@+id/four"
android:layout_toLeftOf="@+id/five"
android:layout_toStartOf="@+id/five"
android:onClick="seven"/>
19
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8"
android:id="@+id/eight"
android:layout_alignTop="@+id/seven"
android:layout_toRightOf="@+id/seven"
android:layout_toEndOf="@+id/seven"
android:onClick="eight"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9"
android:id="@+id/nine"
android:layout_alignTop="@+id/eight"
android:layout_toRightOf="@+id/eight"
android:layout_toEndOf="@+id/eight"
android:onClick="nine"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="*"
android:id="@+id/mul"
android:layout_below="@+id/six"
android:layout_alignRight="@+id/minus"
android:layout_alignEnd="@+id/minus"
android:onClick="ButtonMultiply"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CLEAR"
android:id="@+id/clear"
android:layout_below="@+id/seven"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="Clear"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:id="@+id/zero"
android:layout_alignTop="@+id/clear"
android:layout_toRightOf="@+id/clear"
android:layout_toEndOf="@+id/clear"
android:onClick="zero"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
20
android:text="="
android:id="@+id/equalto"
android:layout_alignTop="@+id/zero"
android:layout_alignLeft="@+id/nine"
android:layout_alignStart="@+id/nine"
android:onClick="ButtonEqualto"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="/"
android:id="@+id/div"
android:layout_below="@+id/nine"
android:layout_toRightOf="@+id/nine"
android:layout_toEndOf="@+id/nine"
android:onClick="ButtonDivide"/>
</RelativeLayout>

MainActivity.java

package com.example.cse.calc;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends ActionBarActivity {


public String str ="";
character op = 'q';
int i, num, numtemp;
EditText showResult;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showResult=(EditText)findViewById(R.id.result_id);
}

public void one(View view) {


insert(1);
}
public void two(View view) {
insert(2);
}
public void three(View view) {
insert(3);
}
public void four(View view) {
insert(4);
}
21
public void five(View view) {
insert(5);
}
public void six(View view) {
insert(6);
}
public void seven(View view) {
insert(7);
}
public void eight(View view) {
insert(8);
}
public void nine(View view) {
insert(9);
}
public void zero(View view) {
insert(0);
}
public void Clear(View view) {
reset();
}
public void ButtonPlus(View view) {
perform();
op = '+';
}
public void ButtonMinus(View view) {
perform();
op = '-';
}
public void ButtonMultiply(View view) {
perform();
op = '*';
}
public void ButtonDivide(View view) {
perform();
op = '/';
}
public void ButtonEqualto(View view) {
calculate();
}

private void insert(int i) {


str = str+Integer.toString(i);
num = Integer.valueOf(str);
showResult.setText(str);
}

private void perform() {


str = "";
numtemp = num;
}

private void calculate() {


if(op == '+')
22
num = numtemp+num;
else if(op == '-')
num = numtemp-num;
else if(op == '/')
num = numtemp/num;
else if(op == '*')
num = numtemp*num;
showResult.setText(""+num);
}

private void reset() {


str ="";
op ='q';
num = 0;
numtemp = 0;
showResult.setText("");
}
}
OUTPUT:

RESULT:
Thus an application to create native calculator has been developed successfully.

23
Ex: No: 4 APPLICATION TO DRAW BASIC GRAPHICAL PRIMITIVES ON SCREEN

AIM:
To create an application to draw basic graphical primitives on screen

ALGORITHM:

Step 1. Open a new project


Step 2. Configure your project, select Minimum SDK, add an activity and customize the activity.
Step 3. In activity_main.xml file. Don't change anything in the layout. Leave it as default.
Step 4. Now select MainActivity.java file and edit the java code(Given below).
Step 5. Execute the program.

PROGRAM:

MainActivity.java
package com.example.adminpc.exno4;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends ActionBarActivity {

public void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(new myview(this));
}
private class myview extends View
{
public myview(Context context)
{
super(context);
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
Paint paint=new Paint();
paint.setTextSize(40);
paint.setColor(Color.BLUE);
canvas.drawText("CIRCLE", 55, 30, paint);
paint.setColor(Color.RED);
canvas.drawCircle(100, 150,100, paint);
paint.setColor(Color.RED);
canvas.drawText("RECTANGLE", 255, 30, paint);
paint.setColor(Color.BLUE);
canvas.drawRect(250, 50,400,350, paint);
paint.setColor(Color.GREEN);
canvas.drawText("SQUARE", 55, 430, paint);
24
paint.setColor(Color.YELLOW);
canvas.drawRect(50, 450,150,550, paint);
paint.setColor(Color.YELLOW);
canvas.drawText("LINE", 255, 430, paint);
paint.setColor(Color.BLACK);
canvas.drawLine(250, 500, 350, 500, paint);
}
}
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

</RelativeLayout>

OUTPUT:

RESULT:
Thus to create an application to draw basic graphical primitives has been developed successfully.

25
Ex: No: 5 DEVELOP AN APPLICATION THAT MAKES USE OF DATABASE

AIM:
To create an application that makes use of database.

ALGORITHM:

Step 1. Open a new project


Step 2. Configure your project, select Minimum SDK, add an activity and customize the activity.
Step 3. Under ‘Package Explorer’ (On the left hand side) double click on java and expand it.
Right click on ‘com.eg.adminpc.exno5’
Choose NewJava class

In Create New Class, Give the name as ‘DBHelper’ and include the DBHelper.java code.
Step 4. In ‘Design View’ of ‘activity_main.xml’, design a database to get student detail.
Step 5. Use the GUI components like
i. Layout  Linear Layout(Vertical) and Linear Layout(Horizontal) to place the
button.
ii. Widgets  Plain TextView (or) LargeText (or) MediumText (or) SmallText,
Button to display the information.
iii. Text Fields Plain Text or Person Name to get the Student information.

(Drag 2 large text from the Palette and place it in


the design view, Right click on it and change the
text as ‘Student Name’ and ‘Student Rollno’
respectively
Drag 2 plain text from the Palette and place it in
the design view, Right click on it make the text
empty ‘’ and id as ‘studname’ and ‘studrollno’
respectively
Drag a 3 button from the Palette and place it in
the design view, Right click on it and change the
text as ‘SAVE’, ‘CLEAR’ & ‘VIEW’)

26
Step 6. For the <Button> element, add the android:onClick attribute. (Under content_main.xml)
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"
android:id="@+id/button"
android:onClick="insert"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear"
android:id="@+id/button2"
android:onClick="clear"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View"
android:id="@+id/button3"
android:onClick="viewdata"/>
Step 7. A notification symbol(bulb) will be displayed on the LHS, click on it and Choose
“Create ‘insert(View)’, ‘clear (View)’ & ‘viewdata(View)’in ‘MainActicity’”
Step 8. Under MainActivity.java include the code (below).
Step 9. Execute the program.

PROGRAM:
DBHelper.java
package com.example.adminpc.ex5;

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

public class DBHelper extends SQLiteOpenHelper {


/* A notification red symbol(bulb) will be displayed on the LHS, click on it and Choose Implement
method and select all the method and click Ok */

public static final String DATABASE_NAME="student.db";

public DBHelper(Context context)


{
super(context,DATABASE_NAME,null,1);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table student(sname TEXT,rollno TEXT)");

27
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS STUDENT");

}
public void insertdata(String studname,String studroll)
{
SQLiteDatabase db=this.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put("sname",studname);
cv.put("rollno",studroll);
db.insert("student",null,cv);
}
public Cursor getalldata()
{
SQLiteDatabase db=this.getWritableDatabase();
Cursor res=db.rawQuery("select * from student",null);
return res;
}
}

Activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:id="@+id/txt">

<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Student Name"
android:id="@+id/textView" />

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

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
28
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Student Rollno"
android:id="@+id/textView2" />

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

<LinearLayout
android:orientation="horizontal"
android:layout_width="367dp"
android:layout_height="wrap_content"
android:layout_weight="0.50">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"
android:id="@+id/button"
android:onClick="insert"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear"
android:id="@+id/button2"
android:onClick="clear"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View"
android:id="@+id/button3"
android:onClick="viewdata"/>
</LinearLayout>

</LinearLayout>
</RelativeLayout>

MainActivity.java

package com.example.adminpc.ex5;

import android.app.AlertDialog;
import android.database.Cursor;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

29
public class MainActivity extends ActionBarActivity {
EditText E1,E2;
DBHelper mydb;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mydb=new DBHelper(this);
E1=(EditText)findViewById(R.id.studname);
E2=(EditText)findViewById(R.id.studrollno);
}

public void insert(View view) {


mydb.insertdata(E1.getText().toString(),E2.getText().toString());
Toast.makeText(this,"Data Inserted",Toast.LENGTH_SHORT).show();
}

public void clear(View view) {


E1.setText("");
E2.setText("");
}

public void viewdata(View view) {


Cursor c=mydb.getalldata();
if(c.getCount()==0)
{
showdialog("Alert","No Data Found");
}
else
{
StringBuffer bf=new StringBuffer();
while (c.moveToNext())
{
bf.append("Name:"+c.getString(0)+"\n");
bf.append("Rollno:"+c.getString(1)+"\n");
}
showdialog("Data",bf.toString());
}

}
public void showdialog(String title,String msg)
{
AlertDialog.Builder bu=new AlertDialog.Builder(this);
bu.setCancelable(true);
bu.setTitle(title);
bu.setMessage(msg);
bu.show();
}
}
}

30
OUTPUT:

RESULT:
Thus to create an application using database to store the student detail has been developed
successfully.

31
Ex: No: 6 APPLICATION THAT IMPLEMENTS MULTITHREADING

AIM:
To create an application that implements multithreading.

ALGORITHM:

Step 1. Open a new project


Step 2. Configure your project, select Minimum SDK, add an activity and customize the activity.
Step 3. In ‘Design View’ of ‘activity_main.xml’, design a page for multithreading.
Step 4. Use the GUI components like
i. Layout  Linear Layout(Vertical) and Linear Layout(Horizontal) to place the
button.
ii. Widgets  Button and LargeText to display the information.

(Drag a button from the Palette and place it in


the design view, Right click on it and change the
text as ‘START MULTITHREADING’

Drag a large text from the Palette and place it in


the design view, Right click on it and change the
text as ‘Click the button’)

Step 5. For the <Button> element, add the android:onClick attribute. (Under content_main.xml)
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="START MULTITHREADING"
android:id="@+id/button"
android:onClick="fetchData"/>

Step 6. A notification symbol(bulb) will be displayed on the LHS, click on it and Choose
“Create ‘fetchData(View)’in ‘MainActicity’”
Step 7. Under MainActivity.java include the code (below).
Step 8. Execute the program.

32
activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="START MULTITHREADING"
android:id="@+id/button"
android:layout_gravity="center_horizontal"
android:onClick="fetchData"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Main Thread"
android:id="@+id/textView" />
</LinearLayout>
</RelativeLayout>

MainActivity.java

package com.example.adminpc.ex6multithreading;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {


private TextView tv;
private static final int t1=1;
private static final int t2=2;
private static final int t3=3;

@Override
protected void onCreate(Bundle savedInstanceState) {
33
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView)findViewById(R.id.textView);
}
public void fetchData(View view) {
tv.setText("In Main thread");
thread1.start();
thread2.start();
thread3.start();
}
Thread thread1=new Thread(new Runnable() {
@Override
public void run() {
for(int i=0;i<5;i++)
{
try
{
Thread.sleep(500);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
handler.sendEmptyMessage(t1);
}
}
});
Thread thread2=new Thread(new Runnable() {
@Override
public void run() {
for(int i=0;i<5;i++)
{
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
handler.sendEmptyMessage(t2);
}

}
});
Thread thread3=new Thread(new Runnable() {
@Override
public void run() {
for(int i=0;i<5;i++)
{
try
{
Thread.sleep(1500);
}
34
catch (InterruptedException e)
{
e.printStackTrace();
}
handler.sendEmptyMessage(t3);
}

}
});
Handler handler=new Handler() {
public void handleMessage(Message msg)
{
if(msg.what==t1)
{
tv.append("\nIn thread 1");
}
if(msg.what==t2)
{
tv.append("\nIn thread 2");
}
if(msg.what==t3)
{
tv.append("\nIn thread 3");
}
}
};
}

35
OUTPUT:

RESULT:
Thus to create an application to implement multithreading has been successfully done.

36
EX.No:7 Implement an application that writes data to the SD card.

Aim:
To implement an application that writes data to the SD data using Android studio.

Algorithm:

Step 1. Open a new project


Step 2. Configure your project, select Minimum SDK, add an activity and customize the activity.
Step 3. In ‘Design View’ of ‘activity_main.xml’, design a page for multithreading.
Step 4. Use the GUI components like
i. Layout  Linear Layout (Vertical).
ii. Widgets  Button and LargeText to display the information.

(Drag a PlainText from the Palette and place it in the design view

Drag 2 button from the Palette and place it in the design view, Right
click on it and change the text as ‘SAVE DATA’, ‘SHOW DATA’)

Step 5. In main.xml file add


the code below
Step 6. Now select MainActivity.java file and edit the java code (Given below).
Step 7. Next step is to set permission to write data in SD card. So go to
AndroidManifest.xml file. Type the following coding. The code should come before
<application> tab.
Step 8. <uses-permission
Step 9. android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-
permission>
Step 10. To run the program click run button and choose emulator under target device and
select your virtual device from the list and click Run button

37
activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:weightSum="1">

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_gravity="center_horizontal">
<requestFocus/>
</EditText>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SAVE DATA"
android:id="@+id/button1"
android:layout_gravity="center_horizontal" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SHOW DATA"
android:id="@+id/button2"
android:layout_gravity="center_horizontal" />

<TextView
android:layout_width="307dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/textView"
android:layout_weight="0.07" />

38
</LinearLayout>
</RelativeLayout>

MainActivity.java

package com.example.admin.exno9;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
Button save,load;
EditText message;
TextView t1;
String Message1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
save=(Button) findViewById(R.id.button1);
load=(Button) findViewById(R.id.button2);
message=(EditText) findViewById(R.id.editText);
t1=(TextView) findViewById(R.id.textView);
save.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Message1 =message.getText().toString();
try{
File sdcard=Environment.getExternalStorageDirectory();
File directory=new File(sdcard.getAbsolutePath()+"/MyDirectory");

39
directory.mkdirs();
File file=new File(directory,"textfile.txt");
FileOutputStream fou=new FileOutputStream(file);
OutputStreamWriter osw=new OutputStreamWriter(fou);
try{
osw.append(Message1);
osw.flush();
osw.close();
Toast.makeText(getBaseContext(),"DataSaved",Toast.LENGTH_LONG).show();
}catch(IOException e){
e.printStackTrace();
}
}catch (FileNotFoundException e){
e.printStackTrace();
}
}
});
load.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
try{
File sdcard=Environment.getExternalStorageDirectory();
File directory=new File(sdcard.getAbsolutePath()+"/MyDirectory");
File file=new File(directory,"textfile.txt");
FileInputStream fis=new FileInputStream(file);
InputStreamReader isr=new InputStreamReader(fis);
char[] data=new char[100];
String final_data="";
int size;
try{
while((size=isr.read(data))>0)
{
String read_data=String.copyValueOf(data,0,size);
final_data+=read_data;
data=new char[100];
}

Toast.makeText(getBaseContext(),"Message:"+final_data,Toast.LENGTH_LONG).show();
}catch(IOException e){
e.printStackTrace();
}
}catch (FileNotFoundException e){
e.printStackTrace();
}
}
});
}

40
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.admin.exno9" >
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-
permission>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
</application>
</manifest>
Output:

Result:
Thus the implementation of an application that writes data to the SD data using Android
studio was completed successfully.

41
Ex: No: 8 IMPLEMENT AN APPLICATION THAT CREATES
AN ALERT UPON RECEIVING A MESSAGE
AIM:
To implement an application that creates an alert upon receiving a message.

ALGORITHM:
Step 1. Open a new project
Step 2. Configure your project, select Minimum SDK, add an activity and customize the activity.
Step 3. In ‘Design View’ of ‘activity_main.xml’, design a page for multithreading.
Step 4. Use the GUI components like
i. Layout  Linear Layout (Vertical) and Linear Layout (Horizontal) to place the
button.
ii. Widgets  Button and LargeText to display the information.
iii. Text Fields  PlainText to get the information

(Drag a large text from the Palette and place it in


the design view, Right click on it and change the
text as ‘Message’

Drag a plaintext from the Palette and place it in


the design view.

Drag a button from the Palette and place it in the


design view, Right click on it and change the
text as ‘NOTIFY’)

Step 5. Under ‘Package Explorer’ (On the left hand side) double click on java and expand it.
Right click on ‘com.eg.adminpc.exno8’
Choose NewActivityBlankActivity.
Give the Activity name as ‘Welcome Activity’
Step 6. For the <Button> element, add the android:onClick attribute. (Under activity_main.xml)
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Notify"
android:id="@+id/button"
android:onClick="notify"/>

42
Step 7. A notification symbol(bulb) will be displayed on the LHS, click on it and Choose
“Create ‘notify(View)’in ‘MainActicity’”
Step 8. Under MainActivity.java include the code (below).
Step 9. Execute the program.

PROGRAM:

ActivityMain.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Message"
android:id="@+id/textView" />

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

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Notify"
android:id="@+id/button"
android:onClick="notify"/>

</LinearLayout>
</RelativeLayout>

43
MainActivity.java

package com.example.adminpc.ExNo8;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void notify(View view) {
EditText E1=(EditText)findViewById(R.id.editText);
Intent intent=new Intent(this,ResultActivity.class);
PendingIntent pending=PendingIntent.getActivity(this,0,intent,0);
Notification noti=new Notification.Builder(this)
.setContentTitle("NewMessage")
.setContentText(E1.getText().toString()).setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pending).build();
NotificationManager manager =
(NotificationManager)getSystemService(NOTIFICATION_SERVICE
);
noti.flags |=Notification.FLAG_AUTO_CANCEL;
manager.notify(0,noti);

}
}
ResultActivity.java
package com.example.adminpc.ExNo8;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;

public class ResultActivity extends ActionBarActivity {


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

44
Resultactivity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.adminpc.annalert.ResultActivity">

</RelativeLayout>

OUTPUT:

RESULT:
Thus the implementation of an application that creates an alert upon receiving a message
using Android studio was completed successfully.

45
Ex: No: 9 TO WRITE AN APPLICATION THAT CREATES ALARM CLOCK

AIM:
To write a mobile application that creates alarm clock.

ALGORITHM:
Step 1. Open a new project
Step 2. Configure your project, select Minimum SDK, add an activity and customize the activity
as ‘AlarmActivity’.
Step 3. In ‘Design View’ of ‘activity_alarm.xml’, design a page for setting alarm.
Step 4. Use the GUI components like
i. Layout  Linear Layout (Vertical)
ii. Widgets  ToggleButton and LargeText.
iii. Date & Time  TimePicker

(Drag a large text from the Palette and place it in


the design view, Right click on it and change the
text as ‘ALARM’

Drag a ‘TimePicker’ from the Palette and place


it in the design view.

Drag a ‘ToggleButton’ from the Palette and


place it in the design view

Drag a large text from the Palette and place it in


the design view)

Step 5. For the <ToggleButton> element, add the android:onClick attribute. (activity_alarm.xml)
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New ToggleButton"
android:id="@+id/toggleButton"
android:onClick="onToggleClicked"/>
Step 6. A notification symbol(bulb) will be displayed on the LHS, click on it and Choose
“Create ‘onToggleClicked(View)’in ‘AlarmActicity’”
Step 7. Under AlarmActivity.java include the code (below).
Step 8. Under ‘Package Explorer’ (On the left hand side) double click on java and expand it.
Right click on ‘com.eg.adminpc.ex9Alarm’
Choose NewJava Class

46
Give the Class name as ‘AlarmReceiver’
Step 9. Under AlarmReceiver.java include the code (below).
Step 10. Under ‘Package Explorer’ (On the left hand side) double click on java and expand it.
Right click on ‘com.eg.adminpc.ex9Alarm’
Choose NewJava Class
Give the Class name as ‘AlarmService’
Step 11. Under AlarmService.java include the code (below).
Step 12. Under ‘Package Explorer’ (On the left hand side) double click on manifests & expand it.
Double click on ‘AndroidManifest.xml’
Step 13. To use WakeLock one must request the android.permission.WAKE_LOCK permission in
an <uses-permission> element of the application's manifest
<uses-permission android:name="android.permission.WAKE_LOCK" />

<service
android:name=".AlarmService"
android:enabled="true" />
<receiver android:name=".AlarmReceiver" />

Step 14. Execute the program.


OUTPUT:

RESULT:
Thus the implementation of an application that creates an alert upon receiving a message using
Android studio was completed successfully.

47
EX:NO:10 DEVELOP A NATIVE APPLICATION THAT USES GPS
LOCATION INFORMATION
AIM
To write a mobile application that create a nature application that uses GPS Location information
ALGORITHM
Step 1. Open android studio and select android project.
Step 2. Give project name and select next.
Step 3. Configure your project,select Minimum SDK, add an Activity and customize the
Activity.
Step 4. In design view of Activity_main.xml design a page for setting GPS Location.
Step 5. Now select the MainActivity.Java file edit the java code.
Step 6. Add Android Manifest.xml code below.
Step 7. Next step is to set permission to uses GPS Location information.
i. So go to AndroidManifest.xml file.type the following coding.
The code should come before <application>tab.
<uses-permission android:name=”android.permission.ACCESS_FINE_LOCATION”/>
<uses-permission android:name=”android.permission.INTERNET”/>
Step 8. To run the program click run button and choose emulator under target device and select
your virtual device from the list and click Run button.
i. After running the AVD Manager,
Select Tools->Android->Android device monitor.
Choose open perspective->DDMS .
Step 9. Then Android device monitor that window then click Send Button.
Step 10. The Latitude and Longitude window will be displayed.
Step 11. Stop the program.

MainActivity.java
package com.example.admin.gps;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity implements LocationListener {


private Context context;

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

LocationManager locationManager;
locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}

48
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
String str = "Latitude: " + location.getLatitude() + "Longitude: " + location.getLongitude();

Toast.makeText(getBaseContext(), str, Toast.LENGTH_LONG).show();


}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}

@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}

@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
}

Activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

</RelativeLayout>

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.admin.gps" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />


<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"

49
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
</application>

</manifest>

50
RESULT:
Thus the implementation of an application that that uses GPS location information using Android
studio was completed successfully.

51
Ex: No: 11 RSS FEEDER

AIM:
To develop an application for RSS reader.

ALGORITHM:
Step 1. Open a new project
Step 2. Configure your project, select Minimum SDK, add an activity and customize the activity.
Step 3. In ‘Design View’ of ‘activity_main.xml’, design a page for RSS Feed.
Step 4. Use the GUI components like
i. TextField  PlainText
ii. Widgets  Button.

(Drag 4 LargeText from the Palette and place it


in the design view, Right Click and change its
name as ‘SAMPLE RSS FEEDER’, ‘Title’,
‘Link’ and ‘Description’

Drag 3 PlainText from the Palette and place it in


the design view,

Drag 2 ‘Button’ from the Palette and place it in


the design view. Right Click and change its
name as ‘FETCH’ and ‘RESULT’)

Step 5. Under MainActivity.java include the code (below).


Step 6. Under ‘Package Explorer’ (On the left hand side) double click on java and expand it.
Right click on ‘com.eg.adminpc.ex9Alarm’
Choose NewActivity  BlankActivity
Give the activity name as “Second”
Step 7. Under activity_second.java include the code (below).
Step 8. In ‘Design View’ of ‘activity_second.xml’, design a page for WebView
Step 9. Use the GUI components like
i. Widgets  WebView to display the web information.

52
(Drag a ‘WebView’ from the Palette and place it
in the design view)

Step 10. Generate ‘HandleXML.java’ and write the code.


Step 11. Next step is to set permission to access internet. Go to AndroidManifest.xml file.
Type the following coding. The code should come before <application> tab.
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17"/>
<uses-permission android:name="android.permission.INTERNET"/>
Step 12. Execute the program

Activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/ll">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="SAMPLE RSS FEEDER"
android:id="@+id/textView"

53
android:layout_gravity="center_horizontal" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Title"
android:id="@+id/textView2" />

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

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Link"
android:id="@+id/textView3" />

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

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Description"
android:id="@+id/textView4" />

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

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FETCH"
android:id="@+id/button"
android:layout_gravity="center_horizontal" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RESULT"
android:id="@+id/button2"
54
android:layout_gravity="center_horizontal" />
</LinearLayout>
</RelativeLayout>

MainActivity.java

package com.example.admin.ex11rss;

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

public class MainActivity extends ActionBarActivity {

EditText title,link,description;
Button b1,b2;
private String finalUrl="http://tutorialspoint.com/android/sampleXML.xml";
private HandleXML obj;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
title=(EditText) findViewById(R.id.editText);
link = (EditText)findViewById(R.id.editText2);
description = (EditText) findViewById(R.id.editText3);

b1=(Button)findViewById(R.id.button);
b2=(Button)findViewById(R.id.button2);

b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
obj = new HandleXML(finalUrl);
obj.fetchXML();

while(obj.parsingComplete);
title.setText(obj.getTitle());
link.setText(obj.getLink());
description.setText(obj.getDescription());
}
});

b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent in=new Intent(MainActivity.this,Second.class);
55
startActivity(in);
}
});
}
}

Activity_second.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.admin.ex11rss.Second">

<WebView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/webView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>

Second.java

package com.example.admin.ex11rss;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.webkit.WebView;

public class Second extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
WebView w1=(WebView)findViewById(R.id.webView);
w1.loadUrl("http://tutorialspoint.com/android/sampleXML.xml");
}
}

HandleXML.java

package com.example.admin.ex11rss;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

56
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class HandleXML {

private String title = "title";


private String link = "link";
private String description = "description";
private String urlString = null;
private XmlPullParserFactory xmlFactoryObject;
public volatile boolean parsingComplete = true;

public HandleXML(String url){


this.urlString = url;
}

public String getTitle(){


return title;
}

public String getLink(){


return link;
}

public String getDescription(){


return description;
}

public void parseXMLAndStoreIt(XmlPullParser myParser) {


int event;
String text=null;

try {
event = myParser.getEventType();

while (event != XmlPullParser.END_DOCUMENT) {


String name=myParser.getName();

switch (event){
case XmlPullParser.START_TAG:
break;

case XmlPullParser.TEXT:
text = myParser.getText();
break;

case XmlPullParser.END_TAG:

if(name.equals("title")){
57
title = text;
}

else if(name.equals("link")){
link = text;
}

else if(name.equals("description")){
description = text;
}

else{
}

break;
}

event = myParser.next();
}

parsingComplete = false;
}

catch (Exception e) {
e.printStackTrace();
}
}

public void fetchXML(){


Thread thread = new Thread(new Runnable(){
@Override
public void run() {

try {
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setReadTimeout(10000 /* milliseconds */);


conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);

// Starts the query


conn.connect();
InputStream stream = conn.getInputStream();

xmlFactoryObject = XmlPullParserFactory.newInstance();
XmlPullParser myparser = xmlFactoryObject.newPullParser();

myparser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
58
myparser.setInput(stream, null);

parseXMLAndStoreIt(myparser);
stream.close();
}

catch (Exception e) {
}
}
});
thread.start();
}
}

AndroidManifest.xml

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


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.admin.ex11rss" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
<activity
android:name=".Second"
android:label="@string/title_activity_second" >
</activity>
</application>

</manifest>

59
60
Ex: No: 12 APPLICATION TO CONVERT TEXT TO SPEECH

AIM:
To create an application that converts text to speech.

ALGORITHM:
Step 1. Open a new project
Step 2. Configure your project, select Minimum SDK, add an activity and customize the activity
Step 3. In ‘Design View’ of ‘activity_main.xml’, design a page for converting text message to
speech
Step 4. Use the GUI components like
i. Layout  Linear Layout(Vertical)
ii. Widgets  Plain LargeText
iii. Text Fields Plain Text.
iv. Widgets->Button, to convert text to speech.
Step 5. For the <Button> element, add the android:onClick attribute. (activity_alarm.xml)
<Button
android:text="text to speech"
android:id="@+id/button"
android:onClick="speakText"/>

A notification symbol(bulb) will be displayed on the LHS, click on it and Choose “Create
‘speckText(View)’in ‘MainActicity’”
Step 6. Under MainActivity.java include the code (below).
Step 7. A TextToSpeech instance can only be used to synthesize text once it has completed its
initialization. Implement the TextToSpeech.OnInitListener to be notified of the completion of the
initialization.
Step 8. Execute the program

(Drag 1 large texts from the Palette and place it


in the design view, Right click on it and change
the text as ‘Write Text’.

Drag and place the Text Fields Plain Text to


get the text.

Drag and place the widgets->button to convert


text to speech)

PROGRAM:
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
61
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<LinearLayout
android:orientation="vertical”
android:layout_width="fill_parent”
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:weightSum="1"
android:background="#ffdeffe9">

<TextView
android:layout_width="180dp"
android:layout_height="wrap_content"
android:text="Write text"
android:id="@+id/textView"
android:textSize="@dimen/abc_action_button_min_width_overflow_material"
android:textColor="#ff6a70ff" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_weight="0.13"
android:textColor="#f7080810" />

<Button
android:layout_width="159dp"
android:layout_height="wrap_content"
android:text="text to speech"
android:id="@+id/button"
android:onClick="speakText"
android:textColor="#ff4d77ff" />

<ImageView
android:layout_width="177dp"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_gravity="center_horizontal"
android:layout_weight="0.42"
android:src="@android:drawable/presence_audio_online"/>
</LinearLayout>
</RelativeLayout>
MainActivity.java
package com.example.admin.texttospeech;

62
import java.util.Locale;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {


TextToSpeech ttobj;
private EditText write;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
write = (EditText)findViewById(R.id.editText);
ttobj=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener()
{
@Override
public void onInit(int status)
{
if(status != TextToSpeech.ERROR)
{
ttobj.setLanguage(Locale.US);
}
}
});
}
@Override
public void onPause(){
if(ttobj !=null){
ttobj.stop();
ttobj.shutdown();
}
super.onPause();
}
public void speakText(View view){
String toSpeak = write.getText().toString();
Toast.makeText(getApplicationContext(), toSpeak, Toast.LENGTH_SHORT).show();
ttobj.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
}
}

63
OUTPUT:

RESULT:
Thus the application to convert text to speech was developed successfully.
“Knowing is not enough
We must apply
Willing is not enough
We must do”
More Details Visit : www.k7cloud.in
: http://k7training.blogspot.in
*************

64

Das könnte Ihnen auch gefallen