making activity_main.xml as splash screen - android

i m new in android.
what if I want to make current view which I have made yet, of my app as splash screen for 5 seconds.
is it possible or not ?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#mipmap/background">
<ImageView
android:id="#+id/icon"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_centerInParent="true"
android:contentDescription="TODO"
android:src="#mipmap/app_icon_app_store"/>
<TextView
android:id="#+id/firstLine"
android:layout_width="300dp"
android:layout_height="60dp"
android:text="EXPRESSIONS"
android:layout_centerHorizontal="true"
android:textColor="#FFFFFF"
android:textSize="50sp"
android:layout_below="#+id/icon"
android:gravity="center" />
</RelativeLayout>

Create a splash activity and you can set above layout using setContentView(R.layout.activity_main);
public class SplashActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// this is count down timer for 5 seconds
// 5000 milliseconds value is for 5 seconds display, 1000 milliseconds value is clock tick interval
new CountDownTimer(5000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
}
#Override
public void onFinish() {
// Launch whatever activity screen you want to display when done with count down timer (i.e. 5 seconds in your case)
Intent intent = new Intent(SplashActivity.this,
YouNextActivity.class);//
startActivity(intent);
finish();
}
}.start();
}
}

you can do like this...
public class SplashScreenActivity extends Activity {
// Splash screen timer
private static int SPLASH_TIME_OUT = 5000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/****** Create Thread that will sleep for 5 seconds *************/
Thread background = new Thread() {
public void run() {
try {
// Thread will sleep for 5 seconds
sleep(SPLASH_TIME_OUT);
Intent i = new Intent(MainActivity.this, AnotherActivity.class);
startActivity(i);
//Remove activity
finish();
}
} catch (Exception e) {
}
}
};
// start thread
background.start();
}
}
Hope this will help you .

public class SplashActivity extends AppCompatActivity {
private final int SPLASH_DISPLAY_LENGTH = 5000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
/* Create an Intent that will start the Menu-Activity. */
Intent mainIntent = new Intent(SplashActivity.this, MainActivity.class);
SplashActivity.this.startActivity(mainIntent);
SplashActivity.this.finish();
}
}, SPLASH_DISPLAY_LENGTH);
}
}

You need to make this activity to sleep for some seconds
and recall start activity function after the time:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Thread splashTimer = new Thread() {
public void run() {
try {
sleep(3000);
} catch (Exception e) {
Log.e("error", e.toString());
} finally {
startMain();
}
}
};
splashTimer.start();
}
public void startMain(){
Intent iMain = new Intent(this, MainActivity.class);
startActivity(iMain);
finish();
}
after startActivity() you need to call finish() to prevent this splash activity if back button pressed.

Related

Launcher activity shows up as blank screen

