Skip to content Skip to sidebar Skip to footer

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 in

Solution 1:

try this:

publicclassLessonsShellextendsActivity{
privatestaticfinalint MESSAGE_SHOW_POPUP=7;
privatestaticfinallong TIME_DELAY=3000;//3 secondsprivate View view;
private Handler handler=newHandler(){
   handleMessage(Message msg){
      switch(msg.what){
        case MESSAGE_SHOW_POPUP:
           view();
           break;
       }
   };
};

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stubsuper.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);

}

privatevoidview() {
// TODO Auto-generated method stubViewGroupparent= (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.

Solution 2:

YOu can not change UI elemets and a non UI thread , so this code will not work .

1)show empty background .

2)execute Async task where

DoinBackground()
{
wait 
}
onPostExecute()
{
addView 
}

(refer google if any term is unknown ).

Post a Comment for "Deploying A Time Delay For An Inflate Layout"