Change the View

by and . modi » Tue, 11 Nov 2008 05:08:01 GMT


Sponsored Links
 Hi,
I am developing a game.
When the game starts, it shows one screen (Splashscreen).I have
created xml file for that which contains only backgroudn. Nothing
else.

I want that after some time (2 sec proabably), it should switch to the
new view, which shows the menu for the game. Now I have created xml
for that also.
What I want is
[1] When view for menu loads, and if user presses back, user should
not get that splashscreen back.
[2] The view should be changed automaticlly (from splash to menu). And
I m not sure, that how can I do this. I googled and what I found is I
have to create activities for each views and create diffrent java file
for that and use Intent to call the view, But I don't find any code
that properly explain this.

If any can throw some light on this, it will be really helpful.
P.S. I m doing this for my college project.

Thanks in advance.

--~--~---------~--~----~------------~-------~--~----~



Change the View

by Dennis W. » Tue, 11 Nov 2008 10:34:37 GMT


 I think, a thread is usefull to switch from your splash screen to the
main menu. To fix your first problem, I've got no idea, unfortunately.

public class SplashScreen extends Activity {

    private static final String TAG = "SplashScreen";
    private static final int MAIN_MENU_ID = Menu.FIRST;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_screen);

        class SleepClass implements Runnable {
            public void run() {
                try {
                    Log.i(TAG, "loading application");
                    Thread.sleep(3000); // wait 3 seconds
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Intent myIntent = new Intent(SplashScreen.this,
YourMainApplication.class);
                startActivity(myIntent);
            }
        }

        Thread t1 = new Thread(new SleepClass());
        t1.start();
    }
}



--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


Change the View

by Mark Murphy » Tue, 11 Nov 2008 13:32:47 GMT


 


1. You can use ViewFlipper to hold both the splash screen and the menu 
in the same layout file. Then, in onCreate(), use View#postDelayed() to 
trigger a switch to the menu after so many seconds.

2. You apparently can call setContentView() multiple times without 
problems, though I have not tried this. So, in onCreate(), you would 
call setContentView() for the splash screen, then use View#postDelayed() 
to call setContentView() for the menu.

In either case, you have a single activity.

I have a blog post up describing the ViewFlipper technique, though it is 
for an older SDK and may require fixes to the poorly-formatted source code:

 http://androidguys.com/?p=654 

ViewFlipper also makes it easy to set up custom animations for 
transitioning from the splash screen to the menu.

-- 
Mark Murphy (a Commons Guy)
 http://commonsware.com 
_The Busy Coder's Guide to Android Development_ Version 1.4 Published!

--~--~---------~--~----~------------~-------~--~----~



Change the View

by [EMAIL PROTECTED] » Fri, 14 Nov 2008 03:46:49 GMT


 i,
I use code as shown by you. But the problem is that I am not getting
the screen shown by main.xml file (first screen).
When I press back button from the screen viewed by menu.xml file
(second view), it shows view by main.xml file (so it is there but
doesn't come up). So now I don't understand why it doesn't come up in
first place.


The code is as follows:

package com.naag;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import android.view.Window;
public class naag extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);

//No Title bar will be displayed
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);

class SleepClass implements Runnable
{
public void run()
{
try
{
Thread.sleep(10000); // wait 10 seconds but not
getting this..
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}

Thread t1 = new Thread(new SleepClass());
t1.start();

Intent menuLauch = new Intent (this, menu.class);
startActivity (menuLauch);

}
}


The problem is I am not getting delay of 10 second.
I provided delay in menu.java file for 2 sec and it is there.

The menu.java file is as follows...
package com.naag;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;

public class menu extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);

//No Title bar will be displayed
requestWindowFeature(Window.FEATURE_NO_TITLE);
try {
Thread.sleep (2000); ///////////////////only
getting this
delay
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
setContentView(R.layout.menu);
}
}

I have also changed AndroidManifest.xml to use intents..

Also
What I found more is:
These two lines of code also not working properly.

requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);

When the application starts, only black screen with Title of Game
comes up. After 2 seconds, the view displayed by menu.xml file comes
up. (Actully it should display first screen provided by main.xml and
that is also without Title bar and when I press back button from menu,
it shows main screen without title bar).
So don't uderstand when I am messing up.
-------------------------------------------------------------------------------------------------------------------------------

The viewflipper is a good idea but its not useful in my application.
Thanks for that information.

Also by changing setContentView(R.layout.menu); after setContentView
(R.layout.main); doesn't help.
As it provides only second view and that is without first even show up
and its not there in history stack.

So please help me in this problem.

Thanks for your replies.

On Nov 11, 8:32a



Change the View

by [EMAIL PROTECTED] » Fri, 14 Nov 2008 11:54:37 GMT


 package com.naag;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

import android.view.Window;
public class naag extends Activity {

        private final int SPLASH_DISPLAY_LENGTH = 2500;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle)
    {
        super.onCreate(icicle);

        //No Title bar will be displayed
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);                   //main is the
first view.

                new Handler().postDelayed(new Runnable(){
            @Override
            public void run(){
                 /*Create an Intent that will start the Menu.Activity
*/
                 Intent mainIntent = new Intent( naag.this,
menu.class ); //menu.class is second view
                 naag.this.startActivity( mainIntent );
                 naag.this.finish();
            }
       }, SPLASH_DISPLAY_LENGTH );
    }

};


I used this code and working perfectly fine...

--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. app in Android market

Hello,
I notice that many people download my app in the first two hours after I
publish my app. After that the downloads are much less than the first two
hours.

Do you know why this happen? Anything to avoid this?

Thanks

--~--~---------~--~----~------------~-------~--~----~

2. Keeping an application running in the background

Hi --

Yet another question -- we have an application that connects to a
device and therefore needs to keep running in the background even when
there are no Activities active.  What is the recommended way to
indicate that our application is still active and for it not to be
killed automatically?

Thanks!
dennis

--~--~---------~--~----~------------~-------~--~----~

3. Any Insight on this crash?

4. Delete assets at runtime

5. "Apple iPhone Developers Mostly Don't Make Much Money"

6. An advanced list view

7. LiveFolder