Floating Window In Android

Floating window is a window in Android which will appear above all the applications in Android. This can be used in the case where the user wants to show something above all the applications in Android. Here I am going to show you how to create a floating window. For this I have an activity and  a service. On the activity there is a button in the User interface, and on clicking on the button a floating window will be shown and the user can drag the window to any position he wants and on minimizing/closing the application also the window will be shown because the service is running in the background. The service is handling this floating window. First create the activity layout file like the following.

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2. xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"  
  3. android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"  
  4. android:paddingRight="@dimen/activity_horizontal_margin"  
  5. android:paddingTop="@dimen/activity_vertical_margin"  
  6. android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">  
  7.   
  8.     <Button  
  9. android:layout_width="wrap_content"  
  10. android:layout_height="wrap_content"  
  11. android:text="Show Floating Menu"  
  12. android:id="@+id/showMenu"  
  13. android:layout_centerVertical="true"  
  14. android:layout_centerHorizontal="true" />  
  15. </RelativeLayout>  
Now bind the button to the java class in the oncreate() method and on clicking in the button start the service for the floating window.
  1. Button showMenu = (Button) findViewById(R.id.showMenu);  
  2. showMenu.setOnClickListener(new View.OnClickListener() {  
  3. @Override  
  4. public void onClick(View view) {  
  5.         startService(new Intent(MainActivity.this, FloatWindowService.class));  
  6.     }  
  7. });  
Now create the service class and in the onCreate() method please add the following code to show a window and a textview in it. Here I am using Window manager class to show the User interface. And in the window manager I added a Linear Layout and within that I added a TextView, please see the code below.
  1. windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);  
  2. linearLayout = new LinearLayout(this);  
  3.  LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);  
  4. linearLayout.setBackgroundColor(Color.argb(66, 255, 0, 0));  
  5. linearLayout.setLayoutParams(layoutParams);  
  6.  TextView textView = new TextView(this);  
  7.  textView.setText("This is a floating window");  
  8.  textView.setTextColor(Color.BLUE);  
  9.  textView.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);  
  10.  LinearLayout.LayoutParams layoutParamsText = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);  
  11. // linearLayout.setBackgroundColor(Color.argb(66, 255, 0, 0));  
  12.  textView.setLayoutParams(layoutParamsText);  
  13.   
  14. linearLayout.addView(textView);  
  15. final WindowManager.LayoutParams params = new WindowManager.LayoutParams(400,150,WindowManager.LayoutParams.TYPE_PHONE,WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);  
  16.  params.x = 0;  
  17.  params.y = 0;  
  18.  params.gravity = Gravity.CENTER | Gravity.CENTER;  
  19.  windowManager.addView(linearLayout, params);  
This will create a window and will show that window in the screen on clicking in the button of the Activity. But we need to move the floating window and for that I  am adding the touch listner for the Linear Layout. Please see the code below.
  1. linearLayout.setOnTouchListener(new View.OnTouchListener() {  
  2.     WindowManager.LayoutParams updatedParams = params;  
  3. int x,y;  
  4. float touchX,touchY;  
  5. @Override  
  6. public boolean onTouch(View view, MotionEvent motionEvent) {  
  7. switch (motionEvent.getAction()){  
  8. case MotionEvent.ACTION_DOWN:  
  9. xupdatedParams.x;  
  10. y=updatedParams.y;  
  11. touchX = motionEvent.getRawX();  
  12. touchY = motionEvent.getRawY();  
  13. break;  
  14. case MotionEvent.ACTION_MOVE:  
  15. updatedParams.x = (int)(x+(motionEvent.getRawX() - touchX));  
  16. updatedParams.y = (int)(y+(motionEvent.getRawY() - touchY));  
  17. windowManager.updateViewLayout(linearLayout,updatedParams);  
  18. default:break;  
  19.         }  
  20.   
  21. return false;  
  22.     }  
  23. })  
Now every thing is ready, but before running the app we need to declare the service in the Manifest File like the following,
  1. <service android:name=".FloatWindowService"></service>  
And we need to add an important permission in the manifest file,
  1. <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"></uses-permission>  
Please see the screen shot also. 
 
 

Up Next
    Ebook Download
    View all
    Learn
    View all