I'm just trying to test some stuff with a splash screen. The strangest thing happens when I run the app though. I can see my Log messages in the LogCat, but the activity itself won't show up. Once the loop finishes, it starts the next activity, which does in fact show up. If I comment out the UIThread, it will show up though. I know I'm doing something simple wrong, but I'm not sure what it is. Ideas?
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#000000">
<ImageView
android:id="#+id/logoIV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="50dp"
android:paddingTop="50dp"
android:src="#drawable/logoa"
/>
Java:
public class Splash extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.splash);
final ImageView logo = (ImageView) findViewById(R.id.logoIV);
final int[] anim = new int[6];
anim[0]=R.drawable.logoa;
anim[1]=R.drawable.logob;
anim[2]=R.drawable.logoc;
anim[3]=R.drawable.logod;
anim[4]=R.drawable.logoe;
anim[5]=R.drawable.logof;
runOnUiThread(new Runnable() {
int img = 0, counter=0;
boolean up = true;
public void run() {
while(counter<21){
logo.setImageResource(anim[img]);
if(up){
img++;
if(img>=5)
up=false;
}else{
img--;
if(img<=0)
up=true;
}
try{
Thread.sleep(150);
}catch (InterruptedException e){
e.printStackTrace();
}
counter++;
Log.e("Tag",Integer.toString(counter));
}
if(counter>=21){
Intent creditsIntent = new Intent(Splash.this, TitlePage.class);
creditsIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Splash.this.startActivity(creditsIntent);
}
}
});
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}
change your oncreate method like this
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.splash);
final ImageView logo = (ImageView) findViewById(R.id.logoIV);
final int[] anim = new int[6];
anim[0] = R.drawable.logoa;
anim[1] = R.drawable.logob;
anim[2] = R.drawable.logoc;
anim[3] = R.drawable.logod;
anim[4] = R.drawable.logoe;
anim[5] = R.drawable.logof;
Thread t = new Thread(new Runnable()
{
int img = 0, counter = 0;
boolean up = true;
#Override
public void run()
{
while (counter < 21)
{
runOnUiThread(new Runnable()
{
public void run()
{
logo.setImageResource(anim[img]);
}
});
if (up)
{
img++;
if (img >= 5)
up = false;
}
else
{
img--;
if (img <= 0)
up = true;
}
try
{
Thread.sleep(150);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
counter++;
Log.e("Tag", Integer.toString(counter));
}
if (counter >= 21)
{
runOnUiThread(new Runnable()
{
public void run()
{
Intent creditsIntent = new Intent(Splash.this, TitlePage.class);
creditsIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Splash.this.startActivity(creditsIntent);
}
});
}
}
});
t.start();
}
instead of calling runOnUiThread directly do the following:
new Timer().schedule(new TimerTask()
{
#Override
public void run()
{
runOnUiThread(new Runnable()
{
int img = 0, counter=0;
.....
}
}
}, 1000);
I'm not sure if your animation will work, but surely your activity will show up.
Related
I've used a splash screen and progress bar in my Android App.But after splash disappears,then next screen becomes black before switching to my main activity. But I don't want black screen.Can anyone please explain what's going on here and how can I prevent that black screen ? This is my Splash Java class.
public class Splash extends AppCompatActivity {
private ProgressBar mProgress;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
mProgress = (ProgressBar) findViewById(R.id.splash_screen_progress_bar);
new Thread((new Runnable() {
#Override
public void run() {
doWork();
startApp();
finish();
}
}
)).start();
}
private void doWork() {
for (int progress = 0; progress < 100; progress += 10) {
try {
Thread.sleep(5500);
mProgress.setProgress(progress);
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void startApp() {
Intent intent = new Intent(Splash.this, MainActivity.class);
startActivity(intent);
}
}
I think you could remove the finish() inside the Thread.
public class Splash extends AppCompatActivity {
private ProgressBar mProgress;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
mProgress = (ProgressBar) findViewById(R.id.splash_screen_progress_bar);
new Thread((new Runnable() {
#Override
public void run() {
doWork();
startApp();
}
}
)).start();
}
private void doWork() {
for (int progress = 0; progress < 100; progress += 10) {
try {
Thread.sleep(5500);
mProgress.setProgress(progress);
return;
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void startApp() {
Intent intent = new Intent(Splash.this, MainActivity.class);
startActivity(intent);
}
}
Approach 1:
If you required splash for few seconds:
new Thread((new Runnable() {
#Override
public void run() {
try { Thread.sleep(5500); }catch(Exception e) {}
startApp();
finish();
}
}
)).start();
Approach 2: Show next screen when splash finish:
public class Splash extends AppCompatActivity {
private ProgressBar mProgress;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
mProgress = (ProgressBar) findViewById(R.id.splash_screen_progress_bar);
new Thread((new Runnable() {
#Override
public void run() {
doWork();
}
}
)).start();
}
private void doWork() {
for (int progress = 0; progress < 100; progress += 10) {
try {
Thread.sleep(5500);
mProgress.setProgress(progress);
} catch (Exception e) {
e.printStackTrace();
}
}
startApp();
finish();
}
private void startApp() {
Intent intent = new Intent(Splash.this, MainActivity.class);
startActivity(intent);
}
}
Try this one:
public class Splash extends AppCompatActivity {
private ProgressBar mProgress;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
mProgress = (ProgressBar) findViewById(R.id.splash_screen_progress_bar);
new Thread((new Runnable() {
#Override
public void run() {
doWork();
}
}
)).start();
}
private void doWork() {
for (int progress = 0; progress < 100; progress += 10) {
try {
Thread.sleep(5500);
mProgress.setProgress(progress);
} catch (Exception e) {
e.printStackTrace();
}
}
startApp();
}
private void startApp() {
Intent intent = new Intent(Splash.this, MainActivity.class);
startActivity(intent);
finish();
}
}
I want to show numbers from 1 to 100 in sequel order in the TextView and to wait 1 second after printing each number. I also want to implement it using Android services.
I don't know the difference between UIHandler and Handler. When I google about this issue, all I am getting is the difference between handler and a thread.
Please help me out of this,
Thanks in advance
private static final int SHOW_MESSAGE = 1;
private static final int m_cdelay = 1000;
private UIHandler m_cUIHandler;
public int m_cI= 0;
TextView m_cTextShow;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
m_cTextShow = (TextView) findViewById(R.id.textview1);
for(m_cI=1; m_cI <= 100; m_cI++){
//m_cUIHandler = new UIHandler();
//m_cUIHandler.sendEmptyMessageDelayed(SHOW_MESSAGE, 1000);
showMessage(m_cI);
}
}
private void showMessage(int m_cI2) {
for(m_cI=1; m_cI <= 100; m_cI++){
m_cTextShow.setText(""+m_cI);
new Thread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
try {
Thread.sleep(m_cdelay);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}).start();
}
}
#Override
protected void onResume() {
super.onResume();
startService(new Intent(this, NumberService.class));
}
public final class UIHandler extends Handler {
#Override
public void handleMessage(Message pObjMessage) {
switch(pObjMessage.what) {
case SHOW_MESSAGE:
m_cTextShow.setText(""+m_cI);
break;
}
}
}
You can actually rewrite your code like that to make it probably work.
Pls test it and respond.
public void showMessage(int number){
runOnUiThread(new Runnable(){
public void run() {
//Write your number onto the screen
}
});
}
protected void onCreate(Bundle savedInstanceState) {
//Blablabla...
for(m_cI=1; m_cI <= 100; m_cI++){
//m_cUIHandler = new UIHandler();
//m_cUIHandler.sendEmptyMessageDelayed(SHOW_MESSAGE, 1000);
showMessage(m_cI);
Thread.sleep(1000);
}
}
I have change code to below and my purpose is to set a button
when I click the timer run,how to change the code below? thanks
new code below
I have change code to below and my purpose is to set a button
when I click the timer run,how to change the code below? thanks
new code below
public class MainActivity extends Activity {
/** Called when the activity is first created. */
Handler aHandler;
TextView aTextView;
Button aButton;
EditText aEditText;
Handler aHandler01;
TextView aTextView01;
Button aButton01;
EditText aEditText01;
Boolean checker=false;
int count = 11;
int count01 = 11;
#Override
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.activity_main);
aEditText = (EditText)findViewById(R.id.editText01);
aTextView = (TextView)findViewById(R.id.TextView01);
aEditText01 = (EditText)findViewById(R.id.editText02);
aTextView01 = (TextView)findViewById(R.id.TextView02);
aButton=(Button)findViewById(R.id.button01);
aButton01=(Button)findViewById(R.id.button02);
//第一個計時器
aButton.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v){ if(!aEditText.getText().toString().equalsIgnoreCase(""))
{count=Integer.parseInt(aEditText.getText().toString());}
aHandler = new Handler();
if(checker==false){
aHandler.post(runnable);
checker=true;
}
}
});
//第二個計時器
aButton01.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
if (!aEditText01.getText().toString().equalsIgnoreCase("")) {
count01 = Integer.parseInt(aEditText01.getText().toString());
}
aHandler01 = new Handler();
aHandler01.post(runnable01);
}
});
}
//第一個計時器
final Runnable runnable = new Runnable() {
public void run() { // TODO Auto-generated method stub
if (count> 0) {
aTextView.setText(Integer.toString(count-1));
count--;
aHandler.postDelayed(runnable, 1000);
}else{
aTextView.setText("boom");
}
}
};
//第二個計時器
final Runnable runnable01 = new Runnable() {
public void run() { // TODO Auto-generated method stub
if (count01> 0) {
aTextView01.setText(Integer.toString(count01-1));
count01--;
aHandler01.postDelayed(runnable01, 1000);
}else{
aTextView01.setText("boom");
}
}
};
#Override
protected void onPause() { // TODO Auto-generated method stub
super.onPause();
if (aHandler != null) {
aHandler.removeCallbacks(runnable);
}
}
}
try this
private Handler handler = new Handler();
Boolean checker=false;
#Override
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.activity_main);
aEditText = (EditText)findViewById(R.id.editText01);
aTextView = (TextView)findViewById(R.id.TextView01);
aButton=(Button)findViewById(R.id.button01);
aButton.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v){ if(!aEditText.getText().toString().equalsIgnoreCase(""))
{count=Integer.parseInt(aEditText.getText().toString());}
if(!checker){
checker=true;
handler.post(runnable);//to start timer
}
}
});
}
final Runnable runnable = new Runnable() {
public void run() {
// TODO Auto-generated method stub
if(checker)
{
checker=false;
return;
}
if (count> 0) {
aTextView.setText(""+Integer.toString(count-1));
count--;
aHandler.postDelayed(runnable, 1000);
}else{
aTextView.setText("boom");
}
}
};
as you are assigning the value to 'count' variable in OnCreate from editText when editText is empty, it is returning null string which then you are parsing for int value which returns NumberFormatException
try to do it as follows
if (!aEditText.getText().toString().equalsIgnoreCase(""))
{
count = Integer.parseInt(aEditText.getText().toString());
}
OR you can assign a value to 'count' from editText on any button's click or using TextChangedListener(TextWatcher) to monitor and get text from edittext at run time.
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 created a splash screen using the follow code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.splash_layout);
Thread splashThread = 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 {
_active = false;
finish();
startActivity(new Intent(SplashActivity.this, MyMainActivity.class));
}
}
};
splashThread.start();
}
there is an image view in splash_layout, after the splash screen appears for some time duration, and disappears then MyMainActivity starts, the problem is, after the splash disappears and before MyMainActivity starts, I could see previous screen(irrelevant to my app, e.g. desktop with widgets, or previous running app), how to make the transition fluent so that splash screen directly goes to MyMainActivity?
Thanks!
Can you try this i am not sure this is 100% work but try may be helpful..
protected int _splashTime = 3000;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_layout);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
finish();
startActivity(new Intent(SplashActivity.this, MyMainActivity.class));
}
}, _splashTime);
}
Try calling finish() after startActivity().
private static final long SPLASH_SCREEN_MS = 2500;
private long mTimeBeforeDelay;
private Handler mSplashHandler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
// Create a new Handler.
mSplashHandler = new Handler();
}
#Override
protected void onResume() {
super.onResume();
// The first time mTimeBeforeDelay will be 0.
long gapTime = System.currentTimeMillis() - mTimeBeforeDelay;
if (gapTime > SPLASH_SCREEN_MS) {
gapTime = SPLASH_SCREEN_MS;
}
mSplashHandler.postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class);
startActivity(intent);
SplashScreenActivity.this.finish();
}
}, gapTime);
// Save the time before the delay.
mTimeBeforeDelay = System.currentTimeMillis();
}
#Override
protected void onPause() {
super.onPause();
mSplashHandler.removeCallbacksAndMessages(null);
}
For your reference, here is a best example for Android - Splash Screen example.
You can try this code:1
public class MainActivity extends Activity {
private ImageView splashImageView;
boolean splashloading = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
splashImageView = new ImageView(this);
splashImageView.setScaleType(ScaleType.FIT_XY);
splashImageView.setImageResource(R.drawable.ic_launcher);
setContentView(splashImageView);
// interesting music
/**
* Gets your sound file from res/raw
*/
splashloading = true;
Handler h = new Handler();
h.postDelayed(new Runnable() {
public void run() {
splashloading = false;
setContentView(R.layout.activity_main);
}
}, 3000);
}
Best of luck!