How to make this type of notification

Asked

Viewed 500 times

0

How can I make this kind of notification?

inserir a descrição da imagem aqui

Even when you click clean it doesn’t leave the notification bar, it also has 2 buttons there. I’d like to do exactly like this, I’ve looked hard enough and can’t find anywhere.

  • Although it is a question without many details, maybe it is something really complicated to know where to start, in my view the question should not be closed, just maybe a little improved.

  • What I need to do is a notification bar that when I click the back button of the device the application does not close and appears this same notification above. If you click Clear notifications it never clears my notification and my app is still running. When activating the On and OFF button the app turns off the functionality that was example the vibrate functionality. We also have the close button which is that X. That’s what I need to do.

  • Yes the question is clear, @claudsan’s answer should help. I didn’t comment to you, my previous comment was that you voted to close the issue.

1 answer

0


You can create your own layout as an example below:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp" >
    <ImageView android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="10dp" />
    <TextView android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/image"
        style="Custom Notification Title" />
    <TextView android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/image"
        android:layout_below="@id/title"
        style="Custom Notification Text" />
</RelativeLayout>

In Activity:

@SuppressWarnings("deprecation")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        int icon = R.drawable.ic_launcher;
        long when = System.currentTimeMillis();
        Notification notification = new Notification(icon, "Custom Notification", when);

        NotificationManager mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

        RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification);
        contentView.setImageViewResource(R.id.image, R.drawable.ic_launcher);
        contentView.setTextViewText(R.id.title, "Custom notification");
        contentView.setTextViewText(R.id.text, "This is a custom layout");
        notification.contentView = contentView;

        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        notification.contentIntent = contentIntent;

        notification.flags |= Notification.FLAG_NO_CLEAR; //Do not clear the notification
        notification.defaults |= Notification.DEFAULT_LIGHTS; // LED
        notification.defaults |= Notification.DEFAULT_VIBRATE; //Vibration
        notification.defaults |= Notification.DEFAULT_SOUND; // Sound

        mNotificationManager.notify(1, notification);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

There are other ways to do this but I believe that with your own layout it is easier.

Here too is a example(English) without the use of the layout.

Post original in the OS (English)

I hope it helped.

  • I was able to do it by the example you sent me. See here: http://imgur.com/DY4AZUW Now I need to program the buttons. Do you know how I can do it? For example I’m testing the vibra call of the phone, I would like when you click the blue button it stops with this functionality. The close button can use System.Exit(0);

  • I’ll see if I find here as I did, I used a button if I’m not mistaken I think it uses equal a button any. I’ll see if I can find it here and put it here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.