I'm making a splash screen for my app and I'm just testing out putting the primary thread to sleep instead of using a timer. My code is:
package com.example.somu.activityswitcher;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class LauncherActivity extends AppCompatActivity {
public void firstActivity() {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launcher);
TextView countDown = findViewById(R.id.count);
for (int cd=3;cd>0;cd--) {
try {
Thread.sleep(1000);
countDown.setText(Integer.toString(cd));
} catch (Exception e) {
e.printStackTrace();
}
}
firstActivity();
}
}
While the MainActivity loads after 3 seconds, the splash screen (LauncherActivity) is a mere blank screen! What's going on here?!
activity_launcher.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.widget.RelativeLayout 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="com.example.somu.activityswitcher.LauncherActivity">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:scaleType="fitCenter"
android:scaleX="0.25"
android:scaleY="0.25"
app:srcCompat="#drawable/logo" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/count"
android:layout_centerHorizontal="true"
android:text="Switching in..." />
<TextView
android:id="#+id/count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="70dp"
android:text="3"
android:textColor="#android:color/black"
android:textSize="50sp" />
</android.widget.RelativeLayout>
How do I fix this?!
NOTE: I'm not bothered about any way to fix this.. I want to know why exactly this method won't work, and what is the next best way without explicitly using a timer.
try this,
may be this is useful to you.
public class LauncherActivity extends AppCompatActivity {
private TextView countDown;
int cd;
public void firstActivity() {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launch);
countDown = (TextView) findViewById(R.id.count);
try {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
for (cd = 3; cd > 0; cd--) {
countDown.setText(Integer.toString(cd));
}
firstActivity();
}
}, 3000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
if you want to add countdown than put this insted of handler,
new CountDownTimer(3000, 1000) {
public void onTick(long millisUntilFinished) {
countDown.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
countDown.setText("done!");
firstActivity();
}
}.start();
Try this instead of Thread.sleep(1000);
new CountDownTimer(3000, 1000) {
#Override
public void onTick(long l) {
countDown.setText(Integer.toString(l/1000));
}
#Override
public void onFinish() {
firstActivity();
finish();
}
}.start();
Try to use CountDownTimer to display timer instead of Thread or Handler as below :
onTick() run on UI thread so you can update UI in this method as you trying to show (3,2,1) on countDown TextView.
onFinish called when given timer is complete so you can write your code here after timer completed as you trying show another activity.
new CountDownTimer(3000, 1000) {
public void onTick(long millisUntilFinished) {
countDown.setText(""+ (millisUntilFinished / 1000));
}
public void onFinish() {
firstActivity();
}
}.start();
You are using Thread.sleep(1000); in the main thread which is freezing your UI.
Since the UI thread is frozen, in the meantime, the activity will fail to inflate and render, resulting a mere blank screen.
However, you can use Thread.sleep in a background thread but you can't update UI directly from a background Thread. You can use runOnUiThread to update UI from background thread if you place you countdown code inside a Thread
new Thread(new Runnable(){
public void run(){
//countdown code.
runOnUiThread(new Runnable(){
public void run(){
textView.setText(...
}
});
}
}).start()
try this code.
Intent intent = new Intent(this, WelcomeActivity.class);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
try{
startActivity(intent);
finish();
}
catch (Exception e){
}
}
}, 1000);
Thread.sleep(1000); freezing your UI.
try this
Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tv);
timer = new Timer();
timer.scheduleAtFixedRate(new RemindTask(), 3000, 1000); // delay*/
}
private class RemindTask extends TimerTask {
#Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
i++;
Log.e("title", "" + i);
tv.setText(i + "");
if (i == 3) {
timer.cancel();
firstActivity();
}
}
});
}
}

Android splash-screen not visible

My splash screen shows up in genymotion, but on a real android device the screen is just white for 5 seconds.
The layout is:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/launchphone"
android:scaleType="fitXY"/>
</RelativeLayout>
the code for the activity:
public class MainActivity extends Activity {
private DownloadAPITask task;
private String response;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(getResources().getConfiguration().isLayoutSizeAtLeast(Configuration.SCREENLAYOUT_SIZE_LARGE)){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}else{
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
setContentView(R.layout.activity_main);
task = new DownloadAPITask(this, null, new Runnable(){
#Override
public void run(){
onAPIDownload();
}
});
task.execute(new APIRequest("http://my-site.com/api/", "request=asd"));
}
private void onAPIDownload(){
StringBuilder strb = task.getResponse();
if(strb == null){
response = null;
}else{
response = strb.toString();
}
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
launch();
}
}, 2000);
}
private void launch(){
Intent intent = new Intent(this, StartActivity.class);
intent.putExtra(StartActivity.API, response);
startActivity(intent);
finish();
}
}
drawable/launchphone is a .png file located in res/drawable/ not in any of the dpi-dependent drawable locations, since it is just to be scaled fullscreen. I'm assuming from the lengthy start-up time on a real device that it's the splash screen but instead of rendering this image it just shows up white.
Any help would be greatly appreciated.
You have to: import android.os.Handler;
new Handler().postDelayed(new Runnable){
#Override
public void run() {
Intent intent = new Intent(getApplicationContext(), NextActivity.class);
startActivity(intent);
ThisActivity.this.finish();
},1000);
}
You can write any time duration of the splash screen. (in this case time=1000 millisecond)

