Search code examples
androidandroid-imageviewandroid-windowmanagerandroid-4.1-jelly-bean

ImageView inside WindowManager not showing in Android 4.1 and 4.4 in home screen


I have ImageView with static image. When I add this ImageView to WindowManager using TYPE_PHONE, it shows up fine on all Android versions (4.1, 4.4, 5.0) However, when I press home button and go out of the application, in 4.1 and 4.4 the ImageView disappears. It works fine in 5.0

I don't see any exception thrown. When I run this in Android 4.4 I just see following error, which I found in other posts I can safely ignore?

1613-1613/info.bosung.smate E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from GradienCache
1613-1613/info.bosung.smate E/OpenGLRenderer﹕ MAX_TEXTURE_SIZE: 16384
1613-1613/info.bosung.smate E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
1613-1613/info.bosung.smate E/OpenGLRenderer﹕ MAX_TEXTURE_SIZE: 16384

I have included 3 screenshots showing what happens (4.4 and 4.1 is different too).

example of ImageView not working in 4.4 and 4.1

Here is my Code.

public class MainActivity extends ActionBarActivity {

private WindowManager windowManager;
ImageView rocketImage1;

WindowManager.LayoutParams params;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

    rocketImage1 = new ImageView(this);
    rocketImage1.setBackgroundResource(R.drawable.a1);

    // Setup layout parameter
    params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT);

    params.gravity = Gravity.TOP | Gravity.LEFT; // Orientation
    params.x = 150; // where you want to draw this, coordinates
    params.y = 150;
    params.alpha = 0.5f;
    params.width = 150;
    params.height = 250;

    // At it to window manager for display, it will be printed over any thing
    windowManager.addView(rocketImage1, params);
 }
 }

Solution

  • I was able to fix the problem by running WindowManager from a Service. Basically something changed in the lifecycle of Android 5 which made it work from Activity which actually confused me more.

    Here is the code that actually works.

    Animate ImageView inside WindowManager - Android