Skip to main content

How to create a one time Splash Screen in Android Studio. Android tutorials for beginners.




SplashScreen.java
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class SplashScreen extends AppCompatActivity {

    SharedPreferences sharedPreferences;
    Boolean firstTime;

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

        sharedPreferences = getSharedPreferences("MyPrefs",MODE_PRIVATE);

        firstTime = sharedPreferences.getBoolean("firstTime",true);

        if (firstTime){
            new Handler().postDelayed(new Runnable() {
                @Override                
                public void run() {

                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    firstTime = false;
                    editor.putBoolean("firstTime",firstTime);
                    editor.apply();

                    Intent i  = new Intent(SplashScreen.this,MainActivity.class);
                    startActivity(i);
                    finish();
                }
            }, 5000);
        }
        else {
            Intent i  = new Intent(SplashScreen.this,MainActivity.class);
            startActivity(i);
            finish();
        }


    }
}

MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

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

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.abhishekpanwar.splashscreentutorial">
  <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=".MainActivity">
        </activity>
    <activity android:name=".SplashScreen">
      <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
      </intent-filter>
    </activity>
  </application>
</manifest>

Comments

  1. FeedBurner
    The feed does not have subscriptions by email enabled

    ReplyDelete
  2. i can't fetch any data without making a class for each url and activity WTF?

    ReplyDelete
  3. The Borgata Hotel Casino & Spa Announces a $200 Million
    The Borgata 광양 출장마사지 Hotel Casino & Spa in 군산 출장안마 Atlantic City will open daily 24 hours, 충주 출장샵 with casino and bingo 전라북도 출장안마 sections available daily 24 hours a 제주 출장샵 week.

    ReplyDelete

Post a Comment

Popular posts from this blog

How to: JSON Data Fetching and Parsing Android Studio Tutorial in Java

Here is the Summary! We will only be using one activity named MainActivity.java  in this whole project.  This activity will be doing the data fetching as well as displaying that JSON data. For this, we will be declaring the MainActivity.java in the AndroidManifest.xml file. Look at the AndroidManifest.xml file below. AndroidManifest.xml . <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.abhishekpanwar.receivedatajson"> <uses-permission android:name="android.permission.INTERNET"/> <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=".MainActivity">