0

I want to show a "boot logo" or image before of setting the final layout of the main activity.
Actually i do this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.authors);
    mContext = this;
    showAuthors();

where showAuthors run this:

private void showAuthors()
{
    setContentView(R.layout.authors);
    Thread logoTimer = new Thread() {
        public void run() {
            try {
                sleep(3000);

            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {

            }
        }
    };

    logoTimer.start();
    try {
        logoTimer.join();
        setContentView(R.layout.activity_main);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

the file authors.xml ( R.layout.authors ) is the same of activity_main, but clean, containing just a pair of strings with my name and email:

<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@raw/call2"
android:src="@raw/call2"
android:scaleType = "centerCrop"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/authorsTV"
    android:textColor="#FF6600"
    android:textSize="16.5sp"
    android:textStyle="bold"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="33dp"
    android:text="@string/serviceStatus" />

</RelativeLayout>

the problem is: all works, but the screen is all white as an empty layout.
where i'm doing wrong? thank you all if consider to reply me!

Fujitina
  • 129
  • 1
  • 2
  • 16
  • http://stackoverflow.com/questions/16750059/why-my-splash-screen-dont-show-the-images – Raghunandan Oct 10 '13 at 12:04
  • This is unrelated to your question, but you're setting the author's view in the onCreate method and then again in the showAuthors method. This could create a difficult-to-find bug later if you decide to modify how you show your authors screen. – Tenfour04 Oct 10 '13 at 12:31

2 Answers2

1

First you have two ways of showing a splash screen,

1.with activity

public class SplashActivity extends Activity implements Runnable
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        ...
        setContentView(R.layout.act_spalsh);
        hideSplashActivity();
    }
    private void hideSplashActivity()
    {
        new Handler().postDelayed(this, 3000);
    }
    @Override //from runnable
    public void run()
    {
        startActivity(new Intent(this, MainActivity .class));
        finish();

    }

2.with dialog

public class MainActivity extends Activity implements Runnable
{
    Dialog splashDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        ...
        splashDialog = new Dialog(this,android.R.style.Theme_Black_NoTitleBar_Fullscreen);
        splashDialog.setContentView(R.layout.dlg_splash);
        splashDialog.show();
        hideSplashDialog();
        setContentView(R.layout.act_main);
    }
    private void hideSplashDialog()
    {
        new Handler().postDelayed(this, 3000);
    }
    @Override //from runnable
    public void run()
    {
        splashDialog.dismiss();
        splashDialog = null;
    }

I prefer using dialog unless you have some trouble with that

Ahmad Dwaik 'Warlock'
  • 5,953
  • 5
  • 34
  • 56
1

You should not call thread.join from the main thread, because you can get an ANR crash.

Here's how to do it with minimal changes to your existing code. Remove everything after logoTimer.start(), and put this inside the try block, immediately after sleep(3000):

runOnUiThread(new Runnable(){
    public void run(){
        setContentView(R.layout.activity_main);
    }
});

But it would be cleaner to rewrite that whole method like this:

private void showAuthors()
{
    setContentView(R.layout.authors);
    new Handler().postDelayed( new Runnable(){
        public void run(){
            setContentView(R.layout.activity_main);
        }
    }, 3000);
}
Tenfour04
  • 83,111
  • 11
  • 94
  • 154