Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

deploying a time delay for an inflate layout

So there is this game im trying to create, and in the lessons activity i have a nice scenic background. I want the player to see this for a sec or 2 before my tutorial popup xml inflates. The thing is the transparent popup xml and the background xml both appear as soon as the activity is initiated. Im new at this and cant understand the explanation in android developers homepage. Any help will be much appreciated.

package com.jibushi;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class LessonsShell extends Activity {

    private View view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.lessons);

        final ViewGroup parent = (ViewGroup) findViewById(R.id.lessons_bg);

        Thread splashTread = new Thread() {
            @Override
            public void run() {
                try {
                    wait(1000);
                } catch (InterruptedException e) {

                } finally {
                    view();
                }
            }

            private void view() {
                // TODO Auto-generated method stub
                view = LayoutInflater.from(getBaseContext()).inflate(
                        R.layout.lessons_dialog, null);
                parent.addView(view);
            }
        };
        splashTread.start();
    }
}
like image 515
kishfoo Avatar asked Dec 04 '25 07:12

kishfoo


1 Answers

try this:

public class LessonsShell extends Activity{
private static final int MESSAGE_SHOW_POPUP=7;
private static final long TIME_DELAY=3000;//3 seconds
private View view;
private Handler handler=new Handler(){
   handleMessage(Message msg){
      switch(msg.what){
        case MESSAGE_SHOW_POPUP:
           view();
           break;
       }
   };
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

setContentView(R.layout.lessons);
//this will send a message to the handler to display the popup after 3 seconds.
handler.sendEmptyMessageDelayed(MESSAGE_SHOW_POPUP,TIME_DELAY);

}

private void view() {
// TODO Auto-generated method stub
 ViewGroup parent = (ViewGroup) findViewById(R.id.lessons_bg);
 view = LayoutInflater.from(getBaseContext()).inflate(R.layout.lessons_dialog, null);
 parent.addView(view);
}

}

A handler is an nice replacement for a timer in android.

What you were previously doing was to create a background thread in onCreate, trying to access the UI thread from there. From my experience it should crash, since you cannot access the UI thread from a background thread.

like image 73
rDroid Avatar answered Dec 05 '25 20:12

rDroid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!