0

With the code below my background image is not showing in my launchscreen's activity. I have also a thread with a sleep of three seconds but I see only white screen as background. After the delay, I redirect to another activity (in OnStart function). Without the redirect I see my image as background. How can I see my launchscreen image before I redirect to my second activity?

Thanks in advance for the help.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mContext = this;
    mAuth = FirebaseAuth.getInstance();

            RequestOptions options = new RequestOptions();
            options.centerCrop();

            RelativeLayout relativeLayout = new RelativeLayout(mContext);
            RelativeLayout.LayoutParams relLayoutParam = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
            imageView = new ImageView(mContext);
            imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            imageView.setBackgroundResource(R.drawable.launch_screen_image);
            relativeLayout.addView(imageView, relLayoutParam);

            setContentView(relativeLayout, relLayoutParam);
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

}

@Override
public void onStart() {
    super.onStart();
    // Check if user is signed in (non-null) and update UI accordingly.
    SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
    FirebaseUser currentUser = mAuth.getCurrentUser();
    if (currentUser != null && currentUser.getUid() != null
            && currentUser.getUid().equals(mPrefs.getString("UID", "DEFAULT"))) {
        Intent i = new Intent(mContext, SocialPage.class);
        startActivity(i);
    } else {
        Intent i = new Intent(mContext, Login.class);
        startActivity(i);
    }
}
Quick learner
  • 10,632
  • 4
  • 45
  • 55
Carlo
  • 813
  • 1
  • 15
  • 34

2 Answers2

1

Try this, explained every line with comments in the code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

        //Other stuff
        //...
            //Remove this line ==> imageView.setBackgroundResource(R.drawable.launch_screen_image);
            relativeLayout.addView(imageView, relLayoutParam);

            setContentView(relativeLayout, relLayoutParam);

            //set image resource after setContentView
            imageView.setImageResource(R.drawable.launch_screen_image);

            //Use handler instead of Thread.sleep()
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    //it calls continue() after 3 seconds of delay
                    continue();
                }
            },3000);


}

And continue() method includes these:

private void continue(){

    // Check if user is signed in (non-null) and update UI accordingly.
    SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
    FirebaseUser currentUser = mAuth.getCurrentUser();
    if (currentUser != null && currentUser.getUid() != null
            && currentUser.getUid().equals(mPrefs.getString("UID", "DEFAULT"))) {
        Intent i = new Intent(mContext, SocialPage.class);
        startActivity(i);
    } else {
        Intent i = new Intent(mContext, Login.class);
        startActivity(i);
    }


}
Oğuzhan Döngül
  • 7,856
  • 4
  • 38
  • 52
0

Ya that is because of the you have not managed the dpi for different different image size for different resolutions of devices.

Please add the image as a nodpi if you have only one image for all devices resolutions at the time of copy in the drawable folder like shown in the image. So if there is no image of that particular resolution than it will automatically takes image from the nodpi of drawable.

enter image description here

Shubham Sejpal
  • 3,556
  • 2
  • 14
  • 31