Search code examples
androidviewdialogandroid-animationandroid-dialogfragment

Animate view in DialogFragment


Well I'm trying to animate a view in DialogFragment when is created and when is dismissed. But I can figure it out, it's not working for me. I've tried also all the answers pepole have gave about this here. I tried : onActivityCreate , setStyle(myCustom), oncreate etc.

I have a custom DialogFragment view. and i want to animate it when it open and when it closed.

Really need help here. Thanks ahead.

here is my code :

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

     inflater = context.getLayoutInflater();
      view = inflater.inflate(R.layout.details_page, container, false);
     //Setting up the image by id
     ImageView img = (ImageView)view.findViewById(R.id.details_img);
     img.setImageResource(this.imageId);
     //Setting up the title
     TextView tittle = (TextView)view.findViewById(R.id.title);
     tittle.setText(this.title);
     tittle.setGravity(Gravity.RIGHT);
     //Setting up all the details about the item
     TextView details = (TextView)view.findViewById(R.id.details);
     details.setText(this.details);
     details.setGravity(Gravity.RIGHT);

     return view;


}

Solution

  • try this . and let me know the updates ...
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    
        View layout = inflater.inflate(R.layout.my_layout, null);
    
        layMain = (LinearLayout) layout.findViewById(R.id.layMain);
    
        TextView btnCancel = (TextView) layout.findViewById(R.id.btnCancel);
            btnCancel.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    final Animation anim = AnimationUtils.loadAnimation(getActivity(), R.anim.translate_to_bottom);
                    anim.setAnimationListener(new AnimationListener() {
    
                        @Override
                        public void onAnimationStart(Animation animation) {
    
                        }
    
                        @Override
                        public void onAnimationRepeat(Animation animation) {
    
                        }
    
                        @Override
                        public void onAnimationEnd(Animation animation) {
                            dismiss();
                        }
                    });
                    layMain.startAnimation(anim);
                }
            });