Showing posts with label Custom ContextMenu using PopupWindow Sample. Show all posts
Showing posts with label Custom ContextMenu using PopupWindow Sample. Show all posts

Monday, October 3, 2011

Android: Displaying a status bar notification message

First, I created an Android project called “AndroidNotifications”.


The code is fairly simple so I’ll dump it here in it’s complete form:
package com.giantflyingsaucer;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class AndroidNotifications extends Activity
{
    private final int NOTIFICATION_ID = 1010;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button btn = (Button)findViewById(R.id.createNotificationButton);
        btn.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Timer timer = new Timer();
                TimerTask timerTask = new TimerTask()
                {
                    @Override
                    public void run()
                    {
                        triggerNotification();
                    }
                };
                timer.schedule(timerTask, 3000);
            }
        });
    }
    private void triggerNotification()
    {
        CharSequence title = "Hello";
        CharSequence message = "Hello, Android!";
        NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        Notification notification = new Notification(R.drawable.icon, "A New Message!", System.currentTimeMillis());
        Intent notificationIntent = new Intent(this, AndroidNotifications.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        notification.setLatestEventInfo(AndroidNotifications.this, title, message, pendingIntent);
        notificationManager.notify(NOTIFICATION_ID, notification);
    }
}
The NOTIFICATION_ID allows you to grab the notification and remove it via your code if needed. Using a TimerTask I made it so that the notification shows up after 3 seconds from when the button is clicked. The real magic happens in the “triggerNotification()” method. The code is not complex and is pretty much the most minimal example I could come up with and keeping it pretty clear.
Here is the main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <Button
            android:id="@+id/createNotificationButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Create Notification"
            android:padding="10dp" />
</LinearLayout>
Keep in mind you can also make the LED lights, play a sound or use the vibration feature to also notify the user. Just make sure not to let your Android app get “annoying”. Better yet in your application let the user choose how they want to be notified via custom settings or default settings, etc.
Here is the final result:


Wednesday, September 28, 2011

Custom ContextMenu using PopupWindow Sample

Here we are creating a  custom contextmenu using PopupWindow in android. A simple PopUpWindow can be created using the following steps.
   1)A layout XML  which describes the View that will be rendered within a PopUpWindow  has to be created.
   2)Invoke the PopUpWindow by inflating the layout XML,  and assign appropriate  “parent view”  to  the pop-up.

Popup_example.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:padding="10dip" android:layout_width="fill_parent"
android:layout_height="wrap_content" >
    <TextView android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:layout_marginTop="10dip" android:text="Test Pop-Up"/>
</LinearLayout>

Java Code:
    LayoutInflater inflater = (LayoutInflater)
    this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    PopupWindow pw = new PopupWindow(
    inflater.inflate(R.layout.popup_example, null, false),  100,  100,    true);
    // The code below assumes that the root container has an id called 'main'
    pw.showAtLocation(this.findViewById(R.id.main), Gravity.CENTER, 0, 0); 

How to Dismiss the PopUp Window?

Declare the necessary variables:

         private PopupWindow Popup;
       //how much time your popup window should appear
        private static final int POPUP_DISMISS_DELAY = 4000
       private DismissPopup mDismissPopup = new DismissPopup();


Class for dismissing the PopUp Window
Create a class which helps in dismissing the window automatically after the specified time:
class DismissPopup implements Runnable {
  public void run() {
      // Protect against null-pointer exceptions
      if (mPopup != null) {
          mPopup.dismiss();
      }
  }
}
Code Snippet:
Popup = new PopupWindow(this.getViewInflate()
                  .inflate(R.layout.main1,null,null),0,0);  
Popup.setOutsideTouchable(false);
Popup.setTouchInterceptor(new OnTouchListener() {

public boolean onTouch(View v, MotionEvent event) {
return false;
}});


Popup.showAtLocation(this.findViewById(R.id.main2),Gravity.BOTTOM, 20, 20);

youractivity.postDelayed(mDismissPopup, POPUP_DISMISS_DELAY)
;