4
\$\begingroup\$

Here's my android splash screen. I am trying to figure out a better way to write it. It just moves the logo up to the center of the screen. It's my third time creating a splash screen for an android app. I am just trying to figure out the best way to write more optimized code.

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

final public class Splash extends AppCompatActivity {
    private final static int timeout = 3000;
    final static Handler handler = new Handler();
    static ImageView logo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        logo = findViewById(R.id.companylogo);
        Animation animation = AnimationUtils.loadAnimation(Splash.this, R.anim.myanim);
        logo.startAnimation(animation);

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent splashscreen = new Intent(Splash.this, Home.class);
                startActivity(splashscreen);
            }

        }, timeout);

    }




}
\$\endgroup\$
4
\$\begingroup\$

This code looks reasonably clean and seems to accomplish what it's intended to do. Some minor nitpicks can still be improved here and there:

  • You're not consistent in visibility modifier ordering between class declaration and member declarations. It should either be public final class or final private static int timeout.
    Most Java conventions that I've seen default to the public static final ordering.

  • handler and logo are currently package-private. It's very unusual for that visibility to be necessary and I think setting these to private if at all possible would be better. (I'm not recommending protected, because the class can not be extended.)

  • static final members are usually named in SNAKE_CAPS (also known as shout-case).

  • the id companylogo might be better off named companyLogo to follow camel case conventions.

  • myanimation doesn't tell us anything about the animation. A better name would be splashScreenAnimation or logoAnimation

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.