0

I have created an app that applies a coloured filter over the screen. I would like to be able to change the colour of the filter by pressing buttons. Changing the background colour in the xml file works, but when I try changing it in a service it does not change colour. I am getting no errors but the colour does not change. I think that because I am adding the view with windowmanager and not setting it with setContentView, it might not apply the colour changes. Here is the code of my service, I know the service is being called because the Log.d(TAG,"onCreated"); is showing up in the logcat. By the way, I have tried changing the colour of root instead of bView, but I end up with the same result.

public class ChangeColor extends Service {
    public static final String TAG = "ChangeColour";
    View mOverlayView;

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

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

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mOverlayView = inflater.inflate(R.layout.overlay_view, null);
        LinearLayout bView = (LinearLayout) mOverlayView.findViewById(R.id.lin);
        if (bView != null) {
            View root = bView.getRootView();
            bView.setBackgroundColor(Color.parseColor("#000000"));
            Log.d(TAG, "onCreated");
        }
        return START_NOT_STICKY;
    }
}

See my old post if you would like to see the service and activity that is adding the view. If anybody has any ideas let me know! Thanks.

sandra burgle
  • 47
  • 1
  • 8
  • Try this. https://stackoverflow.com/questions/11443820/floating-widget-overlay-on-android-launcher. This concept might help. – Sarthak Gandhi Jun 27 '17 at 18:36
  • @sarthakGandhi thanks for the comment, I have already created the overlay but I want to know if there is a way to change the colour of the overlay without editing the xml file. Thanks anyways. – sandra burgle Jun 27 '17 at 18:40
  • I don´t think that manipulating any activity in this way is a good idea. Another possible way could be to use a Broadcast that you send from your service to the activity where the view is inside.. – Opiatefuchs Jun 27 '17 at 19:17

1 Answers1

0

Try the following. I didn't see where you added the view to the window, although I know you had it there before. I also changed the color and some of the values so I could notice the change. Make sure the service is defined in your manifest.

public class ChangeColor extends Service {
    public static final String TAG = "ChangeColour";
    View mOverlayView;

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

    @Override
    public void onCreate() {
        super.onCreate();
        final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
                        WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
                        WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
                PixelFormat.TRANSLUCENT);
        params.alpha = 0.5F;
        params.dimAmount = 0.9F;
        WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mOverlayView = inflater.inflate(R.layout.overlay_view, null);
        wm.addView(mOverlayView, params);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (mOverlayView != null) {
            mOverlayView.setBackgroundColor(Color.parseColor("#FF0000"));
        }
        return START_NOT_STICKY;
    }
}
Cheticamp
  • 61,413
  • 10
  • 78
  • 131
  • Thanks this works! Would it be possible to change the colour of the view after it is added? Or would you have to remove view and then re-add it with different colour? – sandra burgle Jun 27 '17 at 20:21
  • @sandraburgle Yes, use the intent to pass information to the service's `onStartCommand()` method that informs it what color you want. `setBackgroundColor` can be called multiple times. – Cheticamp Jun 27 '17 at 20:35
  • Awesome, works great. I am using [This post you linked earlier](https://stackoverflow.com/questions/3293243/pass-data-from-activity-to-service-using-an-intent/15899750#15899750) to pass down what colour I need. Can't believe it finally works! Thanks for spending the time and helping me, it sure took a while. – sandra burgle Jun 27 '17 at 21:13
  • Hey, I ran into a problem while trying to change the alpha properties of the same view, the colours change fine but for some reason the alpha doesn't. If you wouldn't mind helping me again heres the link [link](https://stackoverflow.com/questions/44814042/setalpha-behaving-weird) – sandra burgle Jun 28 '17 at 23:27