Search code examples
javaandroidsplash-screen

I'm trying to add Splash Screen to my app


I have been trying to implement splash Screen to my app with help of many codes avaible in sites , but none worked for me. Each time the app is crashing after displaying splash screen for 3 secs. I don't know where i'm going wrong , please make corrections to my code to display splash screen correctly ! Thank You !

//This is my Main activity

 1. public class MainActivity extends AppCompatActivity {

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

            Button one = (Button) this.findViewById(R.id.gg);
            final MediaPlayer mp1 = MediaPlayer.create(this, R.raw.gg);
            one.setOnClickListener(new View.OnClickListener(){
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this, "Good Game", Toast.LENGTH_SHORT).show();
                    mp1.start();
                }
            });

            Button two = (Button) this.findViewById(R.id.gm);
            final MediaPlayer mp2 = MediaPlayer.create(this, R.raw.gm);
            two.setOnClickListener(new View.OnClickListener(){
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this, "Oh, They got me!", Toast.LENGTH_SHORT).show();
                    mp2.start();
                }
            });

            Button three = (Button) this.findViewById(R.id.cb);
            final MediaPlayer mp3 = MediaPlayer.create(this, R.raw.cb);
            three.setOnClickListener(new View.OnClickListener(){
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this, "Come on boy", Toast.LENGTH_SHORT).show();
                    mp3.start();
                }
            });

            Button four = (Button) this.findViewById(R.id.ns);
            final MediaPlayer mp4 = MediaPlayer.create(this, R.raw.ns);
            four.setOnClickListener(new View.OnClickListener(){
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this, "Nice shot", Toast.LENGTH_SHORT).show();
                    mp4.start();
                }
            });

            Button five = (Button) this.findViewById(R.id.wp);
            final MediaPlayer mp5 = MediaPlayer.create(this, R.raw.wp);
            five.setOnClickListener(new View.OnClickListener(){
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this, "You wanna piece of me!", Toast.LENGTH_SHORT).show();
                    mp5.start();
                }
            });

            Button six = (Button) this.findViewById(R.id.bi);
            final MediaPlayer mp6 = MediaPlayer.create(this, R.raw.bi);
            six.setOnClickListener(new View.OnClickListener(){
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this, "Bring it", Toast.LENGTH_SHORT).show();
                    mp6.start();
                }
            });

            Button seven = (Button) this.findViewById(R.id.lg);
            final MediaPlayer mp7 = MediaPlayer.create(this, R.raw.lg);
            seven.setOnClickListener(new View.OnClickListener(){
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this, "Let’s go, Yeah!", Toast.LENGTH_SHORT).show();
                    mp7.start();
                }
            });

            Button eight = (Button) this.findViewById(R.id.ru);
            final MediaPlayer mp8 = MediaPlayer.create(this, R.raw.ru);
            eight.setOnClickListener(new View.OnClickListener(){
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this, "Ready Up", Toast.LENGTH_SHORT).show();
                    mp8.start();
                }
            });

            Button nine = (Button) this.findViewById(R.id.nn);
            final MediaPlayer mp9 = MediaPlayer.create(this, R.raw.nn);
            nine.setOnClickListener(new View.OnClickListener(){
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this, "Nooooo", Toast.LENGTH_SHORT).show();
                    mp9.start();
                }
            });

            Button ten = (Button) this.findViewById(R.id.cm);
            final MediaPlayer mp10 = MediaPlayer.create(this, R.raw.cm);
            ten.setOnClickListener(new View.OnClickListener(){
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this, "Cover me", Toast.LENGTH_SHORT).show();
                    mp10.start();
                }
            });

            Button eleven = (Button) this.findViewById(R.id.hh);
            final MediaPlayer mp11 = MediaPlayer.create(this, R.raw.hh);
            eleven.setOnClickListener(new View.OnClickListener(){
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this, "Hoo-ya", Toast.LENGTH_SHORT).show();
                    mp11.start();
                }
            });

            Button twelve = (Button) this.findViewById(R.id.mo);
            final MediaPlayer mp12 = MediaPlayer.create(this, R.raw.mo);
            twelve.setOnClickListener(new View.OnClickListener(){
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this, "Move out", Toast.LENGTH_SHORT).show();
                    mp12.start();
                }
            });

            Button thirteen = (Button) this.findViewById(R.id.gs);
            final MediaPlayer mp13 = MediaPlayer.create(this, R.raw.gs);
            thirteen.setOnClickListener(new View.OnClickListener(){
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this, "Get Some", Toast.LENGTH_SHORT).show();
                    mp13.start();
                }
            });


            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);

            FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
            fab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Snackbar.make(view, "Your Messege",Snackbar.LENGTH_LONG)
                            .setAction("Action", null).show();
                }
            });
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();

            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }
            return super.onOptionsItemSelected(item);
        }
    }


