Implementation principle and sample code of floating window in Android

Source: Internet
Author: User

It took me a weekend to get angry with me. On this issue, I rummaged over the global network without a decent piece of information. Now I will briefly describe the implementation principles as follows:

Call windowmanager and set the relevant properties of windowmanager. layoutparams. Create a view using the addview method of windowmanager. In this way, the generated view will have different effects depending on the properties of windowmanager. layoutparams. For example, you can create a system top-level window to implement the floating window effect!

The windowmanager method is very simple. Basically, three addviews, removeview, and updateviewlayout are used.

Windowmanager. layoutparams has many attributes, which are very rich. For more information, see the SDK documentation. The windowmanager. Java source code in Android is provided here. For details, see.

The following is a simple sample code:

Public class myfloatview extends activity {/** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); button BB = new button (getapplicationcontext (); windowmanager WM = (windowmanager) getapplicationcontext (). getsystemservice ("window"); windowmanager. layoutparams wmparams = new windowmanager. layoutparams ();/*** The following are windowmanager. layoutparams-related attributes * for specific purposes, see the SDK Documentation */wmparams. type = 2002; // here is the key. You can also try 2003 wmparams. format = 1;/*** the flags here are also critical * the code is actually wmparams. flags | = flag_not_focusable; * 40 is the default attribute of wmparams (32) + flag_not_focusable (8) */wmparams. flags = 40; wmparams. width = 40; wmparams. height = 40; WM. addview (BB, wmparams); // create view }}

Do not forget to add permissions in androidmanifest. xml:

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

PS: here is an example of the meaning of the type value:

        /**         * Window type: phone.  These are non-application windows providing         * user interaction with the phone (in particular incoming calls).         * These windows are normally placed above all applications, but behind         * the status bar.         */        public static final int TYPE_PHONE              = FIRST_SYSTEM_WINDOW+2;         /**         * Window type: system window, such as low power alert. These windows         * are always on top of application windows.         */        public static final int TYPE_SYSTEM_ALERT       = FIRST_SYSTEM_WINDOW+3;

The value of first_system_window is 2000. The difference between 2003 and 2002 is that 2003 views are higher than 2002 Views and can be displayed on the system drop-down status bar!

-----------------------------

You have provided a demo for freely moving the floating window.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.