0

I have an onScrollListener animation in my Kotlin project and I will like to use that code in my java as well, I have tired to convert the Kotlin code to Java but i am see some error and my app crashes when it detects the onScrollListener the animation hides a TextView

This is the Kotlin Code

class MainActivity : AppCompatActivity() 
{
    private lateinit var category_recycler: RecyclerView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        
        var btnExtended = true
        //animated scroll
        var animator : ValueAnimator? = null

        //the note onscroll listener
        note_recycler.addOnScrollListener(object : RecyclerView.OnScrollListener(){
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                if (animator==null){
                    animator=createanimator()
                }
                if (dy>0 && btnExtended)
                {
                    animator!!.start()
                    btnExtended = !btnExtended
                }else if (dy<0 && !btnExtended){
                    animator!!.reverse()
                    btnExtended = !btnExtended

                }
            }
        })

  }

   private fun createanimator(): ValueAnimator{
        var textview = findViewById<TextView>(R.id.textview)  //textview i want to hide
        val initSize=textview.measuredWidth
        val animator=ValueAnimator.ofInt(initSize,0)
        animator.duration=250
        animator.addUpdateListener { animation ->
            val value = animation.animatedValue as Int
            val layoutParams = textview.layoutParams   //line 50
            layoutParams.width = value
            textview.requestLayout()

        }
        return animator
    }


}




  //So far this is the code i has converted to java
 public class MainActivity extends AppCompatActivity implements NoteListener {
  
    private RecyclerView note_recycler;

    private ValueAnimator animator = null;

    private Boolean btnExtended;

    private TextView textview;

    private LinearLayout.LayoutParams layoutParams;

      @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



          btnExtended = true;
        note_recycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if(animator==null){
                    animator=createanimator();

                }
                if (dy>0 && btnExtended)
                {
                    if(animator!=null){
                        animator.start();
                    }
                  
                    btnExtended = !btnExtended;
                }
                else if (dy<0 && !btnExtended){
                    if(animator!=null){
                        animator.reverse();
                    }
              
                    btnExtended = !btnExtended;

                }


            }
        });



      }

     
    private ValueAnimator createanimator() {
        textview = findViewById(R.id.textview);
        int initSize = textview.getMeasuredWidth();
        animator=ValueAnimator.ofInt(initSize,0);
        animator.setDuration(250);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(@NonNull ValueAnimator animation) {
                int value = (int) animation.getAnimatedValue();
                          //there is a missing line here named line 50 from the kotlin code,          Please can you help me convert it and also cross check the other code for the java convertion
                layoutParams.width =value;
                textview.requestLayout();

            }
        });

      return animator;
    }


}

//Please I am new to kotlin and i am not sure if this java convertion is right please can you check through it and convert the line 50 i was unable to convert

Archimedes Trajano
  • 35,625
  • 19
  • 175
  • 265
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Feb 18 '23 at 19:31

1 Answers1

0

After a few looking around i wont say i found the answer my self but it sure works :)

ViewGroup.LayoutParams layoutParams = textview.getLayoutParams();

that was the missing line 50 that works, shockingly there are no error with the rest of the conversion. +4hours haha Either way this is a OnScrollAnimation like in gmail with their compose button, i set the button to wrap content and when the animation hides the text in the button, it only shows the icon.