Usage of toast and notification

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.