Home » Android » How to create pop-up notifications like the notification used in whatsapp for android?

How to create pop-up notifications like the notification used in whatsapp for android?

Posted by: admin May 14, 2020 Leave a comment

Questions:

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 🙂

How to&Answers:

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());
}

Answer:

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>

Answer:

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>

Answer:

I don’t know what whatsapp’s pop-up looks like…

You can use a toast or a dialog