You are on page 1of 8

Notification:

A notification is a message you can display to the user outside of your


application's normal UI. When you tell the system to issue a notification, it
first appears as an icon in the notification area. To see the details of the
notification, the user opens the notification drawer. Both the notification area
and the notification drawer are system-controlled areas that the user can
view at any time.
Android Toast class provides a handy way to show users alerts but problem
is that these alerts are not persistent which means alert flashes on the
screen for a few seconds and then disappears.

Create and Send Notifications


You have simple way to create a notification. Follow the following steps in
your application to create a notification

Step 1 - Create Notification Builder


As
a
first
step
is
to
create
a
notification
builder
using NotificationCompat.Builder.build(). You will use Notification Builder to
set various Notification properties like its small and large icons, title, priority
etc.
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
Step 2 - Setting Notification Properties
Once you have Builder object, you can set its Notification properties using
Builder object as per your requirement. But this is mandatory to set at least
following

A small icon, set by setSmallIcon()

A title, set by setContentTitle()

Detail text, set by setContentText()

mBuilder.setSmallIcon(R.drawable.notification_icon);
mBuilder.setContentTitle("Notification Alert, Click Me!");
mBuilder.setContentText("Hi, This is Android Notification Detail!");
Step 3 - Attach Actions
This is an optional part and required if you want to attach an action with the
notification. An action allows users to go directly from the notification to
an Activity in your application, where they can look at one or more events
or do further work.
The action is defined by a PendingIntent containing an Intent that starts
an Activity in your application. To associate the PendingIntent with a
gesture, call the appropriate method of NotificationCompat.Builder. For
example, if you want to start Activity when the user clicks the notification
text in the notification drawer, you add the PendingIntent by
calling setContentIntent().

A PendingIntent object helps you to perform an action on your applications


behalf, often at a later time, without caring of whether or not your
application is running.
PendingIntent getActivity (Context context,
int requestCode,
Intent intent,
int flags)
Parameters
context

Context: The Context in which this PendingIntent should


start the activity.

request
Code

int: Private request code for the sender

intent

Intent: Intent of the activity to be launched.

flags

int:
May
be FLAG_ONE_SHOT, FLAG_NO_CREATE, FLAG_CANCEL_CU
RRENT, FLAG_UPDATE_CURRENT, or any of the flags as
supported byIntent.fillIn() to control which unspecified
parts of the intent that can be supplied when the actual
send happens.

// Prepare intent which is triggered if the


// notification is selected
Intent intent = new Intent(this, NotificationReceiverActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, (int)
System.currentTimeMillis(), intent, 0);
Step 4 - Issue the notification

Finally, you pass the Notification object to the system by calling


NotificationManager.notify() to send your notification. Make sure you
call NotificationCompat.Builder.build() method on builder object before
notifying it. This method combines all of the options that have been set and
return a new Notification object.
NotificationManager mNotificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);

// notificationID allows you to update the notification later on.


/ hide the notification after its selected
noti.flags |= Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(notificationID, mBuilder.build());

Using List View to Display Long Lists


In android, an adapter is a bridge between UI component and data source
that helps us to fill data in the UI component. It holds the data and send the
data to adapter view then view can takes the data from the adapter view and
shows the data on different views like as list view, grid view, spinner etc. For
more customization of views we uses the base adapter. Now lets
discuss BaseAdapter class.

BaseAdapter is a common base class of a general implementation of


an Adapter that can be used in ListView, GridView, Spinner etc.

Whenever you need a customized list in a ListView or customized grids


in a GridView you create your own adapter and extend base adapter in
that.

Base Adapter can be extended to create a custom Adapter for


displaying a custom list item.

Important Note: ArrayAdapter is also an implementation of BaseAdapter.


Here is the code of
the BaseAdapter in that:

Custom

Adapter

public class CustomAdapter extends BaseAdapter {


@Override
public int getCount() {
return 0;
}
@Override
public Object getItem(int i) {
return null;
}

when

we

extends

@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
return null;
}
In
the
above
code
snippet
we
see
the
overrided methods of BaseAdapter which are used to set the data in a list,
grid or a spinner. From there we mainly used two functions getCount() and
getView().
Lets discuss all these functions in detail:
1. getCount():
The getCount() function returns the total number of items to be displayed in
a list. It counts the value from array list size() method or an arrays length.
For example, if we have an list of elements in an arraylist and we have to
display the items in a list view then we can count the total number of
elements using the size function and then that integer value is returned by
the function getCount() as shown below.
@Override
public int getCount() {
int count=arrayList.size(); //counts the total number of elements from the
arrayList
return count;//returns the total count to adapter
}
2. getView(int i, View view, ViewGroup viewGroup):

This function is automatically called when the list item view is ready to be
displayed or about to be displayed. In this function we set the layout for list
items using LayoutInflater class and then add the data to the views
like ImageView, TextView etc.
Below is the getView functions example code with explanation included in
which we set the layout using LayoutInflater and then get the views id and
implement them.
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = inflter.inflate(R.layout.activity_gridview,
displaying items

null);//set

layout

for

ImageView icon = (ImageView) view.findViewById(R.id.icon);//get id for


image view
icon.setImageResource(flags[i]);//set image of the items
return view;
}
3. getItem(int i):
This function is used to Get the data item associated with the specified
position in the data set to obtain the corresponding data of the specific
location in the collection of data items.
Below is the example code in which we returns the array lists item according
to position.
@Override
public Object getItem(int i) {
return arrayList.get(i);
}
4. getItemId(int i):
As for the getItemId (int position), it returns the corresponding to the position
item ID. The function returns a long value of item position to the adapter.

Below is the code in which we returns the position.


@Override
public long getItemId(int i) {
return i;
}

You might also like