How can I use Intent in postDelayed using Android? - java

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);
}
}

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);
}

In Kotlin try this:
Handler(Looper.getMainLooper()).postDelayed({
Intent startApp = new Intent(MainActivity.this, PemasukanActivity.class);
startActivity(startApp)
}, 3000)

Related

screen not chnaging from splash screen to main activity? Java android studio

I am new to Java android development and i am trying to add splash screen before main activity so i created a splash screen class . and in my android manifest i did the following changes:
<activity
android:name="com.example.mayoenglishconversation.SplashScreen"
android:theme="#style/SplashScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.mayoenglishconversation.MainActivity"
android:windowSoftInputMode="stateHidden|adjustResize|adjustPan"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
</activity>
and here is the code of my SplashScreen.java file:
package com.example.mayoenglishconversation;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.PersistableBundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class SplashScreen extends AppCompatActivity {
#Override
public void onCreate(#Nullable Bundle savedInstanceState, #Nullable PersistableBundle
persistentState) {
super.onCreate(savedInstanceState, persistentState);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent i=new Intent(SplashScreen.this,MainActivity.class);
startActivity(i);
}
},3000);
}
}
but issue is i am stuck with the splash screen . what i am doing wrong?
Try to change the override method protected void onCreate instead of public void onCreate. I faced this issue previously, It worked.
try to use following override method;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

How to make the actionbar disappear in Android Studio?

I need to make the action bar disappear from the screen, I've tried everything I've been able to find online, but nothing seems to work. I've tried creating my own style, which doesn't do anything, getActionbar().hide(), which gives me an error and doesn't compile, and requestWindowFeature(Window.FEATURE_NO_TITLE);, which also doesn't do anything. I really need to get rid of that action bar. I'm using a sensorLandscape orientation, but I've already tried changing it and it doesn't change anything.
The manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tanques">
<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/Theme.AppCompat.Light.NoActionBar.FullScreen>
<activity android:name=".MainActivity" android:screenOrientation="sensorLandscape" android:theme="#style/Theme.AppCompat.Light.NoActionBar.FullScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
My own style:
<resources>
<!-- Base application theme. -->
<style name="Theme.AppCompat.Light.NoActionBar.FullScreen" parent="#style/Theme.AppCompat.Light.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">#null</item>
</style>
</resources>
MainActivity.java
package com.example.tanques;
import android.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements JoystickView.JoystickListener, JoystickHorizontal.JoystickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button ShootLeft= findViewById(R.id.ShootLeft);
Button ShootRight= findViewById(R.id.ShootRight);
ShootLeft.setOnClickListener(new View.OnClickListener() { #Override public void onClick(View v){
Log.d("Button","ShootLeft");
}});
ShootRight.setOnClickListener(new View.OnClickListener() { #Override public void onClick(View v){
Log.d("Button","ShootRight");
}});
}
#Override
public void onJoystickMoved(float Percent, int source) {
switch(source){
case R.id.JoystickLeft:
Log.d("Joystick", "JoystickLeft" + " Percentage: " +Math.round(Percent));
break;
case R.id.JoystickRight:
Log.d("Joystick", "JoystickRight" + " Percentage: " +Math.round(Percent));
break;
case R.id.JoystickTurret:
Log.d("Joystick", "JoystickTurret" + " Percentage: " +Math.round(Percent));
break;
}
}
}
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tanques">
<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">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="#style/Theme.AppCompat.Light.NoActionBar"/>
</resources>
Put this inside your onCreate() in MainActivity.java before setContentView()
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//add this two lines to your code to hide the system bar
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN , WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
//this hides the navigation bar at the bottom of your device
hideNavigationBar();
final Button ShootLeft= findViewById(R.id.ShootLeft);
Button ShootRight= findViewById(R.id.ShootRight);
ShootLeft.setOnClickListener(new View.OnClickListener() { #Override public void onClick(View v){
Log.d("Button","ShootLeft");
}});
ShootRight.setOnClickListener(new View.OnClickListener() { #Override public void onClick(View v){
Log.d("Button","ShootRight");
}});
}
create hideNavigationBar() method and call it in the onCreate()
private void hideNavigationBar(){
getWindow().getDecorView()
.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_FULLSCREEN |
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY |
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
);
}

How to go back from new activity to main navigation drawer activity

