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)
Related
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();
}
}
});
}
}
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.
I am making a Splash Screen in my App. I have to set ProgressBar in my Splash Screen. But ProgressBar show green color, I have to set White color using code But It work after some time. First it show green color then it become white.
To Create ProgressBar I have use this https://github.com/rahatarmanahmed/CircularProgressView
any Help be Appreciated.
Java code :
public class SplashScreenActivity extends Activity {
// Set Duration of the Splash Screen
CircularProgressView progressView;
private static int SPLASH_TIME_OUT = 3000;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Remove the Title Bar
requestWindowFeature(Window.FEATURE_NO_TITLE);
// Get the view from splash_screen.xml
setContentView(R.layout.splash_screen);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
progressView = (CircularProgressView) findViewById(R.id.progress_view);
progressView.setColor(Color.parseColor("#FFFFFF"));
finish();
Intent myIntent = new Intent(SplashScreenActivity.this,
MainActivity.class);
startActivity(myIntent);
}
}, SPLASH_TIME_OUT);
}
}
Try this code it will work.
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
progressView = (CircularProgressView) findViewById(R.id.progress_view);
progressView.getIndeterminateDrawable().setColorFilter(getResources().getColor(R.color.accent_dark),
PorterDuff.Mode.SRC_IN);
finish();
Intent myIntent = new Intent(SplashScreenActivity.this,
MainActivity.class);
startActivity(myIntent);
}
}, SPLASH_TIME_OUT);
After changing the code alternative it will work perfectly put the ProgressBar intialization and setcolor property before the Handler code look at the code :
public class SplashScreenActivity extends Activity {
// Set Duration of the Splash Screen
CircularProgressView progressView;
private static int SPLASH_TIME_OUT = 3000;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Remove the Title Bar
requestWindowFeature(Window.FEATURE_NO_TITLE);
// Get the view from splash_screen.xml
setContentView(R.layout.splash_screen);
progressView = (CircularProgressView) findViewById(R.id.progress_view);
progressView.setColor(Color.parseColor("#FFFFFF"));
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
finish();
Intent myIntent = new Intent(SplashScreenActivity.this,
MainActivity.class);
startActivity(myIntent);
}
}, SPLASH_TIME_OUT);
}
}
I'm getting error with this code. Why huhu
123123123
Thread timer = new Thread()
{
public void run()
{
try
{
sleep(1500);
splash.setImgeResource(R.drawable.dilclogo);
sleep(1500);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
finally
{
Intent intent = new Intent(MainActivity.this, MenuScreen.class);
startActivity(intent);
}
}
};
timer.start();
This is because you can NOT access your UI/Main thread directly from any other thread. You can use below methods to access your UI thread though:
Using AsyncTask
Using runOnUiThread()
You can also read this article on threading in android to help you understand this concept better.
put splash.setImgeResource(R.drawable.dilclogo); line into runOnUiThread .
Thread timer = new Thread()
{
public void run()
{
try
{
sleep(2000);
runOnUiThread(new Runnable() {
public void run() {
splash.setImageResource(R.drawable.billboard_image);
}
});
sleep(2000);
runOnUiThread(new Runnable() {
public void run() {
splash.setImageResource(R.drawable.square);
}
});
}
catch (InterruptedException e)
{
e.printStackTrace();
}
finally
{
System.out.println("finally");
}
}
};
timer.start();
You should update ui on the ui thread. Use runonUithread.
runOnUiThread(new Runnable() {
#Override
public void run() {
// set image to imageview here
// ui should be updated on the ui thread.
// you cannot update ui from a background thread
}
});
But i would suggest you to use a handler.
public class Splash extends Activity {
//stopping splash screen starting home activity.
private static final int STOPSPLASH = 0;
//time duration in millisecond for which your splash screen should visible to
//user. here i have taken half second
private static final long SPLASHTIME = 500;
//handler for splash screen
private Handler splashHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
switch (msg.what) {
case STOPSPLASH:
//Generating and Starting new intent on splash time out
Intent intent = new Intent(Splash.this,
MainActivity.class);
startActivity(intent);
Splash.this.finish();
break;
}
super.handleMessage(msg);
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
//Generating message and sending it to splash handle
Message msg = new Message();
msg.what = STOPSPLASH;
splashHandler.sendMessageDelayed(msg, SPLASHTIME);
}
}
splash.xml
<?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:orientation="vertical" android:background="#drawable/mydrawable">
// have a imageview and set background to imageview
</RelativeLayout>
Using handlers and postdelayed
public class Splash extends Activity {
private static final int SPLASH_TIME = 2 * 1000;// 3 seconds
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
ImageView iv= (ImageView) findViewById(R.id.imageView1);
iv.setBackgroundResource(R.drawable.afor);
try {
new Handler().postDelayed(new Runnable() {
public void run() {
Intent intent = new Intent(Splash.this,
MainActivity.class);
startActivity(intent);
Splash.this.finish();
}
}, SPLASH_TIME);
} catch(Exception e)
{
e.printStacktrace();
}
}
#Override
public void onBackPressed() {
this.finish();
super.onBackPressed();
}
}
splash.xml
<?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:orientation="vertical" android:background="#ffffaa">
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
/>
</RelativeLayout>
You can not use normal threading on android system.
Give you some example on thread on android :D
---> Android Asynctask
Android Developer - Android Asynctask
You can use this for some loading effect on UI in android.
---> runOnUiThread
In your case, I suggest to use this.
You can have more detail here.
Click for detail
USEAGE::
runOnUiThread(new Runnable() {
#Override
public void run() {
// Do you ui update here
}
});
public class vv extends Activity {
int b[] = {R.drawable.a, R.drawable.m, R.drawable.b, R.drawable.j, R.drawable.er, R.drawable.chan, R.drawable.vv};
public ImageView i;
int z = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
i = (ImageView) findViewById(R.id.image);
i.setImageResource(b[0]);
Thread timer = new Thread() {
public void run() {
try {
sleep(2000);
for (z = 0; z < b.length + 2; z++) {
if (z < b.length) {
sleep(2000);
runOnUiThread(new Runnable() {
public void run() {
i.setImageResource(b[z]);
}
});
} else {
z = 0;
}
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println("finally");
}
}
};
timer.start();
}
}
Perhaps consider using
AsyncTask.execute(new Runnable {
public void run() {
splash.setImageResource(R.drawable.square);
}
});
I have an issue with my SplashScreenActivity, when I start my application on my phone it shows a white screen for about 0,5 seconds. The MainActitivy extends FragmentActivity and in the AndroidManifest I declare the SplashScreenActivity as launcher and portrait mode as screenOrientation.
The code:
public class SplashScreenActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.splashscreen);
randomSplash();
Thread splashscreen = new Thread() {
public void run() {
try {
Thread.sleep(1000);
Intent mainScreen = new Intent("com.rm.jkrm.MAINACTIVITY");
startActivity(mainScreen);
} catch (InterruptedException e) {
} finally {
finish();
}
}
};
splashscreen.start();
}
private void randomSplash(){
Random random = new Random();
int i = random.nextInt(4);
LinearLayout ln = (LinearLayout) findViewById(R.id.splashscreen);
switch(i){
case 1:
ln.setBackgroundResource(R.drawable.splash1);
break;
case 2:
ln.setBackgroundResource(R.drawable.splash2);
break;
case 3:
ln.setBackgroundResource(R.drawable.splash3);
break;
default:
ln.setBackgroundResource(R.drawable.splash0);
break;
}
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/splashscreen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
Thread splashscreen = new Thread() {
public void run() {
try {
Thread.sleep(1000);
Intent mainScreen = new Intent("com.rm.jkrm.MAINACTIVITY");
startActivity(mainScreen);
} catch (InterruptedException e) {
} finally {
finish();
}
}
};
splashscreen.start();
this is your problem UI thread to sleep not a very good idea use handler instead
and I think it may cause an exception too.
Handler h=new Handler();
h.postDelayed(new Runnable() {
public void run() {
// TODO Auto-generated method stub
startActivity(new Intent(Splash_Activity.this,Main_Activity.class));
finish();
}
}, 2000);
}
You need to run this two actions in an AsyncTask:
setContentView(R.layout.splashscreen);
randomSplash();
put the setContentView in the doInBackground-method and in the postExecute method you run randomSplash.
Change SplashActivity theme in the AndroidManifest.xml file to this.
android:theme="#android:style/Theme.Translucent.NoTitleBar"