iOSBee
iOSBee

Reputation: 229

Android Setting Up Splash Screen(Activity) Like Iphone Part1

I have three images with me and i want them to appear on first layout xml like a splash view so that they can be viewed only once i.e that activity will be called only once when app get's installed or if app get's a new update otherwise app should always start from the Second activity, i don't know how should i begin with this :

enter image description here

Can any one tell me any idea how this can be done.

To show splash for only once.

Next part of this question is here

Coding will be much appreciated.

Upvotes: 1

Views: 904

Answers (3)

Nirav Ranpara
Nirav Ranpara

Reputation: 13785

Save a flag in the Preferences when you start up the application, after you've done the welcome screen stuff. Check for this flag before you show the welcome screen. If the flag is present (in other words, if it's not the first time), don't show it.

In your activity:

SharedPreferences mPrefs;
final String welcomeScreenShownPref = "welcomeScreenShown";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mPrefs = PreferenceManager.getDefaultSharedPreferences(this);

    // second argument is the default to use if the preference can't be found
    Boolean welcomeScreenShown = mPrefs.getBoolean(welcomeScreenShownPref, false);

    if (!welcomeScreenShown) {
        // here you can launch another activity if you like
        // the code below will display a popup

        String whatsNewTitle = getResources().getString(R.string.whatsNewTitle);
        String whatsNewText = getResources().getString(R.string.whatsNewText);
        new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle(whatsNewTitle).setMessage(whatsNewText).setPositiveButton(
                R.string.ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).show();
        SharedPreferences.Editor editor = mPrefs.edit();
        editor.putBoolean(welcomeScreenShownPref, true);
        editor.commit(); // Very important to save the preference
    }

}

Upvotes: 4

stinepike
stinepike

Reputation: 54742

to do this you have to detect the first launch of your application. To do so you can store a boolean value as @Nirav suggested.

And for the splash screen, You can consider using Fragments and ViewPager to create an activity which will only be shown for the first time

Upvotes: 0

Shiv
Shiv

Reputation: 4567

Try this :

     public class MainActivity extends Activity {

private Thread mSplashThread;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    try {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.splash);

        final MainActivity sPlashScreen = this;

        mSplashThread = new Thread() {
            @Override
            public void run() {
                try {
                    synchronized (this) {
                        wait(4000);
                    }
                } catch (InterruptedException ex) {
                }

                finish();
                Intent intent = new Intent();
                intent.setClass(sPlashScreen, StartNewActivity.class);// <-- Activity you want to start after Splash
                startActivity(intent);
            }
        };

        mSplashThread.start();
    } catch (Exception e) {
    }
}

@Override
public boolean onTouchEvent(MotionEvent evt) {
    try {
        if (evt.getAction() == MotionEvent.ACTION_DOWN) {
            synchronized (mSplashThread) {
                mSplashThread.notifyAll();
            }
        }
    } catch (Exception e) {
    }
    return true;

}

}

you put an Image in splash.xml to show

Upvotes: 0

Related Questions