Beruflich Dokumente
Kultur Dokumente
AIM:
To create an application using GUI components and to change the font and color of the text in its
properties.
ALGORITHM:
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.
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;
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.
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:
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)
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;
9
float font=25;
int i=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* 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;
}
});
@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 5. Under ‘Package Explorer’ (On the left hand side) double click on java and expand it.
Right click on ‘com.eg.adminpc.exno2’
Choose NewActivityBlankActivity.
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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
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;
@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.
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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showResult=(EditText)findViewById(R.id.result_id);
}
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:
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;
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:
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.
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;
@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 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 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;
@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:
(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’)
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" />
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
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 NewActivityBlankActivity.
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;
@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;
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
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 NewJava 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 NewJava 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" />
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;
@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();
@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" />
<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" />
</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.
52
(Drag a ‘WebView’ from the Palette and place it
in the design view)
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;
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;
@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;
try {
event = myParser.getEventType();
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();
}
}
try {
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
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
</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
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;
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