1

I want to make a thread which will change the layout of my activity ... I have 2 layouts : welcomepage and activity_main ... The goal of thread: when I launch my application , the welcomepage layout will be visible in only 5 sec and after that the layout again will be activity_main ...

I wrote the code as below:

package com.example.tripolimazad;

import android.os.Bundle;  
import android.app.Activity; 
import android.view.Menu; 
import android.widget.TextView;

public class MainActivity extends Activity {

    public TextView counter = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcomepage);
        counter = (TextView) findViewById(R.id.Counter);

        Thread th=new Thread(){ 
            @Override
            public void run(){
                try
                {  
                            Thread.sleep(10000);  
                            setContentView(R.layout.activity_main);   
                }catch (InterruptedException e) {
                } 
            }

        };
        th.start();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

but it doesnt work !!! have anyone any solution plz !

Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124

4 Answers4

3

You cannot change the UI on a non-UI thread, however in an Activity, you can use the runOnUiThread method:

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

This seems very strange though, to have this in your onCreate.

Phil
  • 35,852
  • 23
  • 123
  • 164
  • 1
    @AhmadMoussa, glad i could help. Remember to accept this answer (and gain +2 reputation yourself)! – Phil Aug 22 '13 at 20:20
  • 1
    yes of course i will do it :) but stackoverflow need 10 minutes to accepte my acceptenece of ur answer ... i will do it (y) –  Aug 22 '13 at 20:22
1

You may also try to use CountDownTimer something like:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);    
    setContentView(R.layout.welcomepage);
    //display the logo during 5 secondes,
    new CountDownTimer(5000,1000){
        @Override
        public void onTick(long millisUntilFinished){} 

        @Override
        public void onFinish(){
               //set the new Content of your activity
               MainActivity.this.setContentView(R.layout.main);
        }
   }.start();
   //...
}

See Displaying logo for few seconds at application start for more.

Community
  • 1
  • 1
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
0

UI related operations must be done on UI thread only and must not be done in non-UI thread as you are doing. But you can update UI from non-UI thread as follows:

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

However a better approach for the situation you mentioned is to use an Async task, provided by the Android.You can try following :

/*
 * Activity/Thread to display the **welcomepage** 
 * when the app is started.
 */
public class SplashActivity extends Activity {

    // how long until we go to the next activity
    protected int _splashTime = 5000; // 5 seconds

    // thread to display welcome page
    private Thread splashTread;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.your_welcome_page_layout);

        // thread for displaying the WelcomeScreen
        splashTread = new Thread() {
            @Override
            public void run() {
                try {
                    synchronized (this) {
                        // wait 5 sec
                        wait(_splashTime);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    // finish the current splashactivity 
                    finish();                   
                    // start MainActivity as next activity
                    startActivity(new Intent(SplashActivity.this, MainActivity.class));
                }
            }
        };

        // start the thread.
        splashTread.start();
    }


}
Ritesh Gune
  • 16,629
  • 6
  • 44
  • 72
0

Alternatively you can use a handler to avoid creating a new thread as following:

    getWindow().getDecorView().getHandler().postDelayed(new Runnable() {
        @Override public void run() {
            setContentView(R.layout.activity_main);
        }
    }, 10000);
sergej shafarenka
  • 20,071
  • 7
  • 67
  • 86