I want to write a code that uses the splash screen .I have written this so far, but Can anyone tell me what is the missing here!?
here is my main code:
package com.example.splash;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
and here is my splash activity code:
package com.example.splash;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class splashscreen extends Activity {
protected int _splashTime = 5000;
private Thread splashTread;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splashh);
final splashscreen sPlashScreen = this;
splashTread = new Thread() {
#Override
public void run() {
try {
synchronized(this){
wait(_splashTime);
}
} catch(InterruptedException e) {}
finally {
finish();
Intent i = new Intent();
i.setClass(sPlashScreen,MainActivity.class);
startActivity(i);
//stop();
}
}
};
splashTread.start();
}
The problem is I do not know how to tell my main to go splash activity , if I use an intent I would stuck on infinite loop.
You can simply use this:
Handler handler=new Handler();
handler.postDelayed(new Runnable()
{
#Override
public void run()
{
Intent intent = new Intent(SplashViewController.this,HomeViewController.class);
startActivity(intent);
SplashViewController.this.finish();
}
}, 3000);
try this instead :
public class splashscreen extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_splash);
Thread t = new Thread(Splash_Runnable);
t.start();
}
Runnable Splash_Runnable = new Runnable() {
#Override
public void run() {
try {
Thread.sleep(3000);
startActivity(new Intent(splashscreen.this,
MainActivity.class));
splashscreen.this.finish();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
}
The problem (i guess) is that your app is starting with your MainActivity as your launcher Activity. Make splashscreen your laucher Activity in your Application Manifest XML and you will avoid the infinite loop.
Try this code:
private boolean _active = true;
private int _splashTime = 5000;
Thread splashTread = new Thread()
{
#Override
public void run()
{
try
{
int waited = 0;
while(_active && (waited < _splashTime))
{
sleep(100);
if(_active)
{
waited += 100;
}
}
}
catch(InterruptedException e)
{
e.printStackTrace();
}
finally
{
Intent intent = new Intent(SplashScreenActivity.this,MainActivity.class);
startActivity(intent);
finish();
}
};
splashTread.start();
in AndroidManifest mention your activity as Main Activity.
Try to change your SplashActivity code from here.
Splash and main activity error
Also make your splashactivtiy as your launcher activity and then redirect to the MainActivity from the SplashScreen
<activity
android:name="com.app.wablogic.SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
Full detail of creating a splash Activity
Create a layout for Splash
splash.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/splash" >
</LinearLayout>
Now create a class Under package . Name it Splash
public class Splash extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
Intent openMainActivity = new Intent(Splash.this, MainActivity.class);
startActivity(openMainActivity);
finish();
}
}, 5000); //it will call the MainActivity after 5 seconds
}
Go to manifest and add the Activity to it.
and cut the intent-filter where main and Launcher are child and paste it in Splash Activity like
<activity
android:name="com.example.yourpackage.Splash"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
You can create a Thread for doing something or just sleep for a few seconds to do, such as
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Thread background = new Thread() {
public void run() {
try {
// Thread will sleep for 3 seconds
sleep(3*1000);
// After 3 seconds redirect to another intent
Intent i=new Intent(getBaseContext(),MenuActivity.class);
startActivity(i);
//Remove activity
finish();
} catch (Exception e) {
}
}
};
background.start();
}
#Override
protected void onDestroy() {
super.onDestroy();
}
You can get more example here.
Related
Here is my activity Mod_1_1_b. on startup, this activity takes user to Mod_1_1_c after 3 sec,
Mod_1_1_b has a button for going to some other activity (main) but whenever i press that button, it first takes me to main activity and then immediately launches Mod_1_1_c. i have tried calling finish(); but not working, looks like the intent already launches in background.
package com.example.abc;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
public class Mod_1_1_b extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mod_1_1_b);
final MediaPlayer mp1 = MediaPlayer
.create(getBaseContext(), R.raw.yolo); // -<
mp1.start();
Handler mHandler = new Handler();
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
Intent startActivity = new Intent(Mod_1_1_b.this,
Mod_1_1_c.class);// -<
startActivity(startActivity);
overridePendingTransition(R.anim.slide_right_to_left_1,
R.anim.slide_right_to_left_2);
finish();
}
}, 3000L);
}
public void back(View view) {
Intent intent = new Intent(this, Mod_1_1AtoZ.class);
startActivity(intent);
overridePendingTransition(R.anim.slide_left_to_right_1,
R.anim.slide_left_to_right_2);
finish();
}
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
}
}
xml
<RelativeLayout
android:layout_width="290dip"
android:layout_height="290dip"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#drawable/b_tile" >
</RelativeLayout>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:onClick="back"
android:text="Back" />
</RelativeLayout>
it first takes me to main activity and then immediately launches Mod_1_1_c
That is because you set your handler to execute in 3 seconds thus after you press that button it will change the activity but it will still execute your handler and change to activity Mod_1_1_c
solution:
In you button back you need to cancel your handler to stop it executing after you changed activity.
sample:
public void back(View view) {
timeoutHandler.removeCallbacks(your_runnable);
Intent intent = new Intent(this, Mod_1_1AtoZ.class);
startActivity(intent);
overridePendingTransition(R.anim.slide_left_to_right_1,
R.anim.slide_left_to_right_2);
finish();
}
Make sure that your Runnable and Handler has global instance of it.
EDIT:
public class Mod_1_1_b extends Activity {
Handler mHandler; //global instance
Runnable your_runnable; //global instance
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mod_1_1_b);
final MediaPlayer mp1 = MediaPlayer
.create(getBaseContext(), R.raw.yolo); // -<
mp1.start();
mHandler = new Handler();
your_runnable = new Runnable() {
#Override
public void run() {
Intent startActivity = new Intent(Mod_1_1_b.this,
Mod_1_1_c.class);// -<
startActivity(startActivity);
overridePendingTransition(R.anim.slide_right_to_left_1,
R.anim.slide_right_to_left_2);
finish();
}
};
mHandler.postDelayed(your_runnable , 3000L);
}
public void back(View view) {
mHandler.removeCallbacks(your_runnable);
Intent intent = new Intent(this, Mod_1_1AtoZ.class);
startActivity(intent);
overridePendingTransition(R.anim.slide_left_to_right_1,
R.anim.slide_left_to_right_2);
finish();
}
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
}
}
currently I'm programming an Android application and I'm using timer for my splash. instead of using timer I want to use handle but I can't integrate that to my code:
package com.tesbih;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class Splash extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
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.tesbih.TESBIHMAINACTIVITY");
startActivity(openStartingPoint);
}
}
};
timer.start();
}
}
public class Splash extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
final Intent openStartingPoint = new Intent(this, TESBIHMAINACTIVITY);
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
startActivity(openStartingPoint);
finish();
}
}, 5000);
}
}
This code is homepage of my app. For this app i want to create splash .
main app code:-
package com.Wase.edittext;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.format.Time;
import android.widget.EditText;
public class Splash extends Activity {
Timer timer = new Timer();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_logo);
Time.schedule(new TimerTask() {
public void run() {
Intent intent = new Intent(Splash.this, MyAndroidAppActivity.class);
startActivity(intent);
finish();
}
}, 5000);
}
}
This my code for splash.. please see the mistake amd tell me ..
First of all create one new XML file and use whatever design you want and after that create activity (java) file and use below code and change your screen and variable name as per your requirement.
public class Splash extends Activity {
Timer timer = new Timer();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
timer.schedule(new TimerTask() {
public void run() {
Intent intent = new Intent(Splash.this, MyAndroidAppActivity.class);
startActivity(intent);
finish();
}
}, 5000);
}
}
and this is your splash.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/splash"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
tools:context=".IMyCompany" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="153dp"
android:src="#drawable/logo" />
</RelativeLayout>
it will move to new activity after 5 seconds.
public class SplashScreen extends Activity {
public SplashScreen instance;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.xml_splashscreen);
if (instance != null) {
instance.finish();
}
instance = this;
Thread threadMenu = new Thread(new Runnable() {
public void run() {
try {
// sleep 3 second and go next page
Thread.sleep(400);
// Check Vefication Success then Load HomeScreen Check
Intent splash = new Intent(getBaseContext(),
MyAndroidAppActivity .class);
startActivity(splash);
finish();
overridePendingTransition(R.anim.push_in_from_left,
R.anim.push_out_to_right);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
threadMenu.start();
}
}
in manifest file :
<activity
android:name="com.demo.SplashScreen"
android:label="#string/app_name"
android:screenOrientation="sensorPortait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".com.demo.MyAndroidAppActivity"
android:configChanges="keyboardHidden"
android:screenOrientation="sensorPortait"
android:windowSoftInputMode="stateHidden" />
use this code
====
public class SplashScreenActivity extends Activity {
final int splashTimeOut = 3000;
ImageView imgSplash;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen);
imgSplash = (ImageView) findViewById(R.id.imgsplash);
setContentBasedOnLayout();
// TODO Auto-generated method stub
Thread splashThread = new Thread() {
int wait = 0;
#Override
public void run() {
try {
super.run();
while (wait < splashTimeOut) {
sleep(100);
wait += 100;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//If You Want To Do Something Write Here Your Code
}
}
};
splashThread.start();
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
return true;
}
return super.onKeyDown(keyCode, event);
}
}
I am making an application in which I have to show a welcome page for only 5 seconds and then redirect to another activity. However, I am not getting what I want because I am getting another activity first and MainActivity after click on the back button of the emulator which is not right. So could somebody please help me?
I am placing some code for your reference specially welcome activity and manifest code:
public class Welcome extends Activity
{
private Runnable runnable;
private static final String TAG="Welcome";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
runnable=new Runnable() {
public void run() {
try
{
Log.v(TAG,"Going to sleep...");
Thread.sleep(3000);
Log.v(TAG,"Going to wake up...");
}
catch(Exception e)
{
e.printStackTrace();
}
}
};
try
{
Thread t=new Thread(null,runnable);
t.start();
Intent i=new Intent(this,TabSample.class);
startActivity(i);
}
catch(Exception e)
{
}
}
}
AndroidManifest.xml
<activity
android:name=".Welcome"
android:label="Restaurant Application" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".TabSample"></
main.xml
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:src="#drawable/home" />
use intent like this
Intent i=new Intent(Welcome.this,TabSample.class);
startActivity(i);
You should try this.
public class Welcome extends Activity
{
private Runnable runnable;
private static final String TAG="Welcome";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
runnable=new Runnable() {
public void run() {
try
{
Log.v(TAG,"Going to sleep...");
Thread.sleep(3000);
Log.v(TAG,"Going to wake up...");
runOnUiThread(new Runnable() .
{
#Override
public void run()
{
Intent i=new Intent(this,TabSample.class);
startActivity(i);
}
});
}
catch(Exception e)
{
e.printStackTrace();
}
}
};
try
{
Thread t=new Thread(null,runnable);
t.start();
}
catch(Exception e)
{
}
}
}
Because if you start Activity with parallel to threat then it will go forward without any delay and thread will run in background. so put you startactivity code in runonuithread. like above.
it may work for you.
Try this
Thread thread = new Thread() {
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Intent intent = new Intent(Welcome.this, TabSample.class);
startActivity(intent);
finish();
}
};
thread.start();
you can use timertask for showing the Welcome screen for some time
So here is how you go about it.
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
public class Welcome extends Activity {
private long splashDelay = 5000; //5 seconds
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TimerTask task = new TimerTask()
{
#Override
public void run() {
finish();
Intent mainIntent = new Intent().setClass(Welcome.this, TabSample.class);
startActivity(mainIntent);
}
};
Timer timer = new Timer();
timer.schedule(task, splashDelay);
}
}
if you only need to show the Welcome Activity for 5 seconds modify your thread. Remove and insert
Intent i=new Intent(this,TabSample.class);
startActivity(i);
inside runnable.
You Can only use a handler to do that . code will be like this
Runnable GotoMenu;
Handler CallMenu = new Handler();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.splash);
GotoMenu = new Runnable(){
public void run(){
CallMenu.removeCallbacks(GotoMenu);
Intent i = new Intent(getApplicationContext(), TabSample.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();
}
};
CallMenu.postDelayed(GotoMenu, 2000);
}
I would like to implement a SplashScreen in my app. I found the best and easiest way is to launch an activity that shows a layout with an image view at the launch of the app and then adding android:noHistory="true" attribute to the manifest.
Now, how do I set the splashscreen activity to launch the MainActivity class after a certain amount of time? Lets say 2 seconds?
This is my splashscreen activity
public class SplashActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen);
}
}
use
handler.postDelayed(runnable, delayinmilliseconds(2000 in your case));
final Runnable runnable = new Runnable()
{
public void run()
{
//start the new activity here.
}
};
Here is a complete sample.
package com.test.splash;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
public class splash extends Activity {
private static final int STOPSPLASH = 0;
//time in milliseconds
private static final long SPLASHTIME = 3000;a
private ImageView splash;
//handler for splash screen
private Handler splashHandler = new Handler() {
/* (non-Javadoc)
* #see android.os.Handler#handleMessage(android.os.Message)
*/
#Override
public void handleMessage(Message msg) {
switch (msg.what) {
case STOPSPLASH:
//remove SplashScreen from view
splash.setVisibility(View.GONE);
break;
}
super.handleMessage(msg);
}
};
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
splash = (ImageView) findViewById(R.id.splashscreen);
Message msg = new Message();
msg.what = STOPSPLASH;
splashHandler.sendMessageDelayed(msg, SPLASHTIME);
}
}
public class TrackMyMoneyActivity extends Activity {
//member fields
private ProgressBar pbar = null;
private TextView counter_txt = null;
Thread splash_thread = null;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
pbar = (ProgressBar) findViewById(R.id.splashpbar);
counter_txt = (TextView) findViewById(R.id.countertxt);
//define thread
splash_thread = new Thread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
int i = 0;
for( i=0;i<100;i++){
pbar.setProgress(i);
// counter_txt.setText(i+" %");
try {
splash_thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(i==100){
Intent intent = new Intent(getBaseContext(), LoginApp.class);
startActivity(intent);
}
}
});
splash_thread.start();
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
}
I hope it will be solved your solution.
You can also use java.util.Timer in this way:
new Timer().schedule(new TimerTask(){
public void run() {
startActivity(new Intent(SplashActivity.this, MainActivity.class));
}
}, 2000 /*amount of time in milliseconds before execution*/ );
public class Splashscreen extends Activity
{
private static final int SPLASH_TIME = 10 * 1000;// 3 seconds
Button logo;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// requestWindowFeature(Window.FEATURE_NO_TITLE);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.splashscreen);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
try {
new Handler().postDelayed(new Runnable() {
public void run() {
Intent intent = new Intent(Splashscreen.this,MainActivity.class);
startActivity(intent);
Splashscreen.this.finish();
//overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
}
}, SPLASH_TIME);
new Handler().postDelayed(new Runnable() {
public void run() {
}
}, SPLASH_TIME);
} catch(Exception e){}
// METHOD 1
/****** Create Thread that will sleep for 5 seconds *************/
Thread background = new Thread() {
public void run() {
try {
// Thread will sleep for 5 seconds
sleep(50*1000);
// After 5 seconds redirect to another intent
Intent i=new Intent(getBaseContext(),MainActivity.class);
startActivity(i);
//Remove activity
finish();
} catch (Exception e) {
}
}
};
background.start();
}