Android Tips and Tricks

 
1) ImageView setCropToPadding by code when setting CENTER_CROP as scaleType:
====================================================================

Field field;
try {
           field = ImageView.class.getDeclaredField("mCropToPadding");
           field.setAccessible(true);
            field.set((ImageView) convertView, true);
     } catch (NoSuchFieldException e) {
            e.printStackTrace();
     } catch (IllegalAccessException e) {
            e.printStackTrace();
     }

2) Always do animation before / after layout pass, never do it in the middle .

3) Relative Layout is not the best layout always, use Linearlayout / Framelayout if it can be used in that context.(because it has 2 measure pass, more complicated algorithm).

4) There is a method called setHasTransientState() for every view. It set whether the view is currently tracking transient state that the framework should attempt to preserve when possible.One use case where developers use this method when you want to preserve listview child views from recycling where you want to perform some animations (for eg listview swipe to delete animations etc)

5) Nested Fragment
================
 Use getChildFragmentManager() instead of getFragmentManager()

6) Nested Fragment : IllegalStateException:- Activity has been destroyed
===========================================================

Solution:
This seems to be a bug in the newly added support for nested fragments. Basically, the child FragmentManager ends up with a broken internal state when it is detached from the activity. A short-term workaround that fixed it for me is to add the following to onDetach() of every Fragment which you call getChildFragmentManager() on:
 
@Override
public void onDetach() {
   super.onDetach();

   try {
       Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
       childFragmentManager.setAccessible(true);
       childFragmentManager.set(this, null);

   } catch (NoSuchFieldException e) {
       throw new RuntimeException(e);
   } catch (IllegalAccessException e) {
       throw new RuntimeException(e);
   }
}

7) Xml Animations
===============
accelerate_decelerate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
  android:interpolator="@android:anim/accelerate_decelerate_interpolator">
  <translate
      android:fromYDelta="-100%p"
      android:toYDelta="0"
      android:duration="2000"/>
</set>

accelerate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
  android:interpolator="@android:anim/accelerate_interpolator">
  <translate
      android:fromYDelta="-100%p"
      android:toYDelta="0"
      android:duration="2000"/>
</set>

anticipate_overshoot.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
  android:interpolator="@android:anim/anticipate_overshoot_interpolator">
  <translate
      android:fromYDelta="-80%p"
      android:toYDelta="0"
      android:duration="2000"/>
</set>

anticipate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
  android:interpolator="@android:anim/anticipate_interpolator">
  <translate
      android:fromYDelta="-80%p"
      android:toYDelta="0"
      android:duration="2000"/>
</set>

bounce.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
  android:interpolator="@android:anim/bounce_interpolator">
  <translate
      android:fromYDelta="-100%p"
      android:toYDelta="0"
      android:duration="2000"/>
</set>

cycle.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
  android:interpolator="@android:anim/cycle_interpolator">
  <translate
      android:fromYDelta="-50%p"
      android:toYDelta="0"
      android:duration="2000"/>
</set>

decelerate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
  android:interpolator="@android:anim/decelerate_interpolator">
  <translate
      android:fromYDelta="-100%p"
      android:toYDelta="0"
      android:duration="2000"/>
</set>

linear.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
  android:interpolator="@android:anim/linear_interpolator">
  <translate
      android:fromYDelta="-100%p"
      android:toYDelta="0"
      android:duration="2000"/>
</set>

overshoot.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
  android:interpolator="@android:anim/overshoot_interpolator">
  <translate
      android:fromYDelta="-80%p"
      android:toYDelta="0"
      android:duration="2000"/>
</set>

Comments