//This is my activity_main.xml

 2. <android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:context="com.example.android.mmm.MainActivity">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/app_bar"
            android:layout_width="match_parent"
            android:layout_height="@dimen/app_bar_height"
            android:fitsSystemWindows="true"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/toolbar_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:contentScrim="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">

                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:layout_collapseMode="pin"
                    app:popupTheme="@style/AppTheme.PopupOverlay" />

            </android.support.design.widget.CollapsingToolbarLayout>
        </android.support.design.widget.AppBarLayout>

        <include layout="@layout/content_main" />

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/fab_margin"
            app:layout_anchor="@id/app_bar"
            app:layout_anchorGravity="bottom|end"
            app:srcCompat="@android:drawable/ic_dialog_email" />

    </android.support.design.widget.CoordinatorLayout>


//This is content_main.xml

 3. <android.support.v4.widget.NestedScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context="com.example.android.mmm.MainActivity"
        tools:showIn="@layout/activity_main">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:background="#000000">

            <Button
                android:layout_width="100dp"
                android:layout_height="48dp"
                android:text="GG"
                android:id="@+id/gg"
                android:textSize="18sp"
                android:textAllCaps="true"
                android:textStyle="bold"
                android:textColor="#000000"
                tools:ignore="HardcodedText"
                android:layout_marginTop="16dp"
                android:layout_gravity="center_horizontal"/>

            <Button
                android:id="@+id/gm"
                android:layout_width="100dp"
                android:layout_height="48dp"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="16dp"
                android:text="GM"
                android:textAllCaps="true"
                android:textColor="#000000"
                android:textSize="18sp"
                android:textStyle="bold"
                tools:ignore="HardcodedText" />

            <Button
                android:layout_width="100dp"
                android:layout_height="48dp"
                android:text="CB"
                android:id="@+id/cb"
                android:textSize="18sp"
                android:textAllCaps="true"
                android:textStyle="bold"
                android:textColor="#000000"
                tools:ignore="HardcodedText"
                android:layout_marginTop="16dp"
                android:layout_gravity="center_horizontal"/>

            <Button
                android:layout_width="100dp"
                android:layout_height="48dp"
                android:text="NS"
                android:id="@+id/ns"
                android:textSize="18sp"
                android:textAllCaps="true"
                android:textStyle="bold"
                android:textColor="#000000"
                tools:ignore="HardcodedText"
                android:layout_marginTop="16dp"
                android:layout_gravity="center_horizontal"/>

            <Button
                android:layout_width="100dp"
                android:layout_height="48dp"
                android:text="WP"
                android:id="@+id/wp"
                android:textSize="18sp"
                android:textAllCaps="true"
                android:textStyle="bold"
                android:textColor="#000000"
                tools:ignore="HardcodedText"
                android:layout_marginTop="16dp"
                android:layout_gravity="center_horizontal"/>

            <Button
                android:layout_width="100dp"
                android:layout_height="48dp"
                android:text="BI"
                android:id="@+id/bi"
                android:textSize="18sp"
                android:textAllCaps="true"
                android:textStyle="bold"
                android:textColor="#000000"
                tools:ignore="HardcodedText"
                android:layout_marginTop="16dp"
                android:layout_gravity="center_horizontal"/>

            <Button
                android:layout_width="100dp"
                android:layout_height="48dp"
                android:text="LG"
                android:id="@+id/lg"
                android:textSize="18sp"
                android:textAllCaps="true"
                android:textStyle="bold"
                android:textColor="#000000"
                tools:ignore="HardcodedText"
                android:layout_marginTop="16dp"
                android:layout_gravity="center_horizontal"/>

            <Button
                android:layout_width="100dp"
                android:layout_height="48dp"
                android:text="RU"
                android:id="@+id/ru"
                android:textSize="18sp"
                android:textAllCaps="true"
                android:textStyle="bold"
                android:textColor="#000000"
                tools:ignore="HardcodedText"
                android:layout_marginTop="16dp"
                android:layout_gravity="center_horizontal"/>

            <Button
                android:layout_width="100dp"
                android:layout_height="48dp"
                android:text="NN"
                android:id="@+id/nn"
                android:textSize="18sp"
                android:textAllCaps="true"
                android:textStyle="bold"
                android:textColor="#000000"
                tools:ignore="HardcodedText"
                android:layout_marginTop="16dp"
                android:layout_gravity="center_horizontal"/>

            <Button
                android:layout_width="100dp"
                android:layout_height="48dp"
                android:text="CM"
                android:id="@+id/cm"
                android:textSize="18sp"
                android:textAllCaps="true"
                android:textStyle="bold"
                android:textColor="#000000"
                tools:ignore="HardcodedText"
                android:layout_marginTop="16dp"
                android:layout_gravity="center_horizontal"/>

            <Button
                android:layout_width="100dp"
                android:layout_height="48dp"
                android:text="HH"
                android:id="@+id/hh"
                android:textSize="18sp"
                android:textAllCaps="true"
                android:textStyle="bold"
                android:textColor="#000000"
                tools:ignore="HardcodedText"
                android:layout_marginTop="16dp"
                android:layout_gravity="center_horizontal"/>

            <Button
                android:layout_width="100dp"
                android:layout_height="48dp"
                android:text="MO"
                android:id="@+id/mo"
                android:textSize="18sp"
                android:textAllCaps="true"
                android:textStyle="bold"
                android:textColor="#000000"
                tools:ignore="HardcodedText"
                android:layout_marginTop="16dp"
                android:layout_gravity="center_horizontal"/>

            <Button
                android:layout_width="100dp"
                android:layout_height="48dp"
                android:text="GS"
                android:id="@+id/gs"
                android:textSize="18sp"
                android:textAllCaps="true"
                android:textStyle="bold"
                android:textColor="#000000"
                tools:ignore="HardcodedText"
                android:layout_marginTop="16dp"
                android:layout_marginBottom="16dp"
                android:layout_gravity="center_horizontal"/>

        </LinearLayout>

    </android.support.v4.widget.NestedScrollView>


