Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating a system overlay window (always on top)
    text
    copied!<p>I'm trying to create an always-op-top button/clickable-image which stays on top of all the windows all the time. </p> <p>The proof of concept is </p> <ul> <li>here - <a href="http://www.appbrain.com/app/smart-taskbar-%28sidebar%29/com.smart.taskbar" rel="noreferrer">Smart Taskbar (on AppBrain)</a></li> <li>and here <a href="http://forum.xda-developers.com/showthread.php?t=865525" rel="noreferrer">V1.4.0 Sidebar style SWKey - Button savior (on xda-developers)</a> </li> </ul> <p>I have been successful and have a running service now. The service displays some text on top left corner of screen all the time while user can freely interact with rest of apps in normal manner. </p> <p>What I'm doing is subclass <code>ViewGroup</code> and add it to root window manager with flag <code>TYPE_SYSTEM_OVERLAY</code>. Now I want to add a button/clickable-image in place of this text which can receive touch events on itself. I tried overriding "onTouchEvent" for the whole <code>ViewGroup</code> but it does not receive any event. </p> <p>How can I receive events only on certain parts of my always-on-top view group? Kindly suggest.</p> <pre><code>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.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, 0, // WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE // | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, 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) { //return super.onTouchEvent(event); Toast.makeText(getContext(),"onTouchEvent", Toast.LENGTH_LONG).show(); return true; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload