EasyWindow

repository·master·Indexed 23 days ago

https://github.com/getactivity/easywindow

An Android framework for simplifying the creation and management of floating windows (overlays). It provides a fluent API to support both local (Activity-bound) and global (system-wide) windows, including tools for lifecycle control, layout configuration, and global window management via EasyWindowManager.

Tokens
2.9K
Snippets
5
Records
9
Agent score
33%

What's inside EasyWindow

  1. Use EasyWindow in Java or Kotlin

    master

    You can create floating windows using either an Activity or an Application object.

    • Passing an Activity creates a local floating window (no overlay permission required).
    • Passing an Application creates a global floating window (requires overlay permission).

    Common configuration methods include setContentView, setWindowDuration, setWindowDraggableRule, and view-specific setters like setTextByTextView or setImageDrawableByImageView.

    // Java Usage
    EasyWindow.with(this)
            .setContentView(R.layout.toast_hint)
            .setWindowDuration(1000)
            .setImageDrawableByImageView(android.R.id.icon, R.mipmap.ic_dialog_tip_finish)
            .setTextByTextView(android.R.id.message, "Tap me to dismiss")
            .setOnClickListenerByView(android.R.id.message, new OnWindowViewClickListener<TextView>() {
                @Override
                public void onClick(@NonNull EasyWindow<?> easyWindow, @NonNull TextView view) {
                    easyWindow.cancel();
                }
            })
            .show();
    // Kotlin Usage
    EasyWindow.with(activity).apply {
        setContentView(R.layout.toast_hint)
        setWindowDuration(1000)
        setImageDrawableByImageView(android.R.id.icon, R.mipmap.ic_dialog_tip_finish)
        setTextByTextView(android.R.id.message, "Tap me to dismiss")
        setOnClickListenerByView(android.R.id.message, OnWindowViewClickListener<TextView?> { easyWindow, view ->
            easyWindow.cancel()
        })
    }.show()
  2. Display floating windows globally without overlay permission

    master

    If you do not have the floating window overlay permission, you cannot display windows globally across other applications. However, you can display them globally within your own application by monitoring the Activity lifecycle.

    To achieve this, use Application.ActivityLifecycleCallbacks to create a new floating window in every onActivityCreated callback.

    public final class WindowLifecycleControl implements Application.ActivityLifecycleCallbacks {
    
        static void with(Application application) {
            application.registerActivityLifecycleCallbacks(new FloatingLifecycle());
        }
    
        @Override
        public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
            EasyWindow.with(activity)
                    .setContentView(R.layout.xxx)
                    .show();
        }
    
        // ... implement other methods
    }
  3. Simulate global windows without permissions

    master

    If you do not have floating window permissions, you cannot display a window globally across other apps. However, you can simulate a global appearance within your own app by using Application.ActivityLifecycleCallbacks. This approach creates a new window for every Activity in your application.

    public final class WindowLifecycleControl implements Application.ActivityLifecycleCallbacks {
    
        static void with(Application application) {
            application.registerActivityLifecycleCallbacks(new FloatingLifecycle());
        }
    
        @Override
        public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
            EasyWindow.with(activity)
                    .setContentView(R.layout.xxx)
                    .show();
        }
    
        // ... implement other methods ...
    }
    public final class WindowLifecycleControl implements Application.ActivityLifecycleCallbacks {
    
        static void with(Application application) {
            application.registerActivityLifecycleCallbacks(new FloatingLifecycle());
        }
    
        @Override
        public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
            EasyWindow.with(activity)
                    .setContentView(R.layout.xxx)
                    .show();
        }
    
        ......
    }
  4. Install EasyWindow via Gradle

    master

    To use EasyWindow, you must first add the JitPack repository to your project and then add the dependency to your app module. The configuration depends on your Gradle version.

    For Gradle < 7.0

    Add the JitPack repository to your root build.gradle file:

    allprojects {
        repositories {
            maven { url 'https://jitpack.io' }
        }
    }

    For Gradle >= 7.0

    Add the JitPack repository to your settings.gradle file:

    dependencyResolutionManagement {
        repositories {
            maven { url 'https://jitpack.io' }
        }
    }

    Add Dependency

    In your app module's build.gradle, ensure you support JDK 1.8 and add the implementation line:

    android {
        compileOptions {
            targetCompatibility JavaVersion.VERSION_1_8
            sourceCompatibility JavaVersion.VERSION_1_8
        }
    }
    
    dependencies {
        implementation 'com.github.getActivity:EasyWindow:15.8'
    }
    implementation 'com.github.getActivity:EasyWindow:15.8'
  5. How to use EasyWindow (Java and Kotlin)

    master

    EasyWindow provides a builder pattern to create floating windows.

    Key Concept: Local vs Global Windows

    • Passing an Activity object to EasyWindow.with() creates a local window (does not require floating window permissions).
    • Passing an Application object to EasyWindow.with() creates a global window (requires floating window permissions).

    Java Example

    EasyWindow.with(this)
            .setContentView(R.layout.toast_hint)
            .setWindowDuration(1000)
            .setImageDrawableByImageView(android.R.id.icon, R.mipmap.ic_dialog_tip_finish)
            .setTextByTextView(android.R.id.message, "点我消失")
            .setOnClickListenerByView(android.R.id.message, new OnWindowViewClickListener<TextView>() {
                @Override
                public void onClick(@NonNull EasyWindow<?> easyWindow, @NonNull TextView view) {
                    easyWindow.cancel();
                }
            })
            .show();

    Kotlin Example

    EasyWindow.with(activity).apply {
        setContentView(R.layout.toast_hint)
        setWindowDuration(1000)
        setImageDrawableByImageView(android.R.id.icon, R.mipmap.ic_dialog_tip_finish)
        setTextByTextView(android.R.id.message, "点我消失")
        setOnClickListenerByView(android.R.id.message, OnWindowViewClickListener<TextView?> { easyWindow, view ->
            easyWindow.cancel()
        })
    }.show()
    EasyWindow.with(this)
            .setContentView(R.layout.toast_hint)
            .setWindowDuration(1000)
            .setImageDrawableByImageView(android.R.id.icon, R.mipmap.ic_dialog_tip_finish)
            .setTextByTextView(android.R.id.message, "点我消失")
            .setOnClickListenerByView(android.R.id.message, new OnWindowViewClickListener<TextView>() {
    
                @Override
                public void onClick(@NonNull EasyWindow<?> easyWindow, @NonNull TextView view) {
                    easyWindow.cancel();
                }
            })
            .show();
  6. EasyWindowManager Static API Reference

    master

    Use EasyWindowManager to manage all active or cached window instances globally.

    Window Management

    • cancelAllWindow(): Cancel all currently showing windows.
    • recycleAllWindow(): Recycle all currently showing windows.
    • showAllWindow(): Show all cancelled but not yet recycled windows.
    • getAllWindowInstances(): Get all current window instances.

    Finding Windows

    • cancelWindowByTag(String tag): Cancel a window by its assigned tag.
    • showWindowByTag(String tag): Show a window by its tag.
    • findWindowInstancesByTag(String tag): Find instances by tag.
    • cancelWindowByClass(Class clazz): Cancel windows of a specific class.
    • existWindowShowingByClass(Class clazz): Check if a window of a specific class is showing.
  7. EasyWindow Object API Reference

    master

    The EasyWindow instance provides methods to control the lifecycle, appearance, and content of the floating window.

    Lifecycle & Visibility

    • show(): Display the window.
    • delayShow(long delayMillis): Delay showing the window (thread-safe).
    • cancel(): Dismiss the window.
    • delayCancel(long delayMillis): Delay dismissing the window (thread-safe).
    • recycle(): Dismiss and recycle the window.
    • update(): Update the window (call after changing parameters).
    • isShowing(): Check if the window is currently visible.

    Content & Layout

    • setContentView(int layoutId): Set layout via resource ID.
    • setContentView(View view): Set layout via a View object.
    • setWindowSize(int width, int height): Set absolute size.
    • setWindowSizePercent(float widthPercent, float heightPercent): Set size as screen percentage.
    • setWindowLocation(int x, int y): Set position.
    • setWindowAlpha(float alpha): Set transparency (0.0 to 1.0).

    View Helpers (Direct manipulation)

    • setTextByTextView(int viewId, CharSequence text): Set text for a specific TextView.
    • setImageDrawableByImageView(int viewId, Drawable drawable): Set image for a specific ImageView.
    • setTextColorByTextView(int viewId, int colorValue): Set text color.
    • setBackgroundDrawableByView(int viewId, Drawable drawable): Set background for a specific view.

    Listeners

    • setOnClickListenerByView(int id, OnWindowViewClickListener listener): Click listener for a specific view.
    • setOnWindowLifecycleCallback(OnWindowLifecycleCallback callback): Listen to window lifecycle events.
    • setWindowDraggableRule(IWindowDraggableRule rule): Set dragging behavior (e.g., MovingWindowDraggableRule).
  8. EasyWindow Object Methods Reference

    master

    The EasyWindow instance provides methods to control the lifecycle, appearance, and behavior of a specific window.

    Lifecycle & Visibility:

    • show(): Display the window.
    • delayShow(long delayMillis): Show with a delay.
    • cancel(): Dismiss the window.
    • recycle(): Cancel and recycle the window.
    • isShowing(): Check if currently visible.
    • update(): Apply parameter changes.

    Content & Size:

    • setContentView(int layoutId): Set layout via resource ID.
    • setContentView(View view): Set custom view.
    • setWindowSize(int width, int height): Set absolute size.
    • setWindowSizePercent(float widthPercent, float heightPercent): Set size by screen percentage.

    Position & Appearance:

    • setWindowLocation(int x, int y): Set position.
    • setWindowAlpha(float alpha): Set transparency (0.0 to 1.0).
    • setBackgroundDimAmount(float amount): Set background dim (0.0 to 1.0).
    • setWindowAnim(int id): Set animation style.

    View Manipulation:

    • setTextByTextView(int viewId, CharSequence text): Set text for a specific TextView.
    • setImageDrawableByImageView(int viewId, Drawable drawable): Set image for a specific ImageView.
    • setVisibilityByView(int viewId, int visibility): Change visibility of a specific view.