Alex Mercer Alex Mercer - 1 year ago 147
Android Question

Start ObjectAnimator after fragment transaction

i have this method to scroll my textview from left to right:

public void scrollText() {
float startX = 0 - scrollTxt.getWidth();
float endX = scrollTxt.getWidth();
ObjectAnimator anim = ObjectAnimator.ofFloat(scrollTxt, View.X, startX, endX);

this method and the textview are in a fragment. i add this fragment in my activity with this code:

protected void onCreate(Bundle savedInstanceState) {

drawer = (DrawerLayout) findViewById(;

MainFragment mainFragment = new MainFragment();
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(, mainFragment);

NavigationView navigationView = (NavigationView) findViewById(;
navigationView.setNavigationItemSelectedListener(new MainNavigationListener(drawer));

i want to start animation when fragmnet added.
i put scrollText() method in fragmnet's onResume but it didn't work.

Answer Source

This happens because scrollTxt.getWidth()is zero in onResume() because your fragment view hierarchy hasn't been measured yet. As a solution, you can postpone the animation start to the next layout pass by calling

Try this:

public void onResume() {
    super.onResume(); Runnable() {
        @Override public void run() {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download