-1

I have an Overlay Button in my Android app. I want to show a layout and interact with the views of my Layout when the user click on the button. For the moment, I show a Toast. How can I do that ?

This is my OverlayShowingService.class :

public class OverlayShowingService extends Service implements OnTouchListener, OnClickListener {

    private View topLeftView;

    private Button overlayedButton;
    private float offsetX;
    private float offsetY;
    private int originalXPos;
    private int originalYPos;
    private boolean moving;
    private WindowManager wm;

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

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

    wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);

    overlayedButton = new Button(this);
    overlayedButton.setText("Overlay button");
    overlayedButton.setOnTouchListener(this);
    overlayedButton.setBackgroundColor(Color.BLACK);
    overlayedButton.setOnClickListener(this);

    WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, PixelFormat.TRANSLUCENT);
    params.gravity = Gravity.LEFT | Gravity.TOP;
    params.x = 0;
    params.y = 0;
    wm.addView(overlayedButton, params);

    topLeftView = new View(this);
    WindowManager.LayoutParams topLeftParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, PixelFormat.TRANSLUCENT);
    topLeftParams.gravity = Gravity.LEFT | Gravity.TOP;
    topLeftParams.x = 0;
    topLeftParams.y = 0;
    topLeftParams.width = 0;
    topLeftParams.height = 0;
    wm.addView(topLeftView, topLeftParams);

    }

    @Override
    public void onDestroy() {
    super.onDestroy();
    if (overlayedButton != null) {
        wm.removeView(overlayedButton);
        wm.removeView(topLeftView);
        overlayedButton = null;
        topLeftView = null;
    }
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {

    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        float x = event.getRawX();
        float y = event.getRawY();

        moving = false;

        int[] location = new int[2];
        overlayedButton.getLocationOnScreen(location);

        originalXPos = location[0];
        originalYPos = location[1];

        offsetX = originalXPos - x;
        offsetY = originalYPos - y;

    } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
        int[] topLeftLocationOnScreen = new int[2];
        topLeftView.getLocationOnScreen(topLeftLocationOnScreen);

        System.out.println("topLeftY="+topLeftLocationOnScreen[1]);
        System.out.println("originalY="+originalYPos);

        float x = event.getRawX();
        float y = event.getRawY();

        WindowManager.LayoutParams params = (LayoutParams) overlayedButton.getLayoutParams();

        int newX = (int) (offsetX + x);
        int newY = (int) (offsetY + y);

        if (Math.abs(newX - originalXPos) < 1 && Math.abs(newY - originalYPos) < 1 && !moving) {
        return false;
        }

        params.x = newX - (topLeftLocationOnScreen[0]);
        params.y = newY - (topLeftLocationOnScreen[1]);

        wm.updateViewLayout(overlayedButton, params);
        moving = true;
    } else if (event.getAction() == MotionEvent.ACTION_UP) {
        if (moving) {
        return true;
        }
    }

    return false;
    }

    @Override
    public void onClick(View v) {
    Toast.makeText(this, "Here I want to show a layout with some options in a box", Toast.LENGTH_SHORT).show();
    }

}
Jéwôm'
  • 3,753
  • 5
  • 40
  • 73

2 Answers2

1

Send a broadcast when then the button is clicked and start activity from Broadcast receivers onReceive method.

Create a broadcast receiver,Register a Broadcast reciever onCreate() of your service then. Broadcast intent when the button is clicked. Finally the receiver will open a Activity(layout) when the overlay button is clicked.

If you only wanted to open a dialog this is a decent answer. https://stackoverflow.com/a/31221355/4179914

Turns out you can also theme a activity like a dialog as mentioned here https://stackoverflow.com/a/7918720/4179914

public class OverlayClickReceiver extends BroadcastReceiver {

   @Override
    public void onReceive(Context context, Intent intent) {

        Intent toDialog = new Intent(context, Main2Activity.class);
        toDialog.setFlags(FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(toDialog);
    }
}

Send Broadcast on overlay button clicked.

    @Override
    public void onClick(View v) {
        broadcastClick();
    }

   private void broadcastClick() {
        final Intent intent = new Intent("user.clicked.overlay.button");
        final LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(this);
        broadcastManager.sendBroadcast(intent);
    }

Register Broadcast receiver onCreate()

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

        overlayShowing = new OverlayClickReceiver();
        .......
    }

   private void registerClickReceiver() {
        LocalBroadcastManager.getInstance(this).registerReceiver(overlayShowing,
                new IntentFilter("user.clicked.overlay.button"));
    }

Unregister Broadcast receiver onDestroy()

@Override
public void onDestroy() {
    super.onDestroy();
    ..........
    unregisterClickReceriver();
}


   private void unregisterClickReceriver() {
        LocalBroadcastManager.getInstance(this).unregisterReceiver(overlayShowing);
    }
nishon.tan
  • 969
  • 2
  • 8
  • 21
0

From what i understood from the question, you want to get a view form xml files and add it to a window.

You can use View view = LayoutInflater.from(context).inflate(r.layout.overlay,null,false); and add that view with WindowManager.addView(view) to the window.

Adib Faramarzi
  • 3,798
  • 3
  • 29
  • 44