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. Button on top of image

Hello,

I'm new to Android so I apologize if this is obvious.

I want to draw an image on the screen, which I did, and then I want to
lay some buttons over the image.  Playing around with the XML layout
file I can get the buttons to show up above, below, beside, etc.  How
do I get a button to show up on top of the image and then be able to
move it around programmatically?

-- 

2. Pass Data Between Activities: HashMap of WeakReferences to Objects

I am trying to pass a custom object to one activity from another.
There is a guide that I am using that can be found here:

http://developer.android.com/guide/appendix/faq/framework.html

What I would like to use is the HashMap of WeakReferences, but the
description isn't clear, at least to me.

"A HashMap of WeakReferences to Objects

You can also use a HashMap of WeakReferences to Objects with Long
keys. When an activity wants to pass an object to another activity, it
simply puts the object in the map and sends the key (which is a unique
Long based on a counter or time stamp) to the recipient activity via
intent extras. The recipient activity retrieves the object using this
key."


My poor attempt:

MyObject object = new MyObject();
HashMap map = new HashMap();
WeakReference reference = new WeakReference(object);
map.put(reference.hashCode(), reference);

Intent i =  new Intent(myIntent);
i.putExtra(map);

I know this is probably way off but I can't figure out how this is
intended to be implemented based on the description.  Are the keys
hashcodes, user defined, or what?  It sounds like all that is passed
is a HashMap containing the key values but how is the object retrieved
with just the key?  I couldn't find any code examples or useful
documentation on this method of passing objects so any help would be
greatly appreciated.

Thanks,

-- 

3. Samsung Moment (SPH-M900) - Application crashing

4. Hands free dialing?

5. Toast doesn't leave

6. Unable to determine signal strength on Droid / Android 2.0 (onSignalStrengthsChanged deprecated)

7. java.lang.IllegalStateException: Content of adapter changed but ListView didn't