AndroidManifest.xml에 아래 권한을 추가합니다.

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
나의 경우는 어떠한 채팅 유져가 입장했을때 LINE이나 카톡처럼 맨위의 레이어에 캐릭터를 표시하고자 합니다.
	public void userJoined(final Map<String, Object> chatResponse) {
		this.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, PixelFormat.TRANSLUCENT);
				final WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
				LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
				final View view = layoutInflater.inflate(R.layout.overlay_chat_join, null);
				ImageView chat_team_manager = (ImageView)view.findViewById(R.id.overlay_chat_team_manager);
				TextView chat_message = (TextView)view.findViewById(R.id.overlay_chat_message);
				chat_team_manager.setImageBitmap("캐릭터이미지");
				chat_message.setText("메세지");
				wm.addView(view, params);
				// 5초후에 삭제
				Handler h = new Handler();
				h.postDelayed(new Runnable() {
					@Override
					public void run() {
						wm.removeView(view);
					}
				}, 5000);
			}
		});
	}
다음으로 레이아웃은 아래와 같습니다.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:padding="15dp"
        android:background="@drawable/bg_overlay_alert"
        android:gravity="center_horizontal"
        android:orientation="vertical" >
        <ImageView
            android:id="@+id/overlay_chat_team_manager"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/overlay_chat_message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="marquee"
            android:singleLine="false"
            android:textColor="#000"
            android:layout_marginTop="5dp"
            android:textSize="11sp" />
    </LinearLayout>
</RelativeLayout>
마지막으로 배경으로 이용한 drawble 그라데이션입니다.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <corners android:radius="5dp"/>
    <!--stroke android:width="2dp" android:color="#9acd32"/-->
    <stroke android:width="2dp" android:color="#ff901e"/>
    <gradient android:startColor="#ffaa00" android:endColor="#ffffff" android:angle="90"/>
</shape>
참고사이트 http://techbooster.org/android/ui/13182/]]>

Related Posts