Omar El-elamy
Omar El-elamy

Reputation: 125

Launch another activity rather than mainactivity after splash screen

could anyone tell me how to make my android application perform this order?

1) Splash Screen (SplashActivity) .. which i have done as launcher

2) Intro Slider (WelcomeActivity) .. which i don't know how to make it appear after splash.

3) Main Activity .. I want it to appear after the welcome or i'm gonna launch it from clicking the "GOT IT" button.

Thanks in advance.

Upvotes: 0

Views: 3932

Answers (2)

Kennedy
Kennedy

Reputation: 556

If i'm correct,what you are asking for is how to work with intents and handlers. First off, your splashActivity.java should look like this;

public class SplashActivity extends Activity{

//timer in miliseconds, 1000ms = 1s//
private static int SPLASH_TIME_OUT = 2000;

//create first screen showed when app is launched//
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);

    new Handler().postDelayed(new Runnable() {
        //showing splashscreen with a timer //

        @Override
        public void run() {
            //this is executed once the timer is over//

            Intent i = new Intent(SplashActivity.this,    WelcomeActivity.class);
            startActivity(i);
            finish();

        }
    },SPLASH_TIME_OUT);

}
}

then declare your menu activity and splash activity in AndroidManifest.xml for example;

<activity
        android:name=".SplashActivity"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

Then for how to open you main activity after your welcome activity, just copy and paste the code for SplashActivity.java into your WelcomeActivity,making necessary changes, but for how to open using button, see example code below first off your button show be designed already in your activity_welcome.xml e.g.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_welcome"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.domainname.yourappname.WelcomeActivity"
android:background="@drawable/splash"

<Button
        android:text="@string/got it"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button"
        style="@style/Widget.AppCompat.Button.Borderless"
        android:textAlignment="center"
        android:textSize="30sp"
        android:layout_marginTop="41dp"
    android:textColorHighlight="@android:color/transparent"
    android:textColorHint="@android:color/transparent"
    android:layout_below="@+id/textView3"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

</RelativeLayout>

In your WelcomeActivity.java

public class WelcomeActivity extends Activity {
Button button;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_welcome);
    addListenerOnButton();

}
public void addListenerOnButton() {

    final Context context = this;

    button = (Button) findViewById(R.id.button);

    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent(context, MainActivity.class);
            startActivity(intent);
            finish();
            Toast.makeText(context, "MainActivity Opened.", Toast.LENGTH_SHORT).show();

        }

    });
}
}

NB: I don't know what program you are writing or how you have designed it so far, this is just an example and your might need to make adjustments for your actual code to run properly

Upvotes: 1

Steve C.
Steve C.

Reputation: 1353

To answer your question of "HOW" to do that;

First, make sure all activities are declared in your Manifest like so:

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

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

Then declare this in SplashActivity at the finish of the splash timer:

    //If you're using a "Timer" to count down splash screen
new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                Intent intent = new Intent(SplashActivity.this, WelcomeActivity.class);
                startActivity(intent);

            }
        }, 2000);

In your WelcomeActivity wherever you call the end of the activity:

    Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
    startActivity(intent);

You can find more information about starting another activity using an intent here including how to add extra data for the next activity to receive. Hope that helps.

Upvotes: 0

Related Questions