Welcome to Objectechenica
Android Status Bar Notifications


A status bar notification allows user to notify an event without interrupting their current activity. Also, we can attach Intent to the notification, and the system will initiate the intent when the user clicks on the notification.  

A status bar notification adds an icon to the system's status bar with an optional ticker-text message and a notification message in the notifications window. When the user selects the notification, Android fires an Intent that is defined by the Notification usually to launch an Activity. You can also configure the notification to alert the user with a sound, a vibration, and flashing lights on the device. 

Here, in this tutorial I will demonstrate creating the status bar notification from the application and the basics responding to notification. For creating user notifications we will require to use Notification and NotificationManager class.

Here are the steps to create a status bar notification:
1.      Get a reference to the NotificationManager

NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

2.      Instantiate the Notification: It takes the notification icon, notification text and the delay time in milliseconds to display the notification.

Notification notification = new Notification(R.drawable.block_user_icon, "Block user", System.currentTimeMillis());

3.      Define the notification's message and PendingIntent. The PendingIntent will launch activity if the user selects this notification. Here you can have your activity class reference. I am using

PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(getApplicationContext(), LoginScreen.class), 0);
notification.setLatestEventInfo(getApplicationContext(), "Title here", ".. And here's some more details..", contentIntent);
4.      Pass the Notification to the NotificationManager:
manager.notify(YOURAPP_NOTIFICATION_ID, notification);

That's it. Your user has now been notified.

5.      To Cancel/Remote the notification from status bar
manager.cancel(YOURAPP_NOTIFICATION_ID); 

Here is the screenshot of my application, where I have an activity with 2 sets of buttons, “Show” and “Cancel”, dynamically placed over LinearLayout. And the output will looks something like the image placed below. Here image (A) displays the notification in Status bar, and image (B) displays the details of the notification, while user expands the notification.




About the Author

I’m a mobile application and game developer located in Bangalore area, India. I’ve been working in mobility since 2007. Currently working in various mobile platforms like BlackBerry, Android, J2ME, Sencha Touch, Phone Gap. Over the last few years I’ve become what I call an ‘optimistic environmentalist’. I try to write about breakthroughs in low power computing. Another issue I’m passionate about gaming.
Follow Me on Facebook : NILANCHALA PANIGRAHY

Custom Screen Title in Android

The default title of an android screen looks pretty simple, with just a simple text. However, we can always customize the title as per our need. Here in this example I am demoing the sample application were the title is customized with a logo, screen name and an icon. It will look like as much shown in the below image.



To achieve this here are the simple steps we can follow.
Step 1: Create and android project

Step 2: we need to have a layout for specifying the layout and alignments of the title elements. Here I am defining a LinearLayout where the logo is placed and another nested LinearLayout for screen title and icon. Checkout the below code

window_title.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="40dip"
    android:gravity="center_vertical"
    android:paddingLeft="5dip"
    android:background="#FFFFFF">

    <ImageView 
        android:id="@+id/header" 
        android:src="@drawable/objectechenica_logo"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" />
    
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="right|center_vertical"
        android:paddingRight="5dip">
    
        <TextView 
            android:id="@+id/title"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:textSize="11dip"
            android:textColor="#0000FF"
            android:paddingRight="5dip" />
                    
        <ImageView 
            android:id="@+id/icon" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"/>
                
    </LinearLayout>

</LinearLayout>

Step 3: Declare the theme resource in values folder.

custom_title.xml
<resources>
    <style name="CustomWindowTitleBackground">
        <item name="android:background">#323331</item>
    </style>
    
    <style name="CustomTheme" parent="android:Theme">
        <item name="android:windowTitleSize">35dip</item>
        <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
    </style>
</resources>



Step 4: Once we are ready with the theme/resource. We can use the theme in the AndroidMainfest.xml using the android:theme attribute. Here is my complete “AndroidMainfest.xml” code.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.objectechenica.droid.ui"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/CustomTheme"  >
        <activity
            android:name=".CustomWindow"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Step 5: My example using the below images, please do download and place it in drawable asserts folder. You can also have your own images placed in “drawable” folder.

       



Step 6: Now, here it comes to your activity class. Activity class should override onCreate() method. Here you can load the layout or the xml file using setContentView(R.layout.main). But in this example as we don’t have any actual screens, I have taken the layouts declared dynamically and added to the activity using addContentView().
We can also use the full screen mode, by hiding the notification bar using

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  WindowManager.LayoutParams.FLAG_FULLSCREEN);
Find the complete code here:

CustomWindow.java

package com.objectechenica.droid.ui;
import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ImageView;

public class CustomWindow extends Activity {
    protected TextView title;
    protected ImageView icon;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        
        LinearLayout ll = new LinearLayout(this);
        addContentView(ll, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
                
        // to hide notification bar and making full screen
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  WindowManager.LayoutParams.FLAG_FULLSCREEN);
        
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
        
        title = (TextView) findViewById(R.id.title);
        icon  = (ImageView) findViewById(R.id.icon);
        
        title.setText("Home");
        icon.setImageResource(R.drawable.info);
    }
}

Here you go. Just compile and run. You will get to see a screen with the title as shown in the image above.
Thank you.

2.1. Why Reference Counting Does Not Work?

Reference counting does not always work. It will fail to work whenever the data structure contains a cycle of references. Python does not prevent the creation of cyclic structures. Consider a circular, singly-linked list, the variable head refers to the head of the linked list and the last element of the linked list also refers to the head. So, the reference count on the first list element is two, whereas, the remaining list elements each has a reference count of one.



If it happened to assign the value none to the head variable, then the reference count on the first list element has been decreased by one because the head variable no longer refers to it. But its reference count is not zero, because the tail of the list still refers to the head.

We now have a problem. The reference counts on all the lists elements are non-zero. Therefore, they are not considered to be garbage by a reference counting garbage collector. On the other hand, no external references to the linked-list elements remain. Therefore, the list elements are indeed garbage.

Reference counting will fail to work whenever the data structure contains a cycle of references, so reference counting by itself is not a suitable garbage collection scheme. However, it is a useful technique for dealing with simple objects that don't refer to other objects, such as int and floats.

Refer 1.0  An Overview of Garbage collector
Refer 2.0: Reference Counting Garbage Collection
Refer 2.1: Why Reference Counting Does Not Work?

About the Author
I’m a mobile application and game developer located in Bangalore area, India. I’ve been working in mobility since 2007. Currently working in various mobile platforms like BlackBerry, Android, J2ME, Sencha Touch, Phone Gap. Over the last few years I’ve become what I call an ‘optimistic environmentalist’. I try to write about breakthroughs in low power computing. Another issue I’m passionate about gaming.
Follow Me on Facebook : NILANCHALA PANIGRAHY