11

I want to create notification like the pop-up notification used in WhatsApp application for android devices.

How can i do it? Since i am a new user I cannot upload the screen shot

please refer this link : http://cdn6.staztic.com/cdn/screenshot/appsdroidnotifythemeicecreamsandwich-1-1.jpg

enter image description here

for the screen shot image and help me :)

PRATEEK BHARDWAJ
  • 2,364
  • 2
  • 21
  • 35
Hari Nadar
  • 111
  • 1
  • 1
  • 5
  • 4
    the qtn is a little broad pls add the screen shot of that notification as well as whatever code you have tried to accomplish it – Athul Harikumar Sep 03 '12 at 09:57
  • I have edited the question check the link for screen shot – Hari Nadar Sep 03 '12 at 10:06
  • 2
    You can use a normal activity for this, and style its theme to Theme.Dialog – Carnal Sep 03 '12 at 10:10
  • 1
    you can accomplish this by either inflating a layout to a dialog box or crating an activity that looks like a dialog for both you can refer android api demos (in version 2.2) or reffer http://stackoverflow.com/questions/12070005/dialog-with-own-layout-in-android/12070177#12070177 – Athul Harikumar Sep 03 '12 at 10:10
  • 1
    but if you look at it then its pop-up should come when the application is running in background It is like any message sent from server side – Hari Nadar Sep 03 '12 at 11:27
  • Hari Nadar wants something like this http://stackoverflow.com/questions/21140721/how-does-whatsapp-pop-up-notification-work I want it too :( – Fabio Crivelaro Oct 04 '14 at 04:35
  • below answer will help you out https://stackoverflow.com/a/66490114/13762440 – anusha anu Mar 05 '21 at 10:21

4 Answers4

6

They're called 'heads-up' notifications. This page has a nice explanation.

To summarize, set the priority to High (or Max).

Here is an example in my code:

public static void notify(Context context, int id, int titleResId,
                          int textResId, PendingIntent intent) {
    NotificationManager notificationManager =
        (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    String title = context.getString(titleResId);
    String text = context.getString(textResId);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.notification)
            .setContentTitle(title)
            .setContentText(text)
            .setDefaults(Notification.DEFAULT_ALL)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setAutoCancel(true)
            .setWhen(System.currentTimeMillis())
            .setTicker(title)
            .setContentIntent(intent);
    notificationManager.notify(id, builder.build());
}
user1857492
  • 697
  • 1
  • 7
  • 22
Timores
  • 14,439
  • 3
  • 46
  • 46
0

Declare a PopUpWindow reference and call initiatePopupWindow() method from where you want to open a pop up window:

private PopupWindow pwindo;

private void initiatePopupWindow() {
try {
// We need to get the instance of the LayoutInflater
LayoutInflater inflater = (LayoutInflater) PopUpWinndowDemoActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.screen_popup,
(ViewGroup) findViewById(R.id.popup_element));
pwindo = new PopupWindow(layout, 300, 370, true);
pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);

} catch (Exception e) {
e.printStackTrace();
}
}

Have an screen_popup.xml for the pop up window like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/popup_element"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#444444"
android:orientation="vertical"
android:padding="10sp" >

<TextView
android:id="@+id/txtView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5sp"
android:text="Hello!" />

</LinearLayout>
Kaveesh Kanwal
  • 1,753
  • 17
  • 16
  • when i change activity that time pop up window manager not displaying but if i want to still it visible to any activity i start or back pressed than any way you have ? – Vishal Patoliya ツ May 11 '17 at 10:39
0

You can achieve it using Dialog theme or by making the activity's window transparent through setting window background to transparent in theme. Just make your UI is in such a way that it looks like a Dialog.

<style name="Theme.AppCompat.Translucent" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:colorBackgroundCacheHint">@null</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowAnimationStyle">@android:style/Animation</item>
</style>
Shailendra Yadav
  • 1,822
  • 1
  • 12
  • 16
-7

I don't know what whatsapp's pop-up looks like...

You can use a toast or a dialog

gh.
  • 339
  • 1
  • 3
  • 20
  • I have given the link of image in my question see it – Hari Nadar Sep 04 '12 at 09:26
  • Is there something dialog will not handle that you wanted to do? – gh. Sep 04 '12 at 09:41
  • it would be a custom dialog but I want to send notification even if the application is not running I want to create a service or something like that for showing these notification – Hari Nadar Sep 04 '12 at 10:39
  • You can't dialog a user from a service, because you shouldn't. If an app ever threw up a dialog while I was involved in a different app, I would remove the app from my device. See the [notifications](http://developer.android.com/reference/android/app/Notification.html) – gh. Sep 04 '12 at 20:16
  • I agre, like new sms or alarm, very annoying – miky Nov 14 '13 at 13:48
  • 3
    `I don't know what whatsapp's pop-up looks like...` Then you shouldn't answer the question. – Roman Holzner Feb 28 '14 at 16:05