First I create app of Navigation Drawer Activity and travel from fragment to new activity, Now I want to set Home Button on New activity which navigates to main activity of navigation drawer.
but when i run it from app it gives me this when Home button is click
but i want to go direct on MainActivity
Here
NewActivity.java
package com.shubham.navi_demo;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
public class NewActivity extends FragmentActivity {
private static Button Button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new);
onclickbutton();
}
public void onclickbutton(){
Button = (Button)findViewById(R.id.button);
Button.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("android.intent.action.MAIN");
startActivity(intent);
}
}
);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".NewActivity">
<intent-filter>
<action android:name="com.shubham.navi_demo.NewActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
You don't have to start activity with Intent. Just call finish() in NewActivity, it will show you the Main Navigation drawer activity.
hope this helps.
Intent intent = new Intent(this,MainActivity.class);
Change
Intent intent = new Intent("android.intent.action.MAIN");
to
Intent intent = new Intent(this,MainActivity.class);
or call finish(); to finish() current Activity
Simple solution that you can provide users to navigate back to home screen is explained here.
developer.android.com/training/implementing-navigation/ancestral.html
Add this line to your 'AndroidManifest' under relevant activity declaration.
android:parentActivityName="com.example.your_main_activity"

Android Studio - Button click not switching to next activity

Added an OnClickListener to my button. I would like it to switch to 'Step1' activity when I click the button. When I debugged it, I clicked on the button but nothing happened. It stayed on 'MyActivity' activity. Here's my main activity:
package apk.olley102.roothelp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MyActivity extends Activity {
Button step1btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
step1btn = (Button) findViewById(R.id.step1btn);
step1btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent openStep = new Intent("apk.olley102.roothelp.Step1");
startActivity(openStep);
}
});
}
}
I added the button in the activity_main.xml and associated the layout to the activity. Also added MainActivity.java and Step1.java to the AndroidManifest.xml
Any help would be appreciated!
Usually to launch an activity you specify the current one instance and the one you want to open. In your case it would be something like this:
Intent openStep = new Intent(MyActivity.this, Step1.class);
startActivity(openStep);
If you still want to launch it using an action string, check that your activity declares the intent filter in AndroidManifest.xml:
<activity android:name=".Step1">
<intent-filter>
<action android:name="apk.olley102.roothelp.Step1" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Also note that the action string does not need to match your activity name: you could name it apk.olley102.roothelp.GOTOSTEP1 for example and use the same string when you create the intent.
I have finally got my button working. I had to use '.DEFAULT' in the category of my second activity.
<activity
android:name=".Step1" android:label="#string/app_name"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation">
<intent-filter>
<action android:name="apk.olley102.instructroot.Step1" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Also I had to specify the activity '.this' in my intent.
step1btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent openStep = new Intent(MyActivity.this, Step1.class);
startActivity(openStep);
}
});
I debugged my app and now the button launches Step1 on click. Thanks for all the help! :)

The application crashes after the splash screen (When the menu screen supposed to appear)

I made a game with the menu screen and a splash screen. I followed the steps on YouTube tutorials. The application crashes every time right after the splash screen ends (when the menu screen supposed to appear).
Here is my code, there's no error. Where could the problem be?
Splash screen:
package com.group5.littlered;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
public class MyMain extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//Remove notification bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//set content view AFTER ABOVE sequence (to avoid crash)
this.setContentView(R.layout.main|R.layout.splash);
setContentView(R.layout.splash);
Thread timer = new Thread(){
public void run(){
try{
sleep(5000);
} catch (InterruptedException e){
e.printStackTrace();
}finally{
Intent openStartingPoint = new Intent("com.group5.littlered.STARTINGPOINT");
startActivity(openStartingPoint);
}
}
};
timer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}
Menu Screen:
package com.group5.littlered;
import android.app.Activity;
import android.os.Bundle;
public class MyMenu extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}}
andriodManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.group5.littlered"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MyMain"
android:label="#string/app_name"
android:screenOrientation="landscape"
android:configChanges="keyboard|keyboardHidden|orientation" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MyMenu"
android:label="#string/app_name"
android:screenOrientation="landscape"
android:configChanges="keyboard|keyboardHidden|orientation" >
<intent-filter>
<action android:name="com.group5.littlered.SPLASH" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
Try this..
Change this..
Intent openStartingPoint = new Intent("com.group5.littlered.STARTINGPOINT");
startActivity(openStartingPoint);
to
Intent openStartingPoint = new Intent(MyMain.this,MyMenu.class);
startActivity(openStartingPoint);

Categories

Resources