Usage of toast
New Project Toast
Resource file Add button btnshowtoast
<button
android:id="@+id/btnshowtoast"
Android:layout_width="Fill_parent"
android:layout_height="Wrap_content"
android:text=" shows a shorter Toast "/>
In the Mainactivity file
Private Button Showtoastshort;
@Override
protected void onCreate (Bundle savedinstancestate) {
Super. OnCreate (savedinstancestate);
Setcontentview (r.layout. Activity_main);
Showtoastshort = (Button) Findviewbyid (r.id. Btnshowtoast);
Showtoastshort.setonclicklistener (new Onclicklistener () {
@Override
Public void OnClick (View v) {
TODO auto-generated Method stub
Toast. Maketext (Mainactivity. This, "show a shorter toast", toast. length_short). Show ();
}
});
}
Create a New button Btnshowtoastlong
<button
android:id="@+id/btnshowtoastlong"
Android:layout_width="Fill_parent"
android:layout_height="Wrap_content"
android:text=" displays a longer Toast "/>
Modify the Mainactivity file
Showtoastlong = (Button) Findviewbyid (r.id. Btnshowtoastlong);
Showtoastlong.setonclicklistener (new Onclicklistener () {
@Override
Public void OnClick (View v) {
TODO auto-generated Method stub
Toast. Maketext (Mainactivity. This, "show a longer toast", toast. Length_long). Show ();
}
});
Specify Toast Location
Toast toast = toast. Maketext (Mainactivity. This, "show a shorter toast", toast. length_short);
Toast.setgravity (Gravity. CENTER, 0, 0);
Toast.show ();
Specify a view for a toast
Adding button buttons
<button
android:id="@+id/btnshowtoastimage"
Android:layout_width="Fill_parent"
android:layout_height="Wrap_content"
android:text=" displays an image with a Toast "/>
Showtoastimage = (Button) Findviewbyid (r.id. Btnshowtoastimage);
Showtoastimage.setonclicklistener (new Onclicklistener () {
@Override
Public void OnClick (View v) {
TODO auto-generated Method stub
Toast toast = toast. Maketext (Mainactivity. This, "show a toast with pictures", toast. Length_long);
ImageView ImageView = new ImageView (mainactivity. this);
Imageview.setimageresource (r.drawable. Ic_launcher);
Toast.setview (ImageView);
Toast.show ();
}
});
The use of notification
Button = (button) Findviewbyid (r.id. button);
Button.setonclicklistener (new Onclicklistener () {
@Override
Public void OnClick (View v) {
TODO auto-generated Method stub
counter++;
Builder builder = new notificationcompat.builder (mainactivity. this);
Builder.setsmallicon (r.drawable. Ic_launcher);
Builder.setcontenttitle ("Wow, you have a" +counter+ "new Message! ");
Builder. Setcontenttext ("You can already create a new notification");
Notification Notification = Builder.build ();
Notificationmanager manager = (Notificationmanager) getsystemservice (Context. Notification_service);
Manager.notify (notification_id, NOTIFICATION);
}
});
Usage of toast and notification