Tuesday 6 January 2015

Notification In Android Development


Creating notification from UI .
Requirements :

1. XML Layout with three Edittext and one Button2. Java file for coding.

public class NotificationActivity extends Activity
{
NotificationManager NM;
  EditText one,two,three;
 
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
one = (EditText)findViewById(R.id.editText1);
two = (EditText)findViewById(R.id.editText2);
three = (EditText)findViewById(R.id.editText3);
             }
                @SuppressWarnings("deprecation")
         public void notify(View vobj){
String title = one.getText().toString();
String subject = two.getText().toString();
String body = three.getText().toString();
NM=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Notification notify=new Notification(android.R.drawable.
stat_notify_more,title,System.currentTimeMillis());
PendingIntent pending=PendingIntent.getActivity(
getApplicationContext(),0, new Intent(),0);
notify.setLatestEventInfo(getApplicationContext(),subject,body,pending);
NM.notify(0, notify);
}
}

XML Layout


<LinearLayout 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:orientation="vertical"
    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_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:onClick="notify"
        android:text="@string/notification" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10" />

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

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

</LinearLayout>


No comments:

Post a Comment

How to do text writing animation?  In Android Development we do not have the option to use any other custom font in our default Tex...