jobin
jobin

Reputation: 1517

Android Splash screen does not display

The splash screen in my app does not display.Only a white backgound is shown. Then it goes to the next page.I have seen other similar questions in stackoverflow but it did not help me. splash.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/splash">

</RelativeLayout>

Code:

public class Splash extends AppCompatActivity {
    @Override
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        Handler handler=new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent=new Intent(Splash.this,MainActivity.class);
                startActivity(intent);
                Splash.this.finish();
            }
        },2000);

    }
}

Manifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.jobinsabu.ohxee">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".Splash">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity"></activity>
    </application>

</manifest>

Upvotes: 4

Views: 1637

Answers (3)

Arati PAtel
Arati PAtel

Reputation: 42

Here, I tried to solve your problem

splash_screen_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/splashscreen">

</LinearLayout>

SplashScreenActivity.java

public class SplashScreenActivity extends AppCompatActivity {

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

        Thread background = new Thread() {
            public void run() {

                try {
                    // Thread will sleep for 5 seconds
                    sleep(5*1000);

                    // After 5 seconds redirect to another intent
                    Intent i=new Intent(getBaseContext(),MainActivity.class);
                    startActivity(i);

                    //Remove activity
                    finish();

                } catch (Exception e) {

                }
            }
        };

        // start thread
        background.start();
    }
}

manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.pantrykart">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".SplashScreenActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity"></activity>

    </application>

</manifest>

Upvotes: 0

Saeed Entezari
Saeed Entezari

Reputation: 3755

The problem is something i faced when creating splash screens. You start the handler in the onCreate method of the activity.

According to the android developer site:

When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it

So you are creating a handler on the main thread which is also responsible for the UI. This thread prepares your UI (which in this case seems to take a bit more than 2 seconds) and then calls the Runnable inside your handler which immediately moves to another activity thus your splash is not shown.

So what could you do? Start the timer after your view is shown!!!

This is how you can do it (Probably not the only way but good enough):

1- Set an id for the RelativeLayout in the XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/splash">
</RelativeLayout>

2- In your onCreate method do like this:

public class Splash extends AppCompatActivity {
    @Override
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        View background = findViewById(R.id.background); // or use Butterknife or DataBinding for ease of use
        final Handler handler=new Handler();
        background.post(new Runnable() {
            @Override
            public void run() {
            handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent=new Intent(Splash.this,MainActivity.class);
                startActivity(intent);
                Splash.this.finish();
            }
        },2000);
            }
        });
    }
}

This way you post a message to the view message queue and after the view is put on the screen then it registers a handler as you wish. Hope it helps.

Upvotes: 0

Noorul
Noorul

Reputation: 3444

When Big sized image you should downsample the image. change the inSampleSize variable value as per your need. increased value, will reduce the image resolution and vise versa

    public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
                                                     int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
  }

private static int calculateInSampleSize(
        BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) > reqHeight
                && (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}

call above methods like below code:

  decodeSampledBitmapFromResource(getResources(), R.drawable.splash, 400, 400);

just, add above methods in your common helper class and call it

Upvotes: 2

Related Questions