At the app I'm developing, I'm interested in an intro of a loading screen. Which automatically move to the next screen after a duration.
The intro itself, is working just fine. As well as the thread who delaying the system.
My problem is to make them work together.
The code:
public class MainActivity extends Activity implements OnClickListener {
ImageView iv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loading_screen);
final Thread t1=new Thread(new Runnable() {
public void run() {
iv=(ImageView)findViewById(R.id.imgBtn1);
iv.setBackgroundResource(R.anim.loading_i_animation);
AnimationDrawable anim=(AnimationDrawable) iv.getBackground();
anim.start();
}
});
t1.start();
try {
Thread.sleep(2000);
finish();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
Intent st=new Intent(MainActivity.this,Welcome.class);
startActivity(st);
}
}
The result of this code is opening a white screen for the thread sleep-time duration. and after that open the "Welcome.class" screen via the intent.
It's just skipping the loading_screen, as is wasn't even exist.
I hope you guys could please help my with that.
You put your sleep on the UI thread which prevents Android to show anything until it finishes. Try the following code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loading_screen);
iv=(ImageView)findViewById(R.id.imgBtn1);
iv.setBackgroundResource(R.anim.loading_i_animation);
AnimationDrawable anim=(AnimationDrawable) iv.getBackground();
anim.start();
new Handler().postDelayed(
new Runnable() {
public void run() {
Intent st=new Intent(MainActivity.this,Welcome.class);
startActivity(st);
finish();
}
}, 2000);
}
This way the delay will run on a separated thread, but after 2 seconds it changes back to the main thread and runs the code you specified in your Runnable
Related
I have a SplashScreen activity at my android app that lasts for 3 seconds, and then it switches to MainActivity.
MainActivity plays a sound, and it has a button. My problem is that the sound plays when the SplashScreen activity is visible.
onResume at MainActivity calls for run(), run() sleeps for 2 seconds and plays the sound.
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
m.start();
try {
Thread.sleep((int) randomValue * 1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
m.seekTo(0);
m.start();
cpu.setText(EranThatWillShortYourDouble(randomValue));
btn.setClickable(true);
}
#Override
protected void onResume() {
super.onResume();
run();
}
Instead of SplashScreen displaying for 3 seconds and switches to MainAcivity, It lasts 5 seconds and plays the sound when SplashScreen is visible.
What do I do?
EDIT: This is my SplashScreen activity:
public class SplashScreen extends Activity {
private static int SPLASH_TIME_OUT = 3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
new Handler().postDelayed(new Runnable() {
/*
* Showing splash screen with a timer. This will be useful when you
* want to show case your app logo / company
*/
#Override
public void run() {
// This method will be executed once the timer is over
// Start your app main activity
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
// close this activity
finish();
}
}, SPLASH_TIME_OUT);
}
}
Please take a look at AsyncTasks, Android Runnables and the ability to use a Handler to postDelayed.
Don't do thread.sleep in the main thread, it's really bad. :)
Do not use splash screens in Android. They are against the design guidelines and provide a poor UX.
http://developer.android.com/design/patterns/help.html#your-app
I have a SetContentView(R.layout.camera);
I want this layout to be start executing after some milliseconds....till then it should be blank. How can I achieve this in android?
For this Write your onCreate() like this..Then it will work..
Thread t = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
t = new Thread(new Runnable() {
#Override
public void run() {
try {
t.sleep(5000);
runOnUiThread(new Runnable() {
public void run() {
setContentView(R.layout.activity_main);
}
});
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
t.start();
}
you can use handler for make delay
Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
//setcontentview
}
};
in oncreate methode
Message m = Message.obtainMessage();
handler.sendMessageDelayed(m, delayMillis);
Your activity will not be visible unless onResume() will get called, Do some operation in main Thread or onCreate() Method it will block your main thread and UI will not be displayed until your operation gets completed.
Create root View with all the components and visibility set to INVISIBLE or GONE and then show it after delay. You can use Handler and postDelayed to execute a code after delay.
I have a problem, I make a simple application to show you my problem.
I want that setContentView executes and displays the .xml BEFORE the Sleep is executed. I thought everything will be execute in order?
Is there anyone how can say me why it doesn't do that?
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// do something
}
Thanks a lot!
EDIT:
Here is the real OnCreate, seems to be a bigger problem.
Everything with the sleep worked fine, but with the Connect method there are problems.
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ConnectBluetooth();
}
In the ConnectBluetooth() method, I just create a new Socket and try a connection.
With a ned thread or a handler it doesn't seems to work, what should I do then? Use something like an asynctask?
Thanks a lot in common!
The layout isn't displayed until after the creation process has finished, after onResume() is called. However there is no callback for when the layout is displayed, but you can use a Handler and Runnable to do this.
Create a couple field variables:
Handler handler = new Handler();
Runnable delay = new Runnable() {
#Override
public void run() {
// Do something
}
};
And onCreate() call:
handler.postDelayed(delay, 10000);
When you call sleep, you are pausing the UI thread. This will prevent onCreate from returning, which will prevent the framework from completing initialization of your activity, including displaying your view hierarchy.
You should never pause the UI thread like that. If you want to do something after 10 seconds, you can start a separate thread that will do it at the right time:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread() {
#Override
public void run() {
try {
Thread.sleep(10000);
doSomething();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
A cleaner approach would be to use a Handler:
Handler mHandler;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mHandler = new Handler();
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
doSomething();
}
}, 10000);
}
I'm having some problems displaying a ProgressDialog. I have a method that scrapes information from a website, and I want to show the user some kind of "Loading" window instead of the app just looking like it is hanging for a second or two when it is working.
Everything works fine when I don't implement a ProgressDialog & Thread, but as soon as I try to implement a Thread to do the heavy lifting, the AboutMe View window is empty.
I have a MainActivity with a TextView that registers a OnClickListener.
A Click on the TextView does a:
startActivity(new Intent(getBaseContext(), AboutMe.class));
This is most of the AboutMe.class Activity:
public class AboutMe extends Activity {
private ProgressDialog aboutMeProgressDialog;
private String htmlAboutMe = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
getAboutMe(); // Get information from Internet
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFeatureDrawableResource(Window.FEATURE_NO_TITLE, android.R.drawable.ic_dialog_alert);
setContentView(R.layout.abutme);
TextView tvAbout = (TextView) findViewById(R.id.aboutMe);
tvAbout.setText(Html.fromHtml(htmlAboutMe));
}
private void getAboutMe() {
try {
aboutMeProgressDialog = ProgressDialog.show(AboutMe.this, "", "Loading");
new Thread() {
#Override
public void run() {
try {
/** Code to scape webpage **/
}
catch (Exception exp) {
exp.printStackTrace();
}
handler.sendEmptyMessage(0);
}
}.start();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
private final Handler handler = new Handler() {
#Override
public void handleMessage(final Message msg) {
aboutMeProgressDialog.dismiss();
}
};
I'm clearly missing out on something trivial, but I've tried to Google just about everything I can think of, but still can't get a Thread together with ProgressDialog to work for me.
please use run on ui thread method
instead of handler.sendEmptyMessage(0); use this code and remove handle message
runOnUiThread(new Runnable() {
#Override
public void run() {
aboutMeProgressDialog.dismiss();
}
});
dude let me know if this was successful,it works most of times
please call getAboutMe() method after calling super.onCreate(savedInstanceState);
I am making an app in which i have to implement alpha effect in splash screen and when i load image it gives null pointer exception.The basic Problem while starting animation.If i remove start animation then my animation do not start at all.I am really stuck.Any help will be appreciated.My code is as follows:
public class SplashScreen extends Activity {
private Thread mSplashThread;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println("hello");
setContentView(R.layout.splash);
final SplashScreen sPlashScreen = this;
Animation a1 = AnimationUtils.loadAnimation(this, R.anim.alpha);
LinearLayout Ll=(LinearLayout)findViewById(R.id.mainLayoutheader);
System.out.println("hello1");
Ll.startAnimation(a1);
System.out.println("hello2");
// The thread to wait for splash screen events
mSplashThread = new Thread(){
#Override
public void run(){
try {
synchronized(this){
// Wait given period of time or exit on touch
// a.reset();
wait(6000);
}
}
catch(InterruptedException ex){
}
finish();
// Run next activity
Intent intent = new Intent();
intent.setClass(sPlashScreen, Main.class);
startActivity(intent);
stop();
}
};
mSplashThread.start();
}
}
Exception is at L1.startAnimation(a1);
I guess you forgot to call setContentView. You have to do that before calling findViewById [edit: doesn't solve the problem here]
You can try timer thread...use this link
http://thedevelopersinfo.wordpress.com/2009/10/18/scheduling-a-timer-task-to-run-repeatedly/
Ll is null or a1 is null and startAnimation throws the exception. The full stack trace shows, which one threw it.
Also:
You might have accidentally typed R.id.mainLayoutheader instead of R.id.mainLayoutHeader.
Check the name R.anim.alpha is correct.
The problem was that the view in which i was showing image was empty.
super.onCreate(savedInstanceState);
// Splash screen view
System.out.println("hello");
setContentView(R.layout.splash);
ImageView iv=(ImageView)findViewById(R.id.splashscreen12);
Animation scaleAnim = AnimationUtils.loadAnimation(this,R.anim.alpha);
iv.startAnimation(scaleAnim);
scaleAnim.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationEnd(Animation animation) {
// Start new activity after some delay
TimerTask newActivity = new TimerTask() {
#Override
public void run() {
startActivity(new Intent(SplashScreeen.this,CallingActivity.class));
SplashScreeen.this.finish();
}
};
Timer t = new Timer(false);
t.schedule(newActivity, 2500);
}
#Override
public void onAnimationRepeat(Animation animation) {
// DO NOTHING
}
#Override
public void onAnimationStart(Animation animation) {
// DO NOTHING
}
});