Sunday, 23 June 2013

Notification using android

XML File

<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/TextView01"
         android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Notification Demo"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dip"
android:textStyle="bold">
     
    </TextView>
<Button
    android:layout_below="@id/TextView01"
android:layout_width="wrap_content"
android:text="Start"
android:id="@+id/start"
android:layout_centerHorizontal="true"
android:layout_height="50dip"
android:layout_marginTop="20dip"></Button>
<Button
    android:layout_width="wrap_content"
    android:text="Clear"
android:layout_below="@+id/start"
android:id="@+id/clear"
android:layout_centerHorizontal="true"
android:layout_height="50dip"
android:layout_marginTop="20dip"></Button>

</RelativeLayout>


SecondXML File

<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=".Second" >

   <TextView android:id="@+id/TextView01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="You just clicked on a notification"></TextView>

</RelativeLayout>


Main JAVA File

package com.example.notif;

import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
Button start,clear;
Notification notification;
private static final int NOTIFICATION_ID=1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

start=(Button)findViewById(R.id.start);
clear=(Button)findViewById(R.id.clear);

final NotificationManager mgr=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notification=new Notification(R.drawable.ic_launcher,"Mongoss Mandaya!", System.currentTimeMillis());
//Intent to start new activity on click of expanded view
Intent intent=new Intent(getApplicationContext(), Second.class);
PendingIntent pendingIntent=PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
notification.setLatestEventInfo(this, "Reminder:  Birthday",
"Today is your friend Saanvi's Birthday, please wish her", pendingIntent);

start.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
mgr.notify(NOTIFICATION_ID,notification);
}
});
clear.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//to clear the notification
mgr.cancel(NOTIFICATION_ID);
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}



Second.java

package com.example.notif;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class Second extends Activity {

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, menu);
return true;
}

}



2 comments:

  1. How can i add this code to my android? notification

    ReplyDelete
    Replies
    1. Get .apk file from your project and install it on your device.

      Delete