//This is AndroidManifest.xml

 4. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.mmm">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity
        android:name=".SplashActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
    </activity>
</application>

//This is styles.xml

 5. <resources>

        <!-- Base application theme. -->
        <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
            <!-- Customize your theme here. -->
            <item name="colorPrimary">@color/colorPrimary</item>
            <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
            <item name="colorAccent">@color/colorAccent</item>
        </style>

        <style name="AppTheme.NoActionBar">
            <item name="windowActionBar">false</item>
            <item name="windowNoTitle">true</item>
        </style>

        <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

        <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

    </resources>


//This is my SplashActivity.Java


public class SplashScreen extends Activity {

    // Splash screen timer
    private static int SPLASH_TIME_OUT = 3000;

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

        new Handler().postDelayed(new Runnable() {

            /*
             * Showing splash screen with a timer. This will be useful when you
             * want to show case your app logo / company
             */

            @Override
            public void run() {
                // This method will be executed once the timer is over
                // Start your app main activity
                Intent i = new Intent(SplashScreen.this, MainActivity.class);
                startActivity(i);

                // close this activity
                finish();
            }
        }, SPLASH_TIME_OUT);
    }
 }

 6. //This is activity_splash.xml

        <RelativeLayout 
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:src="@drawable/wwe_logo" />

        </RelativeLayout>

Solution

  • AppCompatActivity has it's own toolbar so you need to remove it by using android:theme="@style/AppTheme.NoActionBar" in manifest

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" 
            android:theme="@style/AppTheme.NoActionBar">
        </activity>
    

    Although if you are not doing much customization in your toolbar then your can also skip creating your own toolbar by removing it from XML and removing it's initialization from java code as well but by doing this , you will lose the feature of collapsing toolbar