Introduction
A 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.
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
- android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
-
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Show Floating Menu"
- android:id="@+id/showMenu"
- android:layout_centerVertical="true"
- android:layout_centerHorizontal="true" />
- </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.
- Button showMenu = (Button) findViewById(R.id.showMenu);
- showMenu.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- startService(new Intent(MainActivity.this, FloatWindowService.class));
- }
- });
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.
- windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
- linearLayout = new LinearLayout(this);
- LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
- linearLayout.setBackgroundColor(Color.argb(66, 255, 0, 0));
- linearLayout.setLayoutParams(layoutParams);
- TextView textView = new TextView(this);
- textView.setText("This is a floating window");
- textView.setTextColor(Color.BLUE);
- textView.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
- LinearLayout.LayoutParams layoutParamsText = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
- // linearLayout.setBackgroundColor(Color.argb(66, 255, 0, 0));
- textView.setLayoutParams(layoutParamsText);
-
- linearLayout.addView(textView);
- final WindowManager.LayoutParams params = new WindowManager.LayoutParams(400,150,WindowManager.LayoutParams.TYPE_PHONE,WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);
- params.x = 0;
- params.y = 0;
- params.gravity = Gravity.CENTER | Gravity.CENTER;
- 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.
- linearLayout.setOnTouchListener(new View.OnTouchListener() {
- WindowManager.LayoutParams updatedParams = params;
- int x,y;
- float touchX,touchY;
- @Override
- public boolean onTouch(View view, MotionEvent motionEvent) {
- switch (motionEvent.getAction()){
- case MotionEvent.ACTION_DOWN:
- x= updatedParams.x;
- y=updatedParams.y;
- touchX = motionEvent.getRawX();
- touchY = motionEvent.getRawY();
- break;
- case MotionEvent.ACTION_MOVE:
- updatedParams.x = (int)(x+(motionEvent.getRawX() - touchX));
- updatedParams.y = (int)(y+(motionEvent.getRawY() - touchY));
- windowManager.updateViewLayout(linearLayout,updatedParams);
- default:break;
- }
-
- return false;
- }
- })
- ;
Now every thing is ready, but before running the app we need to declare the service in the Manifest File like the following,
- <service android:name=".FloatWindowService"></service>
And we need to add an important permission in the manifest file,
- <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"></uses-permission>
Please see the screen shot also.