Android Splash Screen Timer Does not Work Properly

In my app i set the splash screen timer to 5 sec and later on think that 5 sec is too long so i change it back to 1 sec and my splash screen doesn't seen on the screen and keep me waiting for more than 5 sec i couldn't find what is wrong so here is my Splashscreen code
public class Splash extends Activity
{
private Timer_Countdown timer_Countdown = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
timer_Countdown = new Timer_Countdown(5000, 1000);
timer_Countdown.start();
}
class Timer_Countdown extends CountDownTimer
{
public Timer_Countdown(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish() {
timer_Countdown.cancel();
Intent startIntent;
startIntent = new Intent("android.intent.action.MAINMENU");
startActivity(startIntent);
}
#Override
public void onTick(long millisUntilFinished) {
}
}
#Override
protected void onPause() {
super.onPause();
finish();
}
}
And one last thing if I change it back to 5 sec it shows up on the screen again.
Why you are using this much of code just to use splash screen. Make it simple, you can use below code.
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, NewActivity.class);
startActivity(intent);
finish();
}
}, 2000);
}
}
You can use Handler also
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
startActivity(new Intent(SplashActivity.this, YourNewActivity.class));
finish();
}
}, 3000);
or Using Timer with Timer Schedule
public class Splash extends Activity {
Timer t= new Timer();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
t.schedule(new TimerTask() {
public void run() {
Intent n= new Intent(Splash.this, YourNewActivity.class);
startActivity(n);
}
}, 3000);
}
}
Use this instead of Timer
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
//code for starting new activity
}
}, 5000);

Not calling MainActivity first, calling another activity in android

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

Splash Image for android

I made a splash image to show at the start of my activity..
The image show perfectly.But the problem is when i call this
public class SplashImageActivity extends Activity {
protected boolean active = true;
protected int splashTime = 5000; // time to display the splash screen in ms
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
// thread for displaying the SplashScreen
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) {
// do nothing
} finally {
startActivity(new Intent(SplashImageActivity.this,Myapps.class));
finish();
//startActivity(new Intent("com.splash.com.MyApps"));
//startActivity( new Intent(getApplicationContext(), Myapps.class));
}
}
};
splashTread.start();
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
active = false;
}
return true;
}
}
go for next activity the stop() does not work. And it does not go to this activity. I add all activity in manifest. The stop() shows in code like this
what's the problem?
No need to call stop() and call finish() after starting activity
finally
{
startActivity(new Intent(currentclass.this,nextActivity.class);
finish();
}
I use thread to show the Splash screen, and it works for me:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
mSplashThread = new Thread(){
#Override
public void run(){
try {
synchronized(this){
wait(4000);
}
}catch(InterruptedException ex){
}
finish();
Intent i=new Intent(getApplicationContext(),NextActivity.class);
startActivity(i);
interrupt();
}
};
mSplashThread.start();
}
Please try below code..
public class Splashscreen extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Thread t2 = new Thread() {
public void run() {
try {
sleep(2000);
startActivity( new Intent(getApplicationContext(), Exercise.class));
finish();
} catch (Exception e) {
e.printStackTrace();
}
}
};
t2.start();
}
}
No need to call stop() just call finish() after starting activity
finally {
startActivity(new Intent(currentclass.this,nextActivity.class);
finish();
}
You can also use handler an postdelayed() to make a splash screen like below
public class SplashScreenActivity extends Activity{
private Handler handler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
final Runnable runnable = new Runnable() {
#Override
public void run() {
Intent intent=new Intent(SplashScreenActivity.this, nextActivity.class);
startActivity(intent);
finish();
}
};
handler = new Handler();
handler.postDelayed(runnable, 5000);
}
}
You will show your splash screen for 5 seconds and then move to next Activity
first thing it is not onStop of Activity so looks you are calling stop function of thread which is Deprecated that's why you are getting the strike line so use other way to stop the thread of use better way to implement the splash ........
as looks you try some thing like this link

Categories

Resources