0

I want to make an intro for my android application, so i was thinking to do that in this way :

This is my intro.xml

   <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical" >

    <ImageView
    android:id="@+id/imageView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:src="@drawable/logo_inesc" />

  </LinearLayout>

And imagine my main.xml with some menus and images.

When the user is starting the app i would like to show him first one image of presentation and then the app itself with options and whatever.

I did this in my activity :

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.intro);

    try {
        Thread.sleep(6000); //Intro image will be shown for 6 seconds
        setContentView(R.layout.home);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

I don't know if this is the right procedure to do it, the layouts are changing correctly, but the image is not being showed. Someone knows why?

Regards.

unpix
  • 853
  • 2
  • 15
  • 31
  • 1
    Are you trying to give an Intro of the app? If so, try this library, https://github.com/PaoloRotolo/AppIntro – Vettiyanakan Sep 10 '15 at 08:02
  • Check my optimal and easy solution: https://medium.com/@vatani.ahmad/android-optimal-splash-screen-without-extra-activity-or-fragment-b60fea45a0cc – Ahmad Vatani May 01 '20 at 10:16

3 Answers3

3

While this solution might work or something like this might be better:

public class SplashActivity extends Activity {
    protected boolean active = true;
    protected int splashTime = 1000;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_screen);
        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 {
                    finish();
                    // Start your Activity here
               }
           }
       };
       splashTread.start();    
   }
}

But what if the user presses the back-key (and closes your app) before the splash-delay is over. The app will probably still open the next Activity, which isn't really user-friendly.

Also having a sleep in your GUI is bad practice.

Create a AsyncTask or another seperate Thread.

This guy has a great solution where the splashscreen actually fades.

Community
  • 1
  • 1
Timmetje
  • 7,641
  • 18
  • 36
1
  1. you don't need a LinearLayout if it contains only one child.
  2. Use an AsyncTask for the Thread.sleep(), otherwise you are pausing the UI thread
SimonSays
  • 10,867
  • 7
  • 44
  • 59
0

You are making the current thread (UI Thread) sleep, In order to pause the screen for 6 secs you need create a separate thread.

Thread t=new Thread(
new Runnable()
{
public void run()
{
sleep(6000);
}
}
);
t.start();

........

 setContentView(R.layout.intro);

    try {
        Thread t=new Thread(
    new Runnable()
    {
    public void run()
    {
    Thread.sleep(6000);
    }
    }
    );
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
finally
{
// start new activity with intent if you have a new activity or if you want to change the //contentView change here
setContentView(R.layout.home);
}
  t.start();
Pragnani
  • 20,075
  • 6
  • 49
  • 74
  • Downvoter should have a dare to comment – Pragnani Mar 13 '13 at 16:45
  • 2
    I would imagine the down vote is from your answer doing nothing more than failing to compile. And even if it did compile, it would still do nothing more than spawn a thread that does nothing for 6 seconds. – Justin Breitfeller Mar 13 '13 at 17:22
  • @JustinBreitfeller Don't be there in imagination, be in practical, I am not against downvote, instead I will welcome it, but the matter is what's wrong in the answer. So I need to write his entire code rather than giving idea.? right, Why do you think this code will fail..?What wrong you have found in that let me know. – Pragnani Mar 13 '13 at 17:36
  • 2
    While I appreciate the unneeded special treatment, your edited answer is still wrong. It still won't compile, and if it did, it would still crash. sleep() method doesn't exist in Runnable and setContentView can't be called from a background thread. I'm simply trying to ensure others don't mistakenly use incorrect code. – Justin Breitfeller Mar 13 '13 at 18:27
  • @JustinBreitfeller SetContentView isn't called on thread. Check the code, If it needs to call on thread it should be called in a handler I know that, sorry Brother If I was too rude I will edit my answer for sleep(). – Pragnani Mar 13 '13 at 18:32