Android user interface-status bar notification (3)

Source: Internet
Author: User

Create a custom notification Layout

By default, the notification displayed in the notification window includes the title and message text. The two items are defined using the contenttitle and contenttext parameters of the setlatesteventinfo () method. However, you can also use the remoteviews class to define a custom Layout for notifications. 3 is an example of custom notification layout. It looks similar to the default notification, but it is actually created using a custom XML layout.

Figure 3. Notifications with custom la S.

To create a layout for a notification, You need to instantiate a remoteviews object, use it to fill in a custom Layout file, and then pass the remoteviews object to the contentview attribute field of the notification.

The following uses an example to better understand how to create custom notifications:

1. Create an XML layout for notifications, as defined in the custom_icationication.xml file below:

<Relativelayoutxmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Id = "@ + ID/layout"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: padding = "10dp">
<Imageviewandroid: Id = "@ + ID/image"
Android: layout_width = "wrap_content"
Android: layout_height = "fill_parent"
Android: layout_alignparentleft = "true"
Android: layout_marginright = "10dp"/>
<Textviewandroid: Id = "@ + ID/Title"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_torightof = "@ ID/image"
Style = "@ style/notificationtitle"/>
<Textviewandroid: Id = "@ + ID/text"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_torightof = "@ ID/image"
Android: layout_below = "@ ID/Title"
Style = "@ style/icationicationtext"/>
</Relativelayout>

We noticed that both textview elements contain the style attribute, and it is essential to use style resources for texts in custom notifications, because the background color of the notification varies between different devices and platform versions. From android2.3 (API level 9), the system defines the style for the text used by the default notification layout, So that you should use the style when running android2.3 or a later version, to make sure that the text is visible to the background.

For example, to use a standard text color for versions earlier than android2.3, use the following style (RES/values/styles. XML ):

<? XML version = "1.0" encoding = "UTF-8"?>
<Resources>
<Stylename = "icationicationtext">
<Itemname = "Android: textcolor">? Android: ATTR/textcolorprimary </item>
</Style>
<Stylename = "notificationtitle">
<Itemname = "Android: textcolor">? Android: ATTR/textcolorprimary </item>
<Itemname = "Android: textstyle"> bold </item>
</Style>
<! -- If you want a slightly different color for some text,
Consider using? Android: ATTR/textcolorsecondary -->
</Resources>

Use the following style (RES/values-v9/styles. XML) to give default colors to the notification application system on Versions later than android2.3 ):

<? XML version = "1.0" encoding = "UTF-8"?>
<Resources>
<Stylename = "icationicationtext" parent = "Android: textappearance. statusbar. eventcontent"/>
<Stylename = "notificationtitle" parent = "Android: textappearance. statusbar. eventcontent. Title"/>
</Resources>

Now, when running on android2.3 (API level 9) or a later version, the text in the Custom view object uses the same color as the default notification that the system gives. This is important because later Android versions actually change the background color of the notification to a dark color. Inherit the system style to ensure that the text is highlighted. if the background is another unexpected color, change the text as needed.

2. Now, in the application code, use the removeviews class method to define the image and text, and then pass the remoteview object to the contentview attribute field of the notification, as shown in the following example:

Remoteviews contentview
= Newremoteviews (getpackagename (),
R. layout. custom_icationication_layout );
Contentview. setimageviewresource (R. Id. image,
R. drawable. icationication_image );
Contentview. settextviewtext (R. Id. title, "Custom
Notification ");
Contentview. settextviewtext (R. Id. Text, "This
Is a custom layout ");
Notification. contentview
= Contentview;

As shown in the preceding example, the package name and layout resource ID of the application are passed to the remoteviews constructor. Then, the setimageviewresource () and settextviewtext () methods are used, define the content for the imageview and textview objects. In each statement, you must pass the reference ID of the appropriate view object together with the value set to the view object as a parameter to these two methods. Finally, the remoteviews object is passed to the contentview attribute field of the notification object.

3. Because the setlatesteventinfo () method is not required when you use a custom view object, you must use the contentintent field to define the intent object for the notification, as shown in the following example:

Intent icationicationintent = new intent (this, myclass. Class );

Pendingintent contentintent = pendingintent. getactivity (this, 0, icationicationintent, 0 );

Notification. contentintent = contentintent;

4.
Send a notification using the following methods:

Micationicationmanager. Y (custom_view_id, notification );

The remote class also contains methods that are easy to add timers or progress bars to the notification layout. For more information about creating custom la s for notifications, refer to the remoteviews class.

Warning:When creating a custom notification layout, you must be especially careful to ensure that the customized layout works properly on devices in different directions and resolutions. Although this suggestion applies to all view object la s created in Android, it is especially important in this scenario because the actual layout space is very limited, so do not make the custom layout too complicated, in addition, tests should be performed in various configurations.

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.