5

I need a view to show up at the top of my application, and when it's shown, it can keep showing at the top of all my application's other view(all the fragment and activity). It sounds like a Floating Action Button, but will always show at the top of my app.

I know I can do it via adding view to my phone's WindowManager, and hide it when I quit my app, show it again when I resume my app. This tricky method can work, but it also require some additional permission, which is I am trying to avoid.

If I only want to show in my app, can I achieve it without asking additional permission from user? If the answer is yes, then how? The key seems like some LayoutParams for the view, I tried but failed.

Would be nice if answer can show some detail code and explanation.

Anthonyeef
  • 2,595
  • 1
  • 27
  • 25

2 Answers2

5

You have to use WindowManager for this purpose

First add permission in manifest

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

Add image which you want to appear.

chatheadImg = (ImageView)chatheadView.findViewById(R.id.chathead_img);

Then make the service and add window manager to it.

WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
            PixelFormat.TRANSLUCENT);
    params.gravity = Gravity.TOP | Gravity.LEFT;
    params.x = 0;
    params.y = 100;

And just register touch events on view

chatheadView.setOnTouchListener(new View.OnTouchListener() {
@Override
        public boolean onTouch(View v, MotionEvent event) {
        Switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:

                      //To do
                    break;
                case MotionEvent.ACTION_MOVE:

                break;

});

check these tutorials for better understanding

http://www.androidhive.info/2016/11/android-floating-widget-like-facebook-chat-head/

https://github.com/henrychuangtw/Android-ChatHead

Abdul Kawee
  • 2,687
  • 1
  • 14
  • 26
  • 3
    Thanks for this answer, but I really don't want to ask for that permission from my user. Any way without this permission will be appreciated. – Anthonyeef Apr 26 '17 at 10:38
  • 1
    Below android 6.0 , you dont need to ask for that permission , but starting android 6.0 you have to explicitly ask for permissions. – Abdul Kawee Apr 26 '17 at 10:49
0

adding this permission in manifest

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

And then add this code

windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        LayoutInflater layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        layoutView = layoutInflater.inflate(R.layout.ringing_layout, null);
    phoneTv = layoutView.findViewById(R.id.phone);

    Log.d("TAG", "showLayout: " + phoneTv );
    p = new WindowManager.LayoutParams(
            // Shrink the window to wrap the content rather than filling the screen
            600, 600,
            WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                    | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED,
            PixelFormat.TRANSLUCENT);
    layoutView.findViewById(R.id.close).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            windowManager.removeView(layoutView);
        }
    });
    p.gravity = Gravity.CENTER | Gravity.CENTER;
    p.x = 0;
    p.y = 0;
    windowManager.addView(layoutView, p);
    Log.d("TAG", "showLayout: ");
Upendra Shah
  • 2,218
  • 17
  • 27