0

Ever since convert ConstraintLayout to MotionLayout handler.postdelayed not work

These are my codes

Layout

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.motion.widget.MotionLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/jjjk"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary"
    app:layoutDescription="@xml/chapter2_scene2">

    <ImageView
        android:id="@+id/image_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:src="@drawable/ic_launcher_background"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_percent="0.3"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintWidth_percent="0.5" />

</androidx.constraintlayout.motion.widget.MotionLayout>

Java

Handler mHandler = new Handler();
        mHandler.postDelayed(new Runnable() {
            public void run() {
                imageView.setVisibility(View.VISIBLE);
            }
        }, 100);

But it works if I write the imageView.setVisibility(View.VISIBLE); outside the handler.postdelayed

The problem is that I android:visibility="gone" the image view

If I use the android:visibility="invisible", there is no problem

But I want to delay the display of larger images with this method so that the page loads well

What is the problem?

alkkhu
  • 53
  • 5

1 Answers1

0

It is depend when you call this method. It should work if you call it when activity/fragment is visible to you. Try this :

    @Override
    protected void onResume() {
        Handler mHandler = new Handler(Looper.myLooper());
        mHandler.postDelayed(new Runnable() {
            public void run() {
                View imageView = findViewById(R.id.image_view);
                imageView.setVisibility(View.VISIBLE);
            }
        }, 1000);
        super.onResume();
    }
i30mb1
  • 3,894
  • 3
  • 18
  • 34