-1

I have a cutstom DialogFragment to show a loading message to the user. For the loading message image, I created an animation that goes from the left part of the screen (starting from outside the screen) to the right of the screen (finishing outside the screen). To do so, I thought of starting the animation on y=-imageWith and finish in y=imageWith+dialogWith:

public class MensajeDialogFragment extends DialogFragment {
    TextView mTvMensaje;
    TextView mTvTitulo;     

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();

        ImageView loadingImage = (ImageView) dialogView.findViewById(R.id.ivBus);
        View dialogView = inflater.inflate(R.layout.layout_mensaje_dialog, null);
        TextView mTvTitulo = (TextView) dialogView.findViewById(R.id.tvTitulo);
        TextView mTvMensaje = (TextView) dialogView.findViewById(R.id.tvMensaje);
                    mTvTitulo.setText(getArguments().getString(getString(R.string.bundle_titulo), ""));
        mTvMensaje.setText(getArguments().getString(getString(R.string.bundle_mensaje), ""));

        builder.setView(dialogView);

        TranslateAnimation izqADerAnimacion = new TranslateAnimation(-loadingImage.getWidth(), dialogView.getWidth()+loadingImage.getWidth(), 0, 0);
        izqADerAnimacion.setDuration(3500);
        izqADerAnimacion.setRepeatCount(Animation.INFINITE);
        loadingImage.startAnimation(izqADerAnimacion);

        return builder.create();
    }    
}

But for some reason, the loadingImage.getWidth() and dialogView.getWidth() are returning 0. What can I do to solve this without hardcoding the starting and finishing positions?

IIRed-DeathII
  • 1,117
  • 2
  • 15
  • 34

2 Answers2

1
    loadingImage.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            if (Build.VERSION.SDK_INT > 16)
                loadingImage.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            else
                loadingImage.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            //do animation
        }
    });

hope this works

Arjun Gurung
  • 431
  • 3
  • 17
-1
  TranslateAnimation izqADerAnimacion = 
      new TranslateAnimation(-loadingImage.getWidth(),
           dialogView.getWidth()+loadingImage.getWidth(), 0, 0);

In your code above mentioned you can directly give some value like

TranslateAnimation animation = new TranslateAnimation(-970.0f, 2000.0f, 0.0f, 0.0f);

It works nicely in Nexus 9 tab.

Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
Hints
  • 1
  • 2