This is for a splash screen. I've followed the tutorial but it's still not working. It keeps on getting an error.
This is my code:
package id.ac.umn.finalproject;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent startApp = new Intent(MainActivity.this, PemasukanActivity.class);
new Handler().postDelayed(startActivity(startApp), 3000);
}
}
Nimantha
6,5466 gold badges32 silver badges78 bronze badges
asked Nov 18, 2021 at 2:43
Hillary Dorothea Kristianto
451 silver badge5 bronze badges
2 Answers 2
The correct way to create a Splash screen is as follows
style.xml
<style name="SplashStyle" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="android:windowBackground">@drawable/splash</item>
</style>
Splash.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/black" /> // background color
<item
android:drawable="@drawable/ic_launcher" /> // logo
android:gravity="center" />
</layer-list>
Manifest
<activity
android:name=".SplashActivity"
android:exported="true"
android:noHistory="true"
android:theme="@style/SplashStyle">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
SplashActivity.java
public class SplashActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_splash); // No need setContentView
Intent intent = new Intent(SplashActivity.this,MainActivity.class);
startActivity(intent);
}
Sign up to request clarification or add additional context in comments.
Comments
In Kotlin try this:
Handler(Looper.getMainLooper()).postDelayed({
Intent startApp = new Intent(MainActivity.this, PemasukanActivity.class);
startActivity(startApp)
}, 3000)
Nimantha
6,5466 gold badges32 silver badges78 bronze badges
Comments
Explore related questions
See similar questions with these tags.
lang-java