I need to have a floating view over all of my application.
I can use Window_service but I don't need my view to be outside of the app.
Only inside my app.
I also don't want the user to see "draw on other app" permission.
How else can I do it?
I need to have a floating view over all of my application.
I can use Window_service but I don't need my view to be outside of the app.
Only inside my app.
I also don't want the user to see "draw on other app" permission.
How else can I do it?
I've had to do something similar to this. In my case, I wanted all of my logging statements to be shown above the whole application (Similar to the Developer Options -> Show CPU Usage
settings in an Android device).
In my case, I just wanted to display a TextView
on top of the app, but you can pretty much replace it with your own layout.
Here's an example of the code I used:
import android.app.Service;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.os.Binder;
import android.os.IBinder;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.BackgroundColorSpan;
import android.view.WindowManager;
import android.widget.TextView;
public class OverlayService extends Service {
private final IBinder mBinder = new LocalBinder();
private TextView mTextView;
private Spannable mSpannable;
public class LocalBinder extends Binder {
public OverlayService getService() {
return OverlayService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
@Override
public void onCreate() {
super.onCreate();
mTextView = new TextView(this);
mTextView.setTextSize(10);
mTextView.setTextColor(Color.WHITE);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, 0 | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(mTextView, params);
}
public void setText(String text) {
mSpannable = new SpannableString(text);
mSpannable.setSpan(new BackgroundColorSpan(0xD993d2b9),0, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// simple logic to only display 10 lines
if(mTextView.getLineCount() > 10) {
mTextView.setText(mSpannable);
} else {
mTextView.append(mSpannable);
}
mTextView.append("\n");
}
@Override
public void onDestroy() {
super.onDestroy();
if (mTextView != null) {
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.removeView(mTextView);
}
}
}
Then you can just connect with this Service from your Activity and set it's text, like so:
ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className, IBinder service) {
LocalBinder binder = (LocalBinder) service;
mService = binder.getService();
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
}
};
Intent intent = new Intent(context, OverlayService.class);
context.bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
mService.setText("Sending a test message");
You would of course need to destroy that service as soon as the user leaves the app if you don't want it to be on top of other apps.