Search code examples
androidanimationandroid-linearlayout

LinearLayout animate() clips the view, clipChildren doesn't work


I'm trying to animate the container LinearLayout (20dp) and btn button (20dp) (moving them right), but they get clipped after the full width of the main LinearLayout (40dp). I have tried clipChildren="false" on both, but it doesn't work. I can't use match_parent sizes for layouts, because it is an overlay app and will block the touches behind.

Any ideas?

My layout:

<LinearLayout
    android:id="@+id/main"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:clipChildren="false"
    android:clipToPadding="false" >

    <LinearLayout
        android:id="@+id/container"
        android:layout_width="20dp"
        android:layout_height="80dp"
        android:clipChildren="false"
        android:clipToPadding="false" >        
    </LinearLayout>

    <ImageButton
        android:id="@+id/btn"
        android:layout_width="20dp"
        android:layout_height="80dp"
        android:padding="0dp"
        android:background="@drawable/btn" />

</LinearLayout>

My (simplified) code:

super.onCreate();       
windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
params = new WindowManager.LayoutParams(
    LayoutParams.WRAP_CONTENT, //can't have MATCH_PARENT
    LayoutParams.WRAP_CONTENT, //can't have MATCH_PARENT
    WindowManager.LayoutParams.TYPE_SYSTEM_ALERT | 
    WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
    WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
    PixelFormat.TRANSLUCENT);

LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
main = (LinearLayout) inflater.inflate(R.layout.main,null);

btn = (ImageButton)main.findViewById(R.id.btn);
container = (LinearLayout)main.findViewById(R.id.container);
btn.setOnClickListener(onClickListener);        
windowManager.addView(main, params);
}

Animation code:

public void onClick(View v) {
btn.animate().xBy(100f).setDuration(2000);
container.animate().xBy(100f).setDuration(2000);
}

Solution

  • Turns out you cannot draw outside the bounds of the initial area that was added to the screen using windowManager.addView.

    In my case to animate the views I had to use windowManager.updateViewLayout(view,layoutParams).