1

I wanna show the notification like messaging app Line, both on main screen and on any application it shows a notification box with the message in it for a seconds and disappear then.

something like this:

example

so what are possible ways to do this ? is it a custom Toast? or custom Dialog?

Community
  • 1
  • 1
MBH
  • 16,271
  • 19
  • 99
  • 149

2 Answers2

5

You have to create a window animation like this :

MyService.java

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

/**
 * Created by darshan.mistry on 7/18/2015.
 */
public class MyService extends Service {

    View mView;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        // instance of WindowManager
        WindowManager mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

        LayoutInflater mInflater = (LayoutInflater)
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        // inflate required layout file
        mView = mInflater.inflate(R.layout.abc, null);

        // attach OnClickListener
        mView.findViewById(R.id.someView).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // you can fire an Intent accordingly - to deal with the click event
                // stop the service - this also removes `mView` from the window
                // because onDestroy() is called - that's where we remove `mView`
                stopSelf();
            }
        });

        // the LayoutParams for `mView`
        // main attraction here is `TYPE_SYSTEM_ERROR`
        // as you noted above, `TYPE_SYSTEM_ALERT` does not work on the lockscreen
        // `TYPE_SYSTEM_OVERLAY` works very well but is focusable - no click events
        // `TYPE_SYSTEM_ERROR` supports all these requirements
        WindowManager.LayoutParams mLayoutParams = new WindowManager.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT, 0, 0,
                WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                        | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                        | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
                        | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
                PixelFormat.RGBA_8888);

        // finally, add the view to window
        mWindowManager.addView(mView, mLayoutParams);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        // remove `mView` from the window
        removeNow();
    }



    // Removes `mView` from the window
    public void removeNow() {
        if (mView != null) {
            WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
            wm.removeView(mView);
        }
    }
}

MyReceiver.java

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

/**
 * Created by darshan.mistry on 7/18/2015.
 */
public class MyReciver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent intent1 = new Intent(context, MyService.class);
        context.startService(intent1);
    }
}

In the manifest file, add the permission:

<!-- [My service and reciver] -->
        <service
            android:name=".MyService"
            android:exported="true" />
        <receiver android:name=".MyReciver">
            <intent-filter>
                <action android:name="com.tutorialspoint.CUSTOM_INTENT"></action>
            </intent-filter>
        </receiver>

MyService class xml file :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="100dp"
    android:background="@android:color/white">

    <TextView
        android:id="@+id/someView"
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:background="@android:color/white"
        android:gravity="center"
        android:text="This is Demo"
        android:textColor="@android:color/black" />

</RelativeLayout>

You have to call MyService from where you have got notification add below lines:

 Intent intent = new Intent();
        intent.setAction("com.tutorialspoint.CUSTOM_INTENT");
        sendBroadcast(intent);

This is a simple example so you have got ideas how to implement notification like line app.

Darshan Mistry
  • 3,294
  • 1
  • 19
  • 29
2

You can create a custom dialog with this permission

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

Follow this answer for more details.

Community
  • 1
  • 1
Emil
  • 2,786
  • 20
  • 24