Expand a LinearLayout blocked by a ListView
<p>This is a nice one. I have a simple XML Layout:
menuprincipal.xml</p>

<pre><code>    &lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/white"&gt;
    &lt;LinearLayout
        android:id="@+id/llContent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        &gt;
        &lt;LinearLayout 
            android:id="@+id/menuSlide"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:background="@color/black"
            android:layout_weight="0.01"&gt;

        &lt;/LinearLayout&gt;
        &lt;LinearLayout 
            android:id="@+id/resultadosEquipo"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:background="@color/white"
            android:layout_weight="0.99"
            android:orientation="vertical"&gt;
            &lt;LinearLayout
                android:id="@+id/layoutPrueba"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:background="@color/white"
                &gt;
                &lt;ListView android:id="@+id/listNoticias"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    /&gt;
            &lt;/LinearLayout&gt;
        &lt;/LinearLayout&gt;        
    &lt;/LinearLayout&gt;
&lt;/FrameLayout&gt;
</code></pre>

<p>Then i have two classes to expand the "menuSlide" layout:</p>

<p>ExpandAnimation.java</p>

<pre><code>    package app.rosunad.animations;

import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;


public class ExpandAnimation extends Animation implements Animation.AnimationListener{

    private View view;
    private static int ANIMATION_DURATION;
    private static final String LOG_CAT = "ExpandAnimation";
    private int lastWidth;
    @SuppressWarnings("unused")
    private int fromWidth;
    private int toWidth;
    @SuppressWarnings("unused")
    private static int STEP_SIZE=30;

    public ExpandAnimation(View v,int fromWidth, int toWidth, int duration){
        Log.v(LOG_CAT, "Entramos en el constructor del ExpandAnimation");
        view = v;
        ANIMATION_DURATION = duration;
        this.fromWidth = fromWidth;
        this.toWidth = toWidth;

        setDuration(ANIMATION_DURATION);
        setRepeatCount(duration);
        setFillAfter(false);
        setInterpolator(new AccelerateInterpolator());
        setAnimationListener(this);
        startNow();
    }

    @Override
    public void onAnimationEnd(Animation animation) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onAnimationRepeat(Animation animation) {
        // TODO Auto-generated method stub
        Log.v(LOG_CAT, "Entra en el onAnimationRepeat");
        LayoutParams lyp = view.getLayoutParams();
        lyp.width = lastWidth += toWidth/ANIMATION_DURATION;
        view.setLayoutParams(lyp);
    }

    @Override
    public void onAnimationStart(Animation animation) {
        // TODO Auto-generated method stub
        Log.v(LOG_CAT, "Entra en el onAnimationStart");
        LayoutParams lyp = view.getLayoutParams();
        lyp.width = 0;
        view.setLayoutParams(lyp);
        lastWidth=0;
    }

}
</code></pre>

<p>and CollapseAnimation.java</p>

<pre><code>    package app.rosunad.animations;

import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;

@SuppressWarnings("unused")
public class CollapseAnimation extends Animation implements Animation.AnimationListener{

    private View view;
    private static int ANIMATION_DURATION;
    private int lastWidth;
    private int fromWidth;
    private int toWidth;
    private static int STEP_SIZE=30;

    public CollapseAnimation(View v,int fromWidth,int duration){
        this.view = v;
        LayoutParams lyp = v.getLayoutParams();
        this.fromWidth = lyp.width;
        this.toWidth = lyp.width;
        ANIMATION_DURATION = duration;
        setRepeatCount(ANIMATION_DURATION);
        setFillAfter(false);
        setInterpolator(new AccelerateInterpolator());
        setAnimationListener(this);
    }
    @Override
    public void onAnimationEnd(Animation arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onAnimationRepeat(Animation arg0) {
        // TODO Auto-generated method stub
        LayoutParams lyp = view.getLayoutParams();
        lyp.width = this.lastWidth-(this.toWidth/ANIMATION_DURATION);
        this.lastWidth = lyp.width;
        view.setLayoutParams(lyp);
    }

    @Override
    public void onAnimationStart(Animation arg0) {
        // TODO Auto-generated method stub
        LayoutParams lyp = view.getLayoutParams();
        this.lastWidth = lyp.width;
    }

}
</code></pre>

<p>All this is controlled by te activity, MenuPrincipal.java:</p>

<pre><code>    public class MenuPrincipal extends ActionBarActivity {
    public static String URL_BASE_FOTOS = "http://test";
    public static String URL_BASE = "http://test/";
    public static String URL_NOTICIAS = URL_BASE + "getNoticias.php";
    public static final String EXTRA_IDUSUARIO = "app.rosunad.lls.idusuario";
    public static final String LOG_TAG = "MenuPrincipal";
    public int idUsuario = 0;
    public boolean icon_home_changed = false;

    public ArrayList&lt;Noticia&gt; arrNoticias = new ArrayList&lt;Noticia&gt;();

    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        Intent antIntent = getIntent();
        idUsuario = antIntent.getIntExtra(EXTRA_IDUSUARIO, 0);

        setHomeNavigation();
        setContentView(R.layout.menuprincipal);

        new GetNoticias().execute(URL_BASE_FOTOS);
    }

    public boolean onCreateOptionsMenu(Menu menu){
        Log.v(LOG_TAG,"En el onCreateOptionsMenu del MenuPrincipal");
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.menuactionbar, menu);

        return super.onCreateOptionsMenu(menu);
    }

    public boolean onOptionsItemSelected(MenuItem item){
        switch(item.getItemId()){
            case R.id.menu_refresh:
                Toast.makeText(getApplicationContext(), "Fake Refresh", Toast.LENGTH_SHORT).show();
                getActionBarHelper().setRefreshActionItemState(true);
                getWindow().getDecorView().postDelayed(new Runnable(){
                    public void run(){
                        getActionBarHelper().setRefreshActionItemState(false);
                    }
                }, 1000);
                break;
            case android.R.id.home:
                getActionBarHelper().setHomeIconItemState(icon_home_changed);
                LinearLayout menuSlide = (LinearLayout)findViewById(R.id.menuSlide);
                DisplayMetrics metrics = new DisplayMetrics();
                getWindowManager().getDefaultDisplay().getMetrics(metrics);
                int screenWidth = metrics.widthPixels;

                if(icon_home_changed){
                    menuSlide.startAnimation(new CollapseAnimation(menuSlide, (int)(screenWidth*0.7), 10));
                }else{
                    menuSlide.startAnimation(new ExpandAnimation(menuSlide, 0, (int)(screenWidth*0.7), 10));
                }


                // Vamos a iniciar la animación del frame
                icon_home_changed = !icon_home_changed;
                break;
        }

        return super.onOptionsItemSelected(item);
    }

    @TargetApi(14)
    public void setHomeNavigation(){
        if(Build.VERSION.SDK_INT&gt;=Build.VERSION_CODES.ICE_CREAM_SANDWICH){
            this.getActionBar().setHomeButtonEnabled(true);
        }
    }

    public void publishNoticias(){
        Log.v(LOG_TAG,"Entra en el publishNoticias");
        // Aqui es donde mostramos los datos de las noticias recogidas en GetNoticias
        NoticiasAdapter adap = new NoticiasAdapter(this,R.layout.fila_noticias,arrNoticias);
        ListView listView = (ListView)findViewById(R.id.listNoticias);
        listView.setAdapter(adap);
    }

    public class GetNoticias extends AsyncTask&lt;String,Void,ArrayList&lt;Noticia&gt;&gt;{

        @Override
        protected ArrayList&lt;Noticia&gt; doInBackground(String... params) {
            // TODO Auto-generated method stub
            // Aqui hacemos la consulta a la base de datos para recoger las noticias
            Log.v(LOG_TAG,"Entra en el doInBackground");
            Bitmap icono;
            ArrayList&lt;Noticia&gt; response = new ArrayList&lt;Noticia&gt;();
            JSONObject fila;
            try {

                HttpClass cox = new HttpClass();
                cox.setURL(URL_NOTICIAS);

                cox.SqlEx();

                // Ahora recogemos los valores y vamos creando el Array
                if(cox.GetJResponseArray()!=null){
                    CheckResponse ck = new CheckResponse(cox.GetJResponseArray());

                    if(ck.HasRowsArray()){
                        Log.v(LOG_TAG,"Tiene filas");
                        for(int i = 0 ; i &lt; cox.GetJResponseArray().length()-1; i ++){
                            fila = cox.GetJResponseArray().getJSONObject(i);

                            if(fila.getInt("id_noticia")!=0){
                                // Debemos cargar primero el icono de la noticia
                                if(fila.getInt("id_foto")!=0){
                                    icono = BitmapFactory.decodeStream((InputStream) new URL(URL_BASE_FOTOS + "foto" + fila.getInt("id_foto") + "P.jpg").getContent());
                                }else{
                                    icono = BitmapFactory.decodeStream((InputStream) new URL(URL_BASE_FOTOS + "nop.gif").getContent());
                                }
                                Log.v(LOG_TAG,"Añade elementos");
                                response.add( new Noticia(fila.getInt("id_noticia"),fila.getString("titular"),icono ));
                            }
                        }
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            return response;
        }

        @Override
        protected void onPostExecute(ArrayList&lt;Noticia&gt; result){
            Log.v(LOG_TAG,"Entra en el onPostExecute");
            Log.v(LOG_TAG,"El resultado tiene: " + result.size());
            arrNoticias = result;
            publishNoticias();
        }

    }
}
</code></pre>

<p>Doing different test everything works fine until i introduce the ListView element, since that moment when i call ExpandAnimation (clicking the home button) only reach to the constructor, onAnimationStart doesn't fire. But, and this is funny, if just after click the home button do a click on a item of the ListView the animation start. :o</p>

<p>Doing a new click on home button launch CollapseAnimation without problem. If a remove the listview everything works fine. </p>
