Giovanni
Giovanni

Reputation: 17

Splash Screen - how to?

I have a problem with the splash screen because once the app is started I see the MainActivity first and after the splash screen, what is the errors?

public class MainActivity extends AppCompatActivity {

int time = 3000;

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

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            Intent i = new Intent(MainActivity.this, Splash_screen.class);
            startActivity(i);
            finish();
        }
    }, time);

XML code of MainActivity.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
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"
tools:context=".MainActivity">


<ImageButton
    android:id="@+id/b10"
    android:layout_width="166dp"
    android:layout_height="166dp"
    android:layout_marginTop="450dp"
    android:layout_marginEnd="30dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<ImageButton
    android:id="@+id/b9"
    android:layout_width="166dp"
    android:layout_height="166dp"
    android:layout_marginStart="30dp"
    android:layout_marginTop="450dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<ImageButton
    android:id="@+id/b8"
    android:layout_width="166dp"
    android:layout_height="166dp"
    android:layout_marginTop="250dp"
    android:layout_marginEnd="30dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<ImageButton
    android:id="@+id/b7"
    android:layout_width="166dp"
    android:layout_height="166dp"
    android:layout_marginStart="30dp"
    android:layout_marginTop="250dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<ImageButton
    android:id="@+id/b"
    android:layout_width="166dp"
    android:layout_height="166dp"
    android:layout_marginTop="50dp"
    android:layout_marginEnd="30dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<ImageButton
    android:id="@+id/b1"
    android:layout_width="166dp"
    android:layout_height="166dp"
    android:layout_marginStart="30dp"
    android:layout_marginTop="50dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

XML code of Splash_screen.

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"
tools:context=".Splash_screen">

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="427dp"
    android:layout_height="588dp"
    app:srcCompat="@drawable/beb"
    tools:layout_editor_absoluteX="-8dp"
    tools:layout_editor_absoluteY="16dp" />

These are the xml codes of the MainActivity file and the Splash_screen. Thanks for your help, i am new to android studio and also Stack Overflow

Upvotes: 0

Views: 67

Answers (2)

Luca Murra
Luca Murra

Reputation: 1892

You're calling the splash screen from your MainActivity, so it's normal that your activity is created and then the splash is visualized; you should follow @moon answer, but it's a bit wrong:

You have to edit your AndroidManifest.xml to make the Splash_creen to be called first (make sure that Splash_screen is an Activity):

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="your.pack.age"
    android:installLocation="preferExternal" >

    <!-- Your permissions -->

    <application
        android:allowBackup="true"
        android:icon="@drawable/app_image" <!-- You'll have to modify these 3 lines -->
        android:label="@string/app_name"
        android:roundIcon="@drawable/app_round_image"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

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

        <activity
            android:name="com.pitibi.boulas.MainActivity"/>

    </application>
</manifest>

Then you call MainActivity from the OnCreate of Splash_screen:

public class Splash_screen extends AppCompatActivity {

int time = 3000;

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

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            Intent i = new Intent(Splash_screen.this, MainActivity.class);
            startActivity(i);
            finish();
        }
    }, time);

Upvotes: 1

moon
moon

Reputation: 333

First go to menifest and chnage the intent-filter like that

<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>

here is you need to launcher to start splash activity first than do in splash.java

int time = 3000;
new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        Intent i = new Intent(Splash_screen.this, MainActivity.class);
        startActivity(i);
        finish();
    }
}, time);

Upvotes: 0

Related Questions