CSE MAD Lab
CSE MAD Lab
No: 1 Develop an application that uses GUI components, Font and Colours
Date:
Aim:
To develop an android application that uses GUI components, Font and Colours
using android studio.
Procedure:
1. Open eclipse or android studio and select new android project.
2. Give project name and select next.
3. Choose the android version. Choose the lowest android version(Android 2.2) and
select next.
4. Enter the package name. Package name must be two word separated by comma and
click finish.
5. Go to package explorer in the left hand side. Select the project.
6. Go to “res“ folder and select layout. Double click the activitymain.xml file.
7. Now you can see the Graphics layout window.
8. Click the activitymain.xml file and type the code.
9. Go to project explorer and select “src” folder. Now select mainactivity.java file
and type the code.
10. Go to activitymain.xml and right click. Select run as option and select run
configuration.
11. Android output is present in the android emulator.
Program: activitymain.xml
<?xml version="1.0" encoding="utf-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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="helloapp.example.com.demoapp.MainActivity"
android:background="#03acda"
android:clickable="false"
android:id="@+id/canvas">
<TextView
1
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Username"
android:id="@+id/txtUsername"
android:textColorHighlight="#4d77e2"
android:textIsSelectable="true"
android:typeface="serif"
android:textStyle="bold|italic"
android:textColor="#ffffff"
android:textSize="20dp"
android:layout_marginBottom="5dp"
android:layout_above="@+id/edtUsername"
android:layout_alignParentLeft="true"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/edtUsername"
android:hint="Enter Your Username"
android:textColorHint="#7a7a7a"
android:layout_above="@+id/txtPassword"
android:layout_alignLeft="@id/txtPassword"
android:layout_alignRight="@+id/imageView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Password"
android:id="@+id/txtPassword"
android:textStyle="bold|italic"
android:typeface="serif"
android:textColor="#ffffff"
android:textIsSelectable="false"
android:textSize="20dp"
android:layout_centerVertical="true"
android:layout_alignRight="@+id/txtUsername" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/edtPassword"
android:hint="Enter Your Password"
android:textColorHint="#7a7a7a"
android:layout_below="@+id/txtPassword"
android:layout_alignLeft="@+id/txtPassword"
android:layout_alignRight="@+id/textView" />
<Button
android:layout_width="wrap_content"
2
android:layout_height="wrap_content"
android:text="Change Background Color"
android:id="@+id/btnLogin"
android:typeface="serif"
android:layout_below="@+id/edtPassword"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Note: Username "CSE" and Password "EBET""
android:id="@+id/textView"
android:textStyle="normal|italic"
android:typeface="serif"
android:textColor="#fbfbfb"
android:layout_below="@+id/btnLogin"
android:layout_centerHorizontal="true"
android:layout_marginTop="41dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@drawable/ebetlogo"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_above="@+id/txtUsername" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Font"
android:id="@+id/btnFont"
android:layout_below="@+id/textView"
android:layout_alignLeft="@+id/btnLogin"
android:layout_alignStart="@+id/btnLogin"
android:layout_alignRight="@+id/btnLogin"
android:layout_alignEnd="@+id/btnLogin" />
</RelativeLayout>
mainactivity.java
package com.sg.gui;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
3
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
Button btnLogin;
Button btnFont;
EditText edtUsername, edtPassword;
RelativeLayout canvas;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnLogin = (Button)findViewById(R.id.btnLogin);
btnFont = (Button)findViewById(R.id.btnFont);
edtUsername = (EditText)findViewById(R.id.edtUsername);
edtPassword =(EditText)findViewById(R.id.edtPassword);
canvas = (RelativeLayout)findViewById(R.id.canvas);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
btnFont.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
TextView tx = (TextView)findViewById(R.id.textView);
tx.setTypeface(custom_font);
4
}
});
}
}
Output:
5
Result:
Thus the android application that uses GUI components, Font and Colours is
developed and tested using android studio.
6
Ex.No: 2 Develop an application that uses Layout Managers and event listeners
Date:
Aim:
To develop an android application that uses Layout Managers and event listeners
using android studio.
Procedure:
1. Open eclipse or android studio and select new android project.
2. Give project name and select next.
3. Choose the android version. Choose the lowest android version(Android 2.2) and
select next.
4. Enter the package name. Package name must be two word separated by comma and
click finish.
5. Go to package explorer in the left hand side. Select the project.
6. Go to “res“ folder and select layout. Double click the activitymain.xml file.
7. Now you can see the Graphics layout window.
8. Click the activitymain.xml file and type the code.
9. Go to project explorer and select “src” folder. Now select mainactivity.java file
and type the code.
10. Go to activitymain.xml and right click. Select run as option and select run
configuration.
11. Android output is present in the android emulator.
Program: activitymain.xml
<?xml version="1.0" encoding="utf-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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Layout and Listener Program"
7
android:id="@+id/txtSample"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/txtSample"
android:layout_centerHorizontal="true">
<ImageView
android:layout_width="111dp"
android:layout_height="111dp"
android:id="@+id/imageView"
android:layout_gravity="center_horizontal"
android:src="@drawable/number1" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/tableRow"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
android:id="@+id/btnClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Long Click Me"
android:id="@+id/btnLongClick" />
</TableRow>
</TableLayout>
<GridLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:id="@+id/lytGrid">
<Button
android:layout_width="202dp"
android:layout_height="156dp"
android:text="Click or Long CLick \n Me"
android:id="@+id/btnAll"
8
android:layout_column="3"
android:layout_row="0"
android:layout_columnSpan="1"
android:layout_rowSpan="2" />
<Button
android:layout_width="143dp"
android:layout_height="match_parent"
android:text="Show \nMy \nName"
android:id="@+id/btnShowName"
android:layout_row="1"
android:layout_column="2"
android:layout_rowSpan="3" />
<EditText
android:layout_width="match_parent"
android:layout_height="62dp"
android:id="@+id/txtName"
android:layout_row="3"
android:layout_column="3"
android:hint="Enter your Name"
android:layout_columnSpan="1"
android:layout_rowSpan="1" />
</GridLayout>
</LinearLayout>
</RelativeLayout>
Program: mainactivity.java
package com.sg.layout;
import android.content.ClipData;
import android.content.ClipDescription;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.DragEvent;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.sg.layout.R;
9
Button clickBtn, longClickBtn, allBtn, btnShow;
TextView sample;
EditText nameTxt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
clickBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Click event!",
Toast.LENGTH_SHORT).show();
}
});
longClickBtn.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
allBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "You Just Clicked Me!",
Toast.LENGTH_SHORT).show();
}
});
allBtn.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Toast.makeText(getApplicationContext(), "You clicked me for long!",
Toast.LENGTH_SHORT).show();
return false;
}
});
10
btnShow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sample.setText(nameTxt.getText().toString());
}
});
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
11
Output:
Result:
Thus the android application that Layout Managers and event listeners is developed
and tested using android studio.
12
Ex.No: 3 Write an application that draws basic graphical primitives on the screen
Date:
Aim:
To write an application that draws basic graphical primitives on the screen using
android studio.
Procedure:
1. Open eclipse or android studio and select new android project.
2. Give project name and select next.
3. Choose the android version. Choose the lowest android version(Android 2.2) and
select next.
4. Enter the package name. Package name must be two word separated by comma and
click finish.
5. Go to package explorer in the left hand side. Select the project.
6. Go to “res“ folder and select layout. Double click the activitymain.xml file.
7. Now you can see the Graphics layout window.
8. Click the activitymain.xml file and type the code.
9. Go to project explorer and select “src” folder. Now select mainactivity.javaand
create samplecanvas.java files and type the code.
10. Go to activitymain.xml and right click. Select run as option and select run
configuration.
11. Android output is present in the android emulator.
Program: activitymain.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.sg.graphic.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
13
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
mainactivity.java
package com.sg.graphic;
import android.app.Activity;
import android.os.Bundle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(drawView);
}
}
Canvas.java
package com.sg.graphic;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
14
public Canvas1(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
paint.setColor(Color.BLUE);
paint.setStrokeWidth(0);
paint.setColor(Color.GREEN);
canvas.drawCircle(200, 400, 75, paint);
paint.setColor(Color.MAGENTA);
paint.setStrokeWidth(4);
canvas.drawLine(100, 540, 300, 540, paint);
paint.setColor(getResources().getColor(R.color.teal_blue));
canvas.drawText("Graphical Primitive",100,50,paint);
paint.setColor(Color.RED);
paint.setStrokeWidth(6);
canvas.drawPoint(200,600,paint);
}
}
15
Output:
Result:
Thus the application that draws basic graphical primitives on the screen is developed
and tested using android studio.
16
Ex.No: 4 Develop an application that makes use of database
Date:
Aim:
To develop an application that makes use of database using android studio.
Procedure:
1. Open eclipse or android studio and select new android project.
2. Give project name and select next.
3. Choose the android version. Choose the lowest android version(Android 2.2) and
select next.
4. Enter the package name. Package name must be two word separated by comma and
click finish.
5. Go to package explorer in the left hand side. Select the project.
6. Go to “res“ folder and select layout. Double click the activitymain.xml file.
7. Now you can see the Graphics layout window.
8. Click the activitymain.xml file and type the code.
9. Go to project explorer and select “src” folder. Now select mainactivity.java file
and type the code.
10. Go to activitymain.xml and right click. Select run as option and select run
configuration.
11. Android output is present in the android emulator.
Program: activitymain.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myLayout"
android:stretchColumns="0"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:text="@string/title"
android:layout_x="110dp"
android:layout_y="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:text="@string/roll_no"
android:layout_x="30dp"
android:layout_y="50dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText android:id="@+id/editRollno"
17
android:inputType="number"
android:layout_x="150dp"
android:layout_y="50dp"
android:layout_width="150dp"
android:layout_height="40dp"/>
<TextView android:text="@string/name"
android:layout_x="30dp"
android:layout_y="100dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText android:id="@+id/editName"
android:inputType="text"
android:layout_x="150dp"
android:layout_y="100dp"
android:layout_width="150dp"
android:layout_height="40dp"/>
<TextView android:text="@string/marks"
android:layout_x="30dp"
android:layout_y="150dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText android:id="@+id/editMarks"
android:inputType="number"
android:layout_x="150dp"
android:layout_y="150dp"
android:layout_width="150dp"
android:layout_height="40dp"/>
<Button android:id="@+id/btnAdd"
android:text="@string/add"
android:layout_x="30dp"
android:layout_y="200dp"
android:layout_width="100dp"
android:layout_height="40dp"/>
<Button android:id="@+id/btnDelete"
android:text="@string/delete"
android:layout_x="150dp"
android:layout_y="200dp"
android:layout_width="100dp"
android:layout_height="40dp"/>n
<Button android:id="@+id/btnModify"
android:text="@string/modify"
android:layout_x="30dp"
android:layout_y="250dp"
android:layout_width="100dp"
android:layout_height="40dp"/>
<Button android:id="@+id/btnView"
android:text="@string/view"
android:layout_x="150dp"
android:layout_y="250dp"
android:layout_width="100dp"
android:layout_height="40dp"/>
<Button android:id="@+id/btnViewAll"
android:text="@string/view_all"
18
android:layout_x="30dp"
android:layout_y="300dp"
android:layout_width="100dp"
android:layout_height="40dp"/>
</AbsoluteLayout>
mainactivity.java
package com.sg.database;
import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
}
public void showMessage(String title,String message)
{
Builder builder=new Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
public void clearText()
{
21
editRollno.setText("");
editName.setText("");
editMarks.setText("");
editRollno.requestFocus();
}
}
Output:
Result:
Thus the application that makes use of database is developed and tested using
android studio.
22
Ex.No: 5 Implement an application that implements Multithreading
Date:
Aim:
To implement an application that implements Multithreading using android studio.
Procedure:
1. Open eclipse or android studio and select new android project.
2. Give project name and select next.
3. Choose the android version. Choose the lowest android version(Android 2.2) and
select next.
4. Enter the package name. Package name must be two word separated by comma and
click finish.
5. Go to package explorer in the left hand side. Select the project.
6. Go to “res“ folder and select layout. Double click the activitymain.xml file.
7. Now you can see the Graphics layout window.
8. Click the activitymain.xml file and type the code.
9. Go to project explorer and select “src” folder. Now select mainactivity.java file
and type the code.
10. Go to activitymain.xml and right click. Select run as option and select run
configuration.
11. Android output is present in the android emulator.
Program: activitymain.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="false"
android:max="10"
android:padding="4dip" >
</ProgressBar>
<TextView
android:id="@+id/textView1"
23
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" >
</TextView>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="startProgress"
android:text="Start Progress" >
</Button>
</LinearLayout>
Mainactivity.java
package com.sg.multithread;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progress = (ProgressBar) findViewById(R.id.progressBar1);
text = (TextView) findViewById(R.id.textView1);
Output:
Result:
Thus the application that implements Multithreading is implemented and tested
using android studio.
25
Ex.No: 6 Develop a native application that uses GPS location information
Date:
Aim:
To develop a native application that uses GPS location information using android
studio.
Procedure:
1. Open eclipse or android studio and select new android project.
2. Give project name and select next.
3. Choose the android version. Choose the lowest android version(Android 2.2) and
select next.
4. Enter the package name. Package name must be two word separated by comma and
click finish.
5. Go to package explorer in the left hand side. Select the project.
6. Go to “res“ folder and select layout. Double click the activitymain.xml file.
7. Now you can see the Graphics layout window.
8. Click the activitymain.xml file and type the code.
9. Go to project explorer and select “src” folder. Now select mainactivity.java file
and type the code.
10. Go to activitymain.xml and right click. Select run as option and select run
configuration.
11. Android output is present in the android emulator.
Program: activitymain.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/show_Location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show_Location"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
26
Mainactivity.java
package com.sg.gps;
importandroid.app.AlertDialog;
import android.app.Service;
importandroid.content.Context;
importandroid.content.DialogInterface;
importandroid.content.Intent;
importandroid.location.Location;
importandroid.location.LocationListener;
importandroid.location.LocationManager;
importandroid.os.Bundle;
importandroid.os.IBinder;
importandroid.provider.Settings;
27
use of RSS Feed
isNetworkEnabled=locationManager.isProviderEnabled(LocationM
anager.NETWORK_PROVIDER);
if(!isGPSEnabled && !isNetworkEnabled){
}else{
this.canGetLocation=true;
if(isNetworkEnabled){
Develop
an
application that makes
use of database
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES,this);
}
if(locationManager!=null){
Write a
mobile
application that creates
alarm clock in android
location=locationManager.getLastKnownLocation(LocationManager
.NETWORK_PROVIDER);
if(location !=null){
latitude=location.getLatitude();
longtitude=location.getLongitude();
FAMOUS QUOTES
}
}
}
if(isGPSEnabled){
if(location==null){
locationManager.requestLocationUpdates(LocationManager.GPS_PR
OVIDER,MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES,
this);
if(locationManager!=null){
location=locationManager.getLastKnownLocation(LocationManager
.GPS_PROVIDER);
if(location!=null){
latitude=location.getLatitude();
longtitude=location.getLongitude();
}
Androidmaifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sg.gps">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
28
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
Output:
Result:
Thus the application native application that uses GPS location information is
developed and tested using android studio.
29
Ex.No: 7 Implement an application that writes data to the SD card
Date:
Aim:
To implement an application that writes data to the SD card using android studio.
Procedure:
1. Open eclipse or android studio and select new android project.
2. Give project name and select next.
3. Choose the android version. Choose the lowest android version(Android 2.2) and
select next.
4. Enter the package name. Package name must be two word separated by comma and
click finish.
5. Go to package explorer in the left hand side. Select the project.
6. Go to “res“ folder and select layout. Double click the activitymain.xml file.
7. Now you can see the Graphics layout window.
8. Click the activitymain.xml file and type the code.
9. Go to project explorer and select “src” folder. Now select mainactivity.java file
and type the code.
10. Go to activitymain.xml and right click. Select run as option and select run
configuration.
11. Android output is present in the android emulator.
Program: activitymain.xml
<?xml version="1.0" encoding="utf-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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.linuxpert.myfileapp.MainActivity">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/txtWrite"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
30
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Write"
android:id="@+id/btnWrite"
android:layout_below="@+id/txtWrite"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Read"
android:id="@+id/btnRead"
android:layout_below="@+id/txtWrite"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/txtRead"
android:layout_below="@+id/btnWrite"
android:layout_centerHorizontal="true"
android:layout_marginTop="44dp" />
</RelativeLayout>
Mainactivity.java
package com.sg.myfileapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.BufferedReader;
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;
public class MainActivity extends AppCompatActivity {
EditText txtWrite;
Button btnWrite,btnRead;
TextView txtRead;
@Override
protected void onCreate(Bundle savedInstanceState) {
31
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtWrite = (EditText)findViewById(R.id.txtWrite);
txtRead = (TextView)findViewById(R.id.txtRead);
btnWrite = (Button)findViewById(R.id.btnWrite);
btnRead = (Button)findViewById(R.id.btnRead);
btnWrite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String text = txtWrite.getText().toString();//read data from text field
try {
FileOutputStream ofStream = new FileOutputStream(new
File(getFilesDir(),"demo"));
/*
* file output stream which writes into a new file named demo with the path we
get from
* getFilesDir() function which corresponds to internal memory.
* - getCacheDir() - cache memory
*/
OutputStreamWriter osWriter = new OutputStreamWriter(ofStream);
//create a new writer from ofStream
osWriter.write(text);
//writes data into the file
osWriter.close();
ofStream.close();
//closing stream and writer
} catch (IOException e) {
e.printStackTrace();
}
}
});
btnRead.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
FileInputStream ifStream = new FileInputStream(new File(getFilesDir(),"demo"));
/* creates input stream for reading from the file*/
BufferedReader bReader = new BufferedReader(new
InputStreamReader(ifStream));
/* Here we created a new InputStreamReader from the ifStream.
* (Like we created an OutputStreamWriter from ofStream).
* And we've created a BufferedReader from this InputStreamReader.
* The buffered reader is used to read data from a file as Strings.
*/
String read=""; //creating an empty string to save the read data from file.
String line;
/* Here we read the file line by line so String line is used to store the data
* of the current line the reader pointer is positioned at.
*/
while((line=bReader.readLine())!=null){
/*confirms the line is not null so as to execute loop.*/
32
/*If it is null it means the pointer is at the end of file*/
read+=line+"\n";
}
bReader.close();
ifStream.close();
//closing stream and reader
txtRead.setText(read); //setting the text of the text view
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
Output:
Result:
Thus the application that writes data to the SD card is implemented and tested using
android studio.
33
Ex.No: 8 Implement an application that creates an alert upon
Date: receiving a message
Aim:
To implement an application that creates an alert upon receiving a message using
android studio.
Procedure:
1. Open eclipse or android studio and select new android project.
2. Give project name and select next.
3. Choose the android version. Choose the lowest android version(Android 2.2) and
select next.
4. Enter the package name. Package name must be two word separated by comma and
click finish.
5. Go to package explorer in the left hand side. Select the project.
6. Go to “res“ folder and select layout. Double click the activitymain.xml file.
7. Now you can see the Graphics layout window.
8. Click the activitymain.xml file and type the code.
9. Go to project explorer and select “src” folder. Now select mainactivity.java file
and type the code.
10. Go to activitymain.xml and right click. Select run as option and select run
configuration.
11. Android output is present in the android emulator.
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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:id="@+id/simpleDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Alert Dialog" />
<Button
34
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show Alert Dialog"
android:layout_below="@+id/simpleDialog"
android:layout_marginTop="16dp"
android:onClick="showDialog"/>
</RelativeLayout>
Mainactivity.java
package com.sg.alertdialog;
// Simple android alert dialog example
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
@Override
public boolean onOptionsItemSelected(MenuItem item) {
35
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Output:
Result:
Thus the application that creates an alert upon receiving a message is implemented
and tested using android studio.
36
Ex.No: 9 Develop an application that makes use of RSS Feed
Date:
Aim:
To develop an application that makes use of RSS Feed using android studio.
Procedure:
1. Open eclipse or android studio and select new android project.
2. Give project name and select next.
3. Choose the android version. Choose the lowest android version(Android 2.2) and
select next.
4. Enter the package name. Package name must be two word separated by comma and
click finish.
5. Go to package explorer in the left hand side. Select the project.
6. Go to “res“ folder and select layout. Double click the activitymain.xml file.
7. Now you can see the Graphics layout window.
8. Click the activitymain.xml file and type the code also create supporting layout files.
9. Go to project explorer and select “src” folder. Now select mainactivity.java file
and type the code also create the supporting java codes.
10. Go to activitymain.xml and right click. Select run as option and select run
configuration.
11. Android output is present in the android emulator.
Program: activitymain.xml
<?xml version="1.0" encoding="utf-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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.linuxpert.mitcourses.MainActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/lstMain" />
</RelativeLayout>
37
activity_rss_loader.xml
<?xml version="1.0" encoding="utf-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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.linuxpert.mitcourses.RssLoader">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/lstRssFeed"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/feedProgress"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:indeterminate="false" />
</RelativeLayout>
main_list_layout.xml
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="1"
android:id="@+id/txtIndex"
android:textStyle="normal"
android:textSize="20sp"
android:layout_marginRight="10dp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Title"
38
android:id="@+id/txtTitle"
android:textSize="20sp"
android:textStyle="normal" />
</LinearLayout>
rss_list_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Title"
android:id="@+id/txtTitle"
android:textStyle="bold"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="http://www.example.com/post1"
android:id="@+id/txtLink" />
</LinearLayout>
mainactivity.java
package com.sg.mitcourses;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*END OF LIST*/
mainList.setOnItemClickListener(listener);
}
}
MainListAdapter.java
package com.sg.mitcourses;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
int layoutResourceId;
Context context;
ArrayList<Course> list;
this.layoutResourceId = resource;
this.context = context;
this.list = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
40
MainListHolder holder = null;
if(row==null){
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId,parent,false);
row.setTag(holder);
}
else
holder = (MainListHolder)row.getTag();
holder.indexTxt.setText(String.valueOf(position+1));
holder.titleTxt.setText(course.getTitle());
return row;
}
MainListItemClickListener.java
package com.sg.mitcourses;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import java.util.ArrayList;
ArrayList<Course> list;
Activity activity;
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Course course = list.get(position);
41
Intent intent = new Intent(activity,RssLoader.class);
intent.putExtra("title",course.getTitle());
intent.putExtra("link",MainActivity.rss_path+course.getUrl());
/*Remember we set the rss_path variable private so it can be called by 'dot' (.)
operator.*/
activity.startActivity(intent);
}
}
RssItem.java
package com.sg.mitcourses;
RssItemClickListener.java
package com.sg.mitcourses;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.AdapterView;
import java.util.ArrayList;
Activity activity;
ArrayList<RssItem> list;
42
public RssItemClickListener(Activity activity, ArrayList<RssItem> list) {
this.activity = activity;
this.list = list;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
activity.startActivity(intent);
//opens the link in a new browser window
}
}
RssListAdapter.java
package com.sg.mitcourses;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import org.w3c.dom.Text;
import java.util.ArrayList;
int layoutResourceId;
Context context;
ArrayList<RssItem> list;
this.layoutResourceId = resource;
this.context = context;
this.list = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
RssItemHolder holder = null;
if(row==null){
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
43
row = inflater.inflate(layoutResourceId,parent,false);
holder.titleTxt = (TextView)row.findViewById(R.id.txtTitle);
holder.linkTxt = (TextView)row.findViewById(R.id.txtLink);
row.setTag(holder);
}
else
holder = (RssItemHolder)row.getTag();
holder.titleTxt.setText(item.getTitle());
holder.linkTxt.setText(item.getLink());
return row;
}
RssLoader.java
package com.sg.mitcourses;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;
import java.util.ArrayList;
Activity local;
ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rss_loader);
local = this;
44
progressBar = (ProgressBar) findViewById(R.id.feedProgress);
Intent i = getIntent();
//gets the root intent
String title = i.getStringExtra("title");
//gets the String supplied with the tag title
String url = i.getStringExtra("link");
//gets the String supplied with the tag link
//actionbar
getSupportActionBar().setTitle(title);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
protected ArrayList<RssItem> doInBackground(String... params) {
try {
RssReader rssReader = new RssReader(params[0]);
//create rss reader
return rssReader.getItems();
//returns items parsed from rss file
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(ArrayList<RssItem> rssItems) {
if(rssItems!=null) {
ListView rssList = (ListView) findViewById(R.id.lstRssFeed);
rssList.setAdapter(adapter);
rssList.setOnItemClickListener(new RssItemClickListener(local,rssItems));
progressBar.setVisibility(View.INVISIBLE);
45
}
@Override
protected void onPreExecute() {
Toast.makeText(local,"Loading Feed",Toast.LENGTH_LONG).show();
}
}
}
RssParseHandler.java
package com.sg.mitcourses;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import java.util.ArrayList;
public RssParseHandler() {
rssItems = new ArrayList<>();
}
@Override
public void startElement(String uri, String localName, String qName, Attributes
attributes) throws SAXException {
if("item".equals(qName)){
currentItem = new RssItem(); //create a new RssItem to store data
} else if ("title".equals(qName)){
parsingTitle = true;
}
else if ("link".equals(qName)){
parsingLink = true;
}
46
@Override
public void endElement(String uri, String localName, String qName) throws
SAXException {
if("item".equals(qName)){
rssItems.add(currentItem);
currentItem = null;
} else if ("title".equals(qName)){
parsingTitle = false;
}
else if ("link".equals(qName)){
parsingLink = false;
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
if(parsingTitle){
if(currentItem!=null) {
currentItem.setTitle(new String(ch, start, length)); //sets title of the current item
}
} else if (parsingLink){
if (currentItem!=null){
currentItem.setLink(new String(ch, start, length)); //sets link of the current item
}
/*
* Here we used if(parsingTitle), if(parsingLink).
* These indicators indicate whether title or link is executed.
* It helps to avoid the handler from parsing other unwanted characters inside other rss
tags.
*/
}
}
RssReader.java
package com.sg.mitcourses;
import java.util.ArrayList;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
47
public RssReader(String rssUrl) {
this.rssUrl = rssUrl;
}
parser.parse(rssUrl,handler);
//parse the rss file located at rssUrl using the parsing handler 'handler'.
return handler.getRssItems();
//returns the list of items parsed
}
Course.java
package com.blogspot.shuttereditz.mitcourses;
48
Output:
Result:
Thus the application that makes use of RSS Feed is developed and tested using
android studio.
49