tags:

views:

192

answers:

0

Hi Friends,

I want to animate a layout on touching the screen. If you touch the screen the layout containg buttons on it should translate down out of the screen and again on touching the screen the layout should come up into the screen at its original position.

I am getting the problem,when I do it through programming. and It works when I do it through XML.

In programming approch it works for the first time and on second time it does not works. Even I have observed that If we click the on screen It reads all the code of starting the animation but it does not get animated but after that if we click on the button added in layout, the animation works.

Here is the code:

import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.Gravity; import android.view.MotionEvent; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.view.View.OnClickListener; import android.view.View.OnTouchListener; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.view.animation.LayoutAnimationController; import android.view.animation.Animation.AnimationListener; import android.webkit.WebView; import android.widget.Button; import android.widget.FrameLayout; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.RelativeLayout.LayoutParams;

public class BrowserDemo2 extends Activity implements AnimationListener, OnTouchListener, OnClickListener { WebView browser; RelativeLayout parentLayout; LinearLayout navigationBarOuterLinearLayout; FrameLayout navigationBarInnerFrameLayout;

Button galleryButton;
Button creditsButton;
Button viewChangeButton;

byte animationType;
public static final byte ANIM_IN = 0;
public static final byte ANIM_OUT = 1;
boolean isNavigationBarVisible = true;
boolean isAnimationInProgress;


@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);     
setFullscreen();

setContentView(R.layout.main);  
parentLayout = (RelativeLayout) findViewById(R.id.parent);

setNavigationBarLayout();
parentLayout.addView(navigationBarOuterLinearLayout);

animationType = ANIM_OUT;
animController = new LayoutAnimationController(this, null);

}   

public void setNavigationBarLayout()
{
navigationBarOuterLinearLayout = new LinearLayout(this);
LayoutParams navigationBarOuterLinearLayoutParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);  
navigationBarOuterLinearLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
navigationBarOuterLinearLayout.setLayoutParams(navigationBarOuterLinearLayoutParams);   
navigationBarOuterLinearLayout.setOrientation(LinearLayout.VERTICAL);   

navigationBarInnerFrameLayout = new FrameLayout(this);
LayoutParams navigationBarInnerFrameLayoutParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);   
navigationBarInnerFrameLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
navigationBarInnerFrameLayout.setLayoutParams(navigationBarInnerFrameLayoutParams);
navigationBarInnerFrameLayout.setBackgroundResource(R.drawable.uitabbar);
navigationBarInnerFrameLayout.setPadding(0, 5, 0, 0);

galleryButton = new Button(this);
galleryButton.setOnClickListener(this);
galleryButton.setText("Gallery");
FrameLayout.LayoutParams galleryButtonParams = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER_VERTICAL | Gravity.LEFT);
galleryButton.setLayoutParams(galleryButtonParams);

creditsButton = new Button(this);
creditsButton.setOnClickListener(this);
creditsButton.setText("Credits");
FrameLayout.LayoutParams creditsButtonParams = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
creditsButton.setLayoutParams(creditsButtonParams);

viewChangeButton = new Button(this);
viewChangeButton.setOnClickListener(this);
viewChangeButton.setText("Chnage View");
FrameLayout.LayoutParams viewChangeButtonParams = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER_VERTICAL | Gravity.RIGHT);
viewChangeButton.setLayoutParams(viewChangeButtonParams);

navigationBarInnerFrameLayout.addView(galleryButton);
navigationBarInnerFrameLayout.addView(creditsButton);
navigationBarInnerFrameLayout.addView(viewChangeButton);

navigationBarOuterLinearLayout.addView(navigationBarInnerFrameLayout);
}

public void setFullscreen()
{
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);
}    

public void setAnimations()
{
switch(animationType)
{
    case ANIM_IN:
    {
    isAnimationInProgress = true;
    navigationBarOuterLinearLayout.setVisibility(View.VISIBLE);     
    animateLayout(R.anim.lower_navigation_bar_in, navigationBarOuterLinearLayout);

// parentLayout.requestFocus(); // parentLayout.requestFocusFromTouch(); // parentLayout.requestLayout(); break; } case ANIM_OUT: {
isAnimationInProgress = true; animateLayout(R.anim.lower_navigation_bar_out, navigationBarOuterLinearLayout); break; } }
}

@Override
public boolean onTouchEvent(MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_UP)
{
    Log.v("onTouch", "in touch realease..");
    setAnimations();
}   
return true;    
}



Animation layoutAnimation;
LayoutAnimationController animController;
public void animateLayout(int type, LinearLayout layout)
{
layoutAnimation = AnimationUtils.loadAnimation(this, type);
layoutAnimation.setAnimationListener(this);
animController.setAnimation(layoutAnimation);
layout.setLayoutAnimation(animController);
layout.startLayoutAnimation();
}

@Override
public void onAnimationEnd(Animation animation)
{
if(animationType == ANIM_OUT && isNavigationBarVisible)
{
    animationType = ANIM_IN;
    isNavigationBarVisible = false;
    navigationBarOuterLinearLayout.setVisibility(View.INVISIBLE);
}
else
{
    animationType = ANIM_OUT;
    isNavigationBarVisible = true;      
}
isAnimationInProgress = false;
}

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

}

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

}

@Override
public boolean onTouch(View v, MotionEvent event)
{
return false;
}

@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
}