1

I want to make a layer as always on top so it will appear on every think you can see (all apps and ... ) like the below picture:

enter image description here

it has no error in ide(android studio) but it does not work , it just shows a white screen with no view and in logcat can see below error

E/art: setrlimit(RLIMIT_CORE) failed for pid 18140: Operation not permitted

Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tpck.myapp">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">

<meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />

<activity android:name=".AAA"
    android:exported="true">

    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>

</activity>

<service
    android:name="HUD"
    android:exported="false"
   />
</application>

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

</manifest>

MainActivity

package com.tpck.myapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class  AAA extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent svc = new Intent(this, OverlayShowingService.class);
startService(svc);
    }
}

Service

package com.tpck.myapp;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Toast;

public class HUD extends Service {
HUDView mView;

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

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

Toast.makeText(getBaseContext(),"onCreate",    Toast.LENGTH_LONG).show();
mView = new HUDView(this);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(

        WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
        WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
        PixelFormat.TRANSLUCENT);
        params.gravity = Gravity.RIGHT | Gravity.TOP;
        params.setTitle("Load Average");
 WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(mView, params);
}

@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(getBaseContext(),"onDestroy", Toast.LENGTH_LONG).show();
if(mView != null)
{
    ((WindowManager) getSystemService(WINDOW_SERVICE)).removeView(mView);
    mView = null;
}
    }
}

class HUDView extends ViewGroup {
private Paint mLoadPaint;

public HUDView(Context context) {
super(context);
Toast.makeText(getContext(),"HUDView", Toast.LENGTH_LONG).show();

mLoadPaint = new Paint();
mLoadPaint.setAntiAlias(true);
mLoadPaint.setTextSize(10);
mLoadPaint.setARGB(255, 255, 0, 0);
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawText("Hello World", 5, 15, mLoadPaint);
}

@Override
protected void onLayout(boolean arg0, int arg1, int arg2, int arg3,    int     arg4) {
}

@Override
public boolean onTouchEvent(MotionEvent event) {


Toast.makeText(getContext(),"onTouchEvent", Toast.LENGTH_LONG).show();
return true;
    }
}
sjokkogutten
  • 2,005
  • 2
  • 21
  • 24
roz
  • 27
  • 1
  • 8
  • oh, I see...you want to have your window shown on top of everything else -all the time-. Like facebook's chatheads. I don't know how to do that and will delete my answer. – rothloup Mar 28 '16 at 16:53

1 Answers1

2

Take a look at Floaties. This library does exactly what you want.

Eric B.
  • 4,622
  • 2
  • 18
  • 33
  • tnx , i have used your library but i have a problem i have setted a button to head view and i want it return user to my app back if i want explain more : i have a button in my app that opens an other app by intent and i want get back uset to my app by clicking to head button but when user clickes he backs to a new activity in my app , my MainActivity has been created again and all MainActivity layout elements is reseted ,how can i solve this problem ? – roz Nov 14 '16 at 14:53
  • this not the solution to the problem above mentioned... This may be is an work around... – kAmol Aug 10 '17 at 07:00