Search code examples
androidandroid-layoutrippledrawableandroid-include

Android: <include> with RippleEffect & StateListAnimator


I have a layout, that includes another layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="vertical"
          android:id="@+id/layout1">

    <include layout="@layout/my_layout"/>
</LinearLayout>

I need to add a RippleEffect as well as a StateListAnimator to the included layout.

Example:

<include layout="@layout/my_layout"
          android:stateListAnimator="@anim/lift_up"
          android:background="@drawable/ripple_effect"/>

Both the RippleEffect and StateListAnimator work 100%. I cannot alter the included layout. Thus the reason why I need to do the effects either on the include tag or the parent layout itself.

I have tried both techniques, none of which have been successful.

UPDATE

If possible, this should be down programmatically.

UPDATE 2

Secondly, how would I go about keep the View elevated, once it has animated?


Solution

  • You need to find the view and call the appropriate method to change the state list animator and the background. You might need to also call setClickable on the root view of you included layout.

    LinearLayout layout1 = findViewById(R.id.layout1);
    View root = layout1.getChildAt(0);
    
    StateListAnimator sla = AnimatorInflater.loadStateListAnimator(context, R.anim.lift_up); 
    root.setStateListAnimator(sla);
    root.setBackground(R.drawable.ripple_effect);