MAD Lab Manual
MAD Lab Manual
AIM:
To develop an Android Application that uses GUI components , fonts and colors.
PROCEDURE:
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20sp"
android:gravity="center"
android:text="HELLO WORLD"
android:textSize="20sp"
android:textStyle="bold" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Change font size"
android:textSize="20sp" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Change color"
android:textSize="20sp" />
</LinearLayout>
Main Activity Coding:
import android.app.Activity;
import android.graphics.*;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity
{
float font =24;
int i=1;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView t1=(TextView) findViewById(R.id.textView);
Button b1 = (Button) findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
t1.setTextSize(font);
font=font+4;
font=20;
if(font==40)
}
});
switch(i)
{
case 1:
t1.setTextColor(Color.parseColor("#0000FF"));
break;
case 2:
t1.setTextColor(Color.parseColor("#00FF00"));
break;
case 3:
t1.setTextColor(Color.parseColor("#FF0000"));
break;
case 4:
t1.setTextColor(Color.parseColor("#800000"));
break;
}
i++;
if(i==5)
i=1;
}
});
}
}
OUTPUT:
Result:
The application for GUI components, fonts and colors has been created successfully and
the result is verified.
EX NO :02
AIM:
To develop an Android Application for an Intent and Activity.
PROCEDURE:
<RelativeLayoutxmlns: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=".FirstActivity">
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="124dp"
android:ems="10" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="46dp"
android:text="GO" />
</RelativeLayout>
Main Activity:
Packagecom.example.intent;
importandroid.os.Bundle;
importandroid.app.Activity;
importandroid.content.Intent;
importandroid.view.Menu;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.Button;
importandroid.widget.EditText;
public class MainActivity extends Activity {
EditTextet;
Button btn;
@Override
Second.xml coding
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FINISH" />
</LinearLayout>
Second MainActivity:
packagecom.example.intent;
importandroid.os.Bundle;
importandroid.app.Activity;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.Button;
importandroid.widget.TextView;
public class SecondMainActivity extends Activity {
TextViewtv;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second_main);
tv=(TextView)findViewById(R.id.textView1);
btn=(Button)findViewById(R.id.button1);
tv.setText(getIntent().getExtras().getString("myname").toString());
btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0){
finish();
}
});
}
}
Android Manifest.xml:
</manifest>
OUTPUT:
RESULT:
Hence the android application that uses for an Intent and Activity has been created,
deployed and the output is verified.
EX No : 03 Develop an application that uses Layout Manager
AIM :
To develop a simple android application that uses Layout Manager and Event Listeners
PROCEDURE:
<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" >
<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/fstnum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="15dp"
android:layout_marginTop="60dp"
android:text="First Num" />
<ImageButton android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/secnum"
android:layout_centerVertical="true"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/secnum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/fstnum"
android:layout_below="@+id/fstnum"
android:layout_marginTop="32dp"
android:text="Second Num" />
<EditText android:id="@+id/secondnum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/secnum"
android:layout_alignBottom="@+id/secnum"
android:layout_alignParentRight="true"
android:layout_toRightOf="@+id/secnum"
android:ems="10" />
<EditText
android:id="@+id/firstnum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/fstnum"
android:layout_alignBottom="@+id/fstnum"
android:layout_alignParentRight="true"
android:ems="10" />
</RelativeLayout>
Main Activity.java
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText firstnum=(EditText)findViewById(R.id.firstnum);
final EditText secondnum=(EditText)findViewById(R.id.secondnum);
ImageButton imgbtn=(ImageButton)findViewById(R.id.imageButton1);
imgbtn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
if(firstnum.getText().toString().isEmpty()||
secondnum.getText().toString().isEmpty());
{
int num1=Integer.parseInt(firstnum.getText().toString());
int num2=Integer.parseInt(secondnum.getText().toString());
Toast.makeText(getApplicationContext(), "sum of " + (num1 + num2),
Toast.LENGTH_SHORT).show();
}
}
});
}
}
OUTPUT :
RESULT :
Hence the android application that uses for an Intent and Activity has been created,
deployed and the output is verified.
EX NO :04 Write an application that draws basic graphical primitives
AIM:
To develop a simple application that draws basic graphicalprimitives on
the screen.
PROCEDURE:
Step1: Open eclipse or android studio and create new project
Step2: Select our project in the project explorer
Step3: Go to res folder and select layout Double click the main xml file
Step4: Type the code for main.xml or drag and drop various componentsused in our program
Step5: Drag and drop relative layout and change its properties
Step6 : Drag and drop image view and change its properties according to our programs
Step7: Screen layout can be viewed by clicking graphics layout tab
Step8: Include necessary files
Step9: Override OnCreate() function
Step10: Create image view and initialize its using id for somecomponents
used in the xml program
Step11: Save the program
Step12: Run the program
Step13: Output can be viewed in the android emulator.
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: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/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<ImageView android:id="@+id/imageView1"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="130dp"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
MainActivity.java
package com.example.graphical29;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.widget.ImageView;
public class MainActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Creating a Bitmap
Bitmap bg = Bitmap.createBitmap(720, 1280,
Bitmap.Config.ARGB_8888);
//Setting the Bitmap as background for the ImageView ImageView i =
(ImageView) findViewById(R.id.imageView1);
i.setBackgroundDrawable(new BitmapDrawable(bg));
//Creating the Paint Object and set its color & TextSizePaint paint =
new Paint();
paint.setColor(Color.BLUE);
paint.setTextSize(50);
AIM:
To develop an application that makes use of RSS Feed.
PROCEDURE:
<ListView android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
MainActivity.Java:
package com.example.rssfeed;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);new
MyAsyncTask().execute();
}
{
headlines = new ArrayList();links = new ArrayList();
try
{
factory =
XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp = factory.newPullParser();
(eventType != XmlPullParser.END_DOCUMENT)
{
if (eventType == XmlPullParser.START_TAG)
{
if (xpp.getName().equalsIgnoreCase("item"))
{
insideItem = true;
}
else if (xpp.getName().equalsIgnoreCase("title"))
{
if(insideItem)
headlines.add(xpp.nextText()); //extract the headline
}
else if (xpp.getName().equalsIgnoreCase("link"))
{
if (insideItem) links.add(xpp.nextText());
}
}
else if(eventType==XmlPullParser.END_TAG &&
xpp.getName().equalsIgnoreCase("item"))
{
insideItem=false;
}
eventType = xpp.next(); //move to next element
}
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (XmlPullParserException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return null;
}
protected void onPostExecute(ArrayAdapter adapter)
{
adapter = new ArrayAdapter(MainActivity.this,
android.R.layout.simple_list_item_1, headlines);
setListAdapter(adapter);
}
}
@Override
startActivity(intent);
Date:
AIM:
PROCEDURE:
Activity.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/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Main thread" />
<Button android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="24dp"
android:onClick="fetchData" android:text="Start
MULTITHREAD" />
</RelativeLayout>
Main_Activity.java
package com.example.multithearding;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;
@SuppressLint("HandlerLeak") public class MainActivity extends Activity {
private TextView tvOutput; private static
final int t1 = 1;private static final int t2 = 2;
private static final int t3 = 3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvOutput = (TextView) findViewById(R.id.textView1);
}
public void fetchData(View v)
{
tvOutput.setText("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(1000);
}
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);
}
}
Result:
AIM:
PROCEDURE:
<TimePicker
android:id="@+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="20dp"
android:checked="false"
android:onClick="OnToggleClicked" />
</LinearLayout>
MainActivity.Java:
package com.example.exno07;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TimePicker;
import android.widget.Toast;
import android.widget.ToggleButton;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity
{
TimePicker alarmTimePicker;
PendingIntent pendingIntent;
AlarmManager alarmManager;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
alarmTimePicker = (TimePicker) findViewById(R.id.timePicker);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
}
public void OnToggleClicked(View view)
{
long time;
if (((ToggleButton) view).isChecked())
{
Toast.makeText(MainActivity.this, "ALARM ON", Toast.LENGTH_SHORT).show();
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getCurrentHour());
calendar.set(Calendar.MINUTE, alarmTimePicker.getCurrentMinute());
Intent intent = new Intent(this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
time=(calendar.getTimeInMillis()-(calendar.getTimeInMillis()%60000));
if(System.currentTimeMillis()>time)
{
if (calendar.AM_PM == 0)
time = time + (1000*60*60*12);
else
time = time + (1000*60*60*24);
}
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time, 10000,
pendingIntent);
}
else
{
alarmManager.cancel(pendingIntent);
Toast.makeText(MainActivity.this, "ALARM OFF", Toast.LENGTH_SHORT).show();
}
}
}
AlarmReceiver.Java:
package com.example.exno07;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.widget.Toast;
The application for alarm clock has been created successfully and the result is verified.
Ex No :08 Develop an application Using Widgets
Date :
AIM:
PROCEDURE:
<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/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="42dp"
android:text="Talking ANALOG CLOCK" />
<AnalogClock android:id="@+id/analogClock1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="38dp" />
<TextView android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/analogClock1"
android:layout_centerHorizontal="true"
android:layout_marginTop="60dp"/>
</RelativeLayout>
MainActivity.java
MainActivity.java
package
com.example.Widget;
import android.os.Bundle;
import android.app.Activity;
import java.util.Calendar;
import android.speech.tts.TextToSpeech;
import
android.speech.tts.TextToSpeech.OnInitListener;
import android.view.View;
import
android.widget.AnalogClock;
import android.widget.TextView;
public class MainActivity extends Activity implements OnInitListener
{
TextToSpeech
Talktome; AnalogClock
Clock2; TextView
ReadText;
this);
Clock2 = (AnalogClock)findViewById(R.id.AnalogClock);
Clock2.setOnClickListener(MyAnalogClockOnClickListener);
ReadText = (TextView)findViewById(R.id.Text2Read);
}
public void onInit(int status)
{
// TODO Auto-generated method stub
}
@Override
protected void onDestroy()
{
// TODO Auto-generated method
stub super.onDestroy();
Talktome.shutdown();
}
private AnalogClock.OnClickListener MyAnalogClockOnClickListener
= new AnalogClock.OnClickListener()
{
@Override
public void onClick(View v)
{
final Calendar c =
Calendar.getInstance(); mHour =
c.get(Calendar.HOUR_OF_DAY); mMinute
= c.get(Calendar.MINUTE); String
+ String.valueOf(mHour)
+ " Hour "
+String.valueOf(mMinute)
+ " Minute";
ReadText.setText(myTime);
Talktome.speak(myTime, TextToSpeech.QUEUE_FLUSH, null);
}
};
}
OUTPUT:
RESULT :
Hence the application using Widget for Talking clock has been created,deployed and the
output is verified.
Ex.no : 09 Implement an application that writes data to the SD card
Date:
AIM:
To develop an application that writes data to the SD card.
PROCEDURE:
<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.example.intesd.MainActivity" >
<LinearLayout android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="145dp"
android:orientation="vertical" >
</LinearLayout>
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/linearLayout1"
android:layout_alignParentLeft="true"
android:text="Save Data" />
<Button android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/linearLayout1"
android:layout_alignParentRight="true"
android:layout_marginRight="15dp" android:text="Show Data" />
<TextView android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/linearLayout1"
android:layout_centerHorizontal="true"
android:layout_marginTop="54dp"
android:hint="Display saved Data" />
<EditText android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button"
android:layout_centerHorizontal="true"
android:layout_marginBottom="48dp"
android:ems="10" android:hint="ENTER THE
MESSAGE" >
<requestFocus />
</EditText>
</RelativeLayout>
MainActivity.java
package com.example.intesd;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
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
{
Button b1,b2;
TextView tv;
EditText et;
String data;
private String file="mydata";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button);
b2=(Button)findViewById(R.id.button2);
tv=(TextView)findViewById(R.id.textView2);
et=(EditText)findViewById(R.id.editText1);
b1.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
data=et.getText().toString();
try
{
FileOutputStream fOut=openFileOutput(file,MODE_WORLD_READABLE);
fOut.write(data.getBytes());
fOut.close();
Toast.makeText(getBaseContext(),"file saved",Toast.LENGTH_SHORT).show();
}
catch(Exception e)
{
e.printStackTrace();
}
}
});
b2.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
try
{
FileInputStream fin=openFileInput(file);
int c;
String temp="";
while((c=fin.read())!=-1)
{
temp=temp+Character.toString((char)c);
}
tv.setText(temp);
Toast.makeText(getBaseContext(),"file read",Toast.LENGTH_SHORT).show();
}
catch(Exception e)
{
}
}
});
}}
});
Output:
Result:
Date:
AIM:
To develop an Android Application an alert upon receiving a message
PROCEDURE:
Step 1: Start Android Developer Tools
Step 2: Select File->New->Android Application Project
Step 3: New Android Application dialog box will appear. Enter ApplicationName and Click Next
Step 4: Configure project if needed and Click Next
Step 5: Configure the attributes of the icon set if needed and Click Next
Step 6: Select Blank Activity and Click Next
Step 7: Activity Name and Layout Name will be displayed Click Finish.
Step 8: Android app design area will be displayed. Place necessary controls in the design area.
Step 9: Open MainActivity.java file and write the coding
Step 10: Save and run the application. Exit ADT.
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: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" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="168dp"
android:text=" NOTIFICATION" />
</RelativeLayout>
MainActivity.java
package com.example.notify;
import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
importandroid.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt=(Button)findViewById(R.id.button1);
bt.setOnClickListener(new View.OnClickListener()
{
@Override public void
onClick(View v)
NotificationManager nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification=new
Notification(android.R.drawable.stat_notify_more,"You have got an
email",System.currentTimeMillis()); Context context=MainActivity.this;
Intent intent=new Intent(context,MainActivity.class);
PendingIntent pending=PendingIntent.getActivity(getApplicationContext(),0,intent,0);
notification.setLatestEventInfo(context,"E-Mail from Facebook","You have 4friend
requests",pending); nm.notify(0,notification);
}
});
}
Output :
RESULT:
Hence the Android application that creates an alert upon receiving amessage has been
created, deployed and the output is verified.
Ex No : 11 Develop an application that makes use of Database
Date:
AIM:
To develop an Android Application that makes use of database.
PROCEDURE:
package com.example.database;
import android.support.v7.app.ActionBarActivity;
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;
EditText name,regno,mark;
SQLiteDatabase db;
@Override
setContentView(R.layout.activity_main); regno=
(EditText)findViewById(R.id.editText1);name=
(EditText)findViewById(R.id.editText2);
mark=(EditText)findViewById(R.id.editText3);
btnAdd (Button)findViewById(R.id.button1);
btnView = (Button)findViewById(R.id.button2);
btnViewAll (Button)findViewById(R.id.button3);
btnUpdate= (Button)findViewById(R.id.button4);
btnDelete= (Button)findViewById(R.id.button5);
return;
}
Cursor c=db.rawQuery("SELECT * FROM student WHERE regno=""+regno.getText()+"",
null);
if(c.moveToFirst())
{
db.execSQL("DELETE FROM student WHERE regno=""+regno.getText()+""");
showMessage("Success", "Record Deleted");
}
else
{
showMessage("Error", "Invalid Reg. No.");
clearText();
}
});
@Override
if(regno.getText().toString().trim().length()==0)
return;
}
Cursor c=db.rawQuery("SELECT * FROM student WHERE regno="+regno.getText()+"'", null);
if(c.moveToFirst())
} else
} clear Text();
});
btnView.setOnClickListener(new OnClickListener()
@Override
if(regno.getText().toString().trim().length()==0)
if(c.moveToFirst())
name.setText(c.getString(1));
mark.setText(c.getString(2));
} else
clearText();
});
btnViewAll.setOnClickListener(new OnClickListener()
@Override
return;
while(c.moveToNext())
buffer.append("Reg. No : "+c.getString(0)+"\n");
buffer.append("Name : "+c.getString(1)+"\n");
buffer.append("Mark: "+c.getString(2)+"\n\n");
});
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
regno.setText("");
name.setText("");
mark.setText("");
regno.requestFocus();
}
OUTPUT :
RESULT :
The application for creating and using a database is created successfullyand the result is
verified.