Bulu
Bulu

Reputation: 1441

How to centre the same asset/drawables between Splash Screen Activity and other activity?

I'have created the splash screen without any layout xml file but this is done by the vector drawable with centring the app logo like below:

 <item name="android:windowBackground">@drawable/splash_background</item>

But I also would like to show the same app logo in the next subsequent screen. For e.q. In my case let us say Main Activity screen where I've a layout xml where I've centred the icon with the constraint layout.

activity_main.xml

    <!-- By adding android:layout_marginTop="26dp" to the ImageView kind of works but not sure why the magic number works
     Not sure where to get the same number for some other devices-->

    <ImageView
        android:id="@+id/imageViewLogo"
        android:layout_width="@dimen/splash_icon"
        android:layout_height="@dimen/splash_icon"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_invest_logo" />


</android.support.constraint.ConstraintLayout>

But I noticed when the same asset logo swap happens between Splash and Main Activity I can see a jumpy behaviour with the logos.

Appreciate your help. Below is the github for the entire source code.

https://github.com/nksaroj/InvestApp

You can see the jumpy issue with the green logo here

enter image description here

Upvotes: 0

Views: 88

Answers (1)

Bulu
Bulu

Reputation: 1441

getWindow().getDecorView().getRootView().getViewTreeObserver().addOnGlobalLayoutListener(
        new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                // Layout has happened here.

                float statusBarHeight = getStatusBarHeight() * 1f;

                //This is tricks where Samsung and some other devices don't consider the height in splash screen so that you need to adjust the height manually 
                View navigationBarBackground = findViewById(android.R.id.navigationBarBackground);

                if (navigationBarBackground == null) {
                    statusBarHeight = statusBarHeight * -1f;
                }

                imageViewIconY = imageViewReadyIcon.getY() + (statusBarHeight / 2);

                imageViewIconY.setY(imageViewIconY);



                // Don't forget to remove your listener when you are done with it.
                getWindow().getDecorView().getRootView().getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });

Upvotes: 0

Related Questions