Hello i am trying to create splash screen in android with one image but i want to display multiple images at a time in splash screen when app is launch.
but it only displays one image please any solution for this please help me.
Here is my code.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
for(int i=0;i<5;i++)
{
progress +=50;
h.post(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
pgr.setProgress(progress);
if(progress==pgr.getMax())
{
//pgr.setVisibility(4);
Intent in= new Intent(getApplicationContext(),Home.class);
startActivity(in);
}
}
});
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO: handle exception
}
}
}
}).start();
}
}
The simplest solution would be the use of CountDown timer,
like,
below code would change activity after 10 sec and also change image after every 1 sec (change it according to your login). int variable i to keep track of image changing number.
int i = 0;
new CountDownTimer(10000, 1000) {
public void onTick(long millisUntilFinished) {
i++;
if(i == 1){
imageview.setImageResource(R.drawable.image1);
}
else if(i == 2){
imageview.setImageResource(R.drawable.image2);
}
else if(i == 3){
imageview.setImageResource(R.drawable.image3);
}
//and so on..........................
}
public void onFinish() {
//finish your splash screen activity
SplashActivity.this.finish();
}
}.start();
Related
I am developing a quiz application in which I am using runnable thread for seekbar functionality. seekbar shows how much time is remaining. Now I want to stop the seekbar when a answer button is click.
here is my code for runnable thread.
new Thread(new Runnable() {
public void run() {
while (seekbarStatus < 100) {
seekbarStatus = LoadingStatus();
// sleep 1 second to show the progress
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
// Update the progress bar
progressBarHandler.post(new Runnable() {
public void run() {
seekbar.setProgress(seekbarStatus);
}
});
}
if (seekbarStatus >= 100) {
// sleep for 2 seconds, so that you can see the 100% of file download
runOnUiThread(new Runnable() {
#Override
public void run() {
if(!(Ans_1.getTag()=="clicked" ||Ans_2.getTag()=="clicked" || Ans_3.getTag()=="clicked" ))
{
Ans_1.setEnabled(false);
Ans_2.setEnabled(false);
Ans_3.setEnabled(false);
Ans_1.setBackgroundColor(Color.GREEN);
points.setText("0 Punkt");
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
/* Create an Intent that will start the Menu-Activity. */
showDialogView();
}
}, SPLASH_DISPLAY_LENGHT);
}
}
public void showDialogView() {
// TODO Auto-generated method stub
dialog.show();
}
});
}
}
}).start();
please help me. Any help would be appreciated.
I am not getting how to solve it.
Thanks in advance.
Just add an instance variable:
private boolean isCancelled = false;
Then in the button onClick:
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
isCancelled = true;
}
});
Then change
while (seekbarStatus < 100) {
to
while (seekbarStatus < 100 && !isCancelled) {
I have this code in an attempt to have my app change it's background every 5 seconds, however, it only displays the first image in my array. Is there a simple tweak to this I can make so it will run through the 5 different images continuously??
Here is what I have in my MainActivity.class
int count=0; //outside oncreate
//all that is below is within oncreate
final int[] drawablearray=new int[]{R.drawable.one,R.drawable.two,R.drawable.three,R.drawable.four,R.drawable.five};
new Handler().postDelayed(new Runnable() {
public void run() {
if(count<drawablearray.length){
MainActivity.this.getWindow().
setBackgroundDrawableResource(drawablearray[count]);
count++;
}
else{
count=0;
}
}
}, 5000);
Thanks for all and any help!!
You need to call the method recursivly. The way you have currently set it up you only call it once.
int count =0;
boolean abort;
onResume(){
super.onResume();
abort = false;
this.changeBackground(count);
}
onPause(){
abort = true;
super.onPause();
}
private void changeBackground(int count){
if (abort)
return;
new Handler().postDelayed(new Runnable() {
public void run() {
if(count<drawablearray.length){
count++;
}
else{
count=0;
}
changeBackgroundColor(count);
changeBackground(count);
}
}, 5000);
}
private void changeBackgroundColor(int count){
if (abort)
return;
MainActivity.this.getWindow().
setBackgroundDrawableResource(drawablearray[count]);
}
Spin in an infinite loop. Also, you can remove the if-else condition with %:
while (changeBackground == true)
{
new Handler().postDelayed(new Runnable() {
public void run() {
MainActivity.this.getWindow().
setBackgroundDrawableResource(drawablearray[count]);
count = (count + 1) % drawablearray.length;
}, 5000);
}
PS: You may want to configure background change from some event, like on Event e, background changing should stop. One example of such event may be switching from one activity to other. You can achieve this by setting/resetting variable changeBackground.
int count=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
//all that is below is within oncreate
final int[] drawablearray=new int[]{R.drawable.one,R.drawable.two,R.drawable.three,R.drawable.four,R.drawable.five};
gettingBackground(drawablearray);
}
private void gettingBackground(final int[] drawablearray) {
// TODO Auto-generated method stub
new Handler().postDelayed(new Runnable() {
public void run() {
if(count<drawablearray.length){
LoginAct.this.getWindow().
setBackgroundDrawableResource(drawablearray[count]);
count++;
gettingBackground(drawablearray);
}
else{
count=0;
}
}
}, 5000);
}
I want to upload code here but I promise you, it's a mess and you do not want to see it. I've been trying to somewhat follow the Facebook 3.6 Samples and use their API but it is so much more complicated than the previous version.
Has anybody accomplished this? Could anybody point me in the right direction?
Here I included the code which i had done in my project. Please check this out. It may help you.
Animation animationFadeIn = AnimationUtils.loadAnimation(this, R.anim.fade_in);
Animation animationFadeOut = AnimationUtils.loadAnimation(this, R.anim.fade_out);
handler = new Handle(this);
animator = new Thread() {
public void run() {
try {
handler.sendMessage(handler.obtainMessage(1));
sleep(2000);
handler.sendMessage(handler.obtainMessage(2));
sleep(2000);
handler.sendMessage(handler.obtainMessage(3));
sleep(2000);
handler.sendMessage(handler.obtainMessage(4));
} catch (Exception e) {
e.printStackTrace();
}
}
};
animator.start();
static class Handle extends Handler {
private final WeakReference<SplashActivity> mSplash;
Handle(SplashActivity splash) {
// weakReference is used for avoiding memory leak
mSplash = new WeakReference<SplashActivity>(splash);
}
#Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
SplashActivity splash = mSplash.get();
if (msg.what == 1 && splash.isRunning == true) {
splash.iv.startAnimation(splash.animationFadeIn);
} else if (msg.what == 2 && splash.isRunning == true) {
} else if (msg.what == 3 && splash.isRunning == true) {
splash.iv.startAnimation(splash.animationFadeOut);
} else if (msg.what == 4 && splash.isRunning == true) {
splash.iv.setImageResource(android.R.color.black);
Intent i = new Intent(splash, MainActivity.class);
splash.overridePendingTransition(0, 0);
splash.startActivity(i);
splash.finish();
}
super.handleMessage(msg);
}
}
Check this example:
public class SplashActivity extends Activity {
private long splashDelay = 3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
// Remove notification bar
/*
* this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
* WindowManager.LayoutParams.FLAG_FULLSCREEN);
*/
setContentView(R.layout.activity_splash);
TimerTask task = new TimerTask() {
#Override
public void run() {
finish();
startActivity(new Intent().setClass(SplashActivity.this,
MainActivity.class));
overridePendingTransition(R.anim.fade, R.anim.hold);
}
};
Timer timer = new Timer();
timer.schedule(task, splashDelay);
}
}
Source: Android Activity Splash Screen
refer here for facebook splash UI: Facebook's splash screen
I write a Splash Screeen to run at the boot time of application
public class SplashScreen extends Activity {
ImageView imgView;
int[] imgID = new int[]{R.drawable.frame0, R.drawable.frame1, R.drawable.frame2, R.drawable.frame3,
R.drawable.frame4, R.drawable.frame5, R.drawable.frame6};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
imgView = (ImageView) findViewById(R.id.imgSplash);
new Thread(new WelcomeScreen()).start();
}
private class WelcomeScreen implements Runnable {
#Override
public void run() {
try {
for (int i = 0; i < imgID.length; i++)
{
imgView.setImageResource(imgID[i]);
sleep(500);
}
} catch (InterruptedException e) {
}finally {
Intent intent = new Intent(SplashScreen.this,LoginActivity.class);
startActivity(intent);
finish();
}
}
}
}
It getting error "Sorry the application has stopped unexpectedly" . I don't know why . Somebody can help me ????
you can not set the resource for yuor ImageView inside a thread different from the UI Thread.
you can use runOnUiThread. It takes as paramter a runnable, and post it in the UI Thread queue. There, the UI thead takes it and update your ImageView. All in all your runnable will become:
private class WelcomeScreen implements Runnable {
#Override
public void run() {
try {
for (int i = 0; i < imgID.length; i++)
{
final int resuorceId = imgID[i];
runOnUiThread(new Runnable() {
#Override
public void run() {
imgView.setImageResource(resuorceId);
}
});
sleep(500);
}
} catch (InterruptedException e) {
}finally {
Intent intent = new Intent(SplashScreen.this,LoginActivity.class);
startActivity(intent);
finish();
}
}
You can not access your views from Thread.
You will need to put your code imgView.setImageResource(imgID[i]); in runOnUiThread
use like:
runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
imgView.setImageResource(imgID[i]);
}
});
Thanks
You can not change something in UI from non-UI thread so replace this you code:
imgView.setImageResource(imgID[i]);
to:
runOnUiThread(new Runnable() {
#Override
public void run() {
imgView.setImageResource(imgID[i]);
}
});
//try code this way...
public class SplashScreen extends Activity {
private Intent launchIntent;
private Thread splashThread; //used for perform splash screen operation
private int splashTime = 10000, sleepTime = 50; //used for threading operation
private boolean active = true; //used for touch event
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen); //Set splashscreen.xml here
try {
splashThread = new Thread() { // Creating Thread for splash the screen
#Override
public void run() { // run method implemented to perform threading operation
try {
int waitTime = 0; //counter for threading
do {
sleep(sleepTime); //delay for specific time
if (active)
waitTime += 100;
//write your image code here that display your no. of images
} while (active && (waitTime < splashTime)); //Check touch condition and counter
} catch (Exception e) {
// to handle runtime error of run method
Validation.displayToastMessage(SplashScreen.this, e.toString()); //Call static method of class ToastMessage
}
finish(); //finish current activity
startJustCoupleActivityScreen(); //Call below defined function
}
};
splashThread.start(); //start thread here
} catch (Exception e) {
message("SplashScreen : "+ e.toString()); //Call static method of class ToastMessage
}
}
public void startJustCoupleActivityScreen() {
launchIntent=new Intent(SplashScreen.this,JustCoupleActivity.class); //call Next Screen
startActivity(launchIntent); //start new activity
}
#Override
public boolean onTouchEvent(MotionEvent event) { //onTouch Event
//on touch it immediate skip splash screen
if(event.getAction()==MotionEvent.ACTION_DOWN) active=false; //Check Touch happened or not
return true;
}
public void message(String msg)
{
Validation.displayToastMessage(SplashScreen.this, msg); //display Error Message
}
}
I use this Tutorial to create custom Progressbar and it works .
But I want to start new activity when progress bar go to 100% .
anyone can help to put start new activity code in correct place?
You can check if the progress has reached the maximum possible while you're setting it, like this:
#Override
public synchronized void setProgress(int progress) {
super.setProgress(progress);
// the setProgress super will not change the details of the progress bar
// anymore so we need to force an update to redraw the progress bar
invalidate();
if(progress >= getMax()) {
Intent intent....
}
}
You can call the onContinue function timer thread and set the intent to next activity and register the activity name in manifest file.
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.progress);
mProgressBar = (ProgressBar)findViewById(R.id.adprogress_progressBar);
final Thread timerThread = new Thread() {
#Override
public void run() {
mbActive = true;
try {
int waited = 0;
while(mbActive && (waited < TIMER_RUNTIME)) {
sleep(200);
if(mbActive) {
waited += 200;
updateProgress(waited);
}
}
} catch(InterruptedException e) {
} finally {
onContinue();
}
}
};
timerThread.start();
}
#Override
public void onDestroy() {
super.onDestroy();
}
public void updateProgress(final int timePassed) {
if(null != mProgressBar) {
final int progress = mProgressBar.getMax() * timePassed / TIMER_RUNTIME;
mProgressBar.setProgress(progress);
}
}
public void onContinue() {
Intent intd=new Intent(this,MainActivity.class);
startActivity(intd);
}
Try this...
new Thread(new Runnable() {
public void run() {
while (progressStatus < 100) {
progressStatus += 5;
// Update the progress bar and display the current value in the text view
handler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressStatus);
textView.setText("Loading "+progressStatus+"/"+progressBar.getMax());
}
});
try {
// Sleep for 200 milliseconds. Just to display the progress slowly
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
**Intent intent = new Intent(Main_Activity.this, Send_Email.class);
startActivity(intent);**
}